Skip to content

Commit

Permalink
MQTT连接失败时重试连接
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardlcl committed Sep 22, 2022
1 parent e38c71a commit 6df6c19
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
16 changes: 11 additions & 5 deletions custom_components/mhtzn/Gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,19 @@ async def reconnect(self, entry: ConfigEntry):
mqtt_client.init_client()
await mqtt_client.async_connect()

async def init(self):
async def init(self, entry: ConfigEntry):
"""Initialize the gateway business logic, including subscribing to device data, scene data, and basic data,
and sending data reporting instructions to the gateway"""

mqtt_connected = self._hass.data[MQTT_CLIENT_INSTANCE].connected
_LOGGER.warning(mqtt_connected)

while not mqtt_connected:
await self.reconnect(entry)
await asyncio.sleep(1)
mqtt_connected = self._hass.data[MQTT_CLIENT_INSTANCE].connected
_LOGGER.warning(mqtt_connected)

discovery_topics = [
# Subscribe to device list
f"{MQTT_TOPIC_PREFIX}/center/p5",
Expand All @@ -171,10 +181,6 @@ async def init(self):
for topic in discovery_topics
)
)
await asyncio.sleep(5)
mqtt_connected = self._hass.data[MQTT_CLIENT_INSTANCE].connected

_LOGGER.warning(mqtt_connected)

if mqtt_connected:
# publish payload to get device list
Expand Down
9 changes: 7 additions & 2 deletions custom_components/mhtzn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""The Detailed MHTZN integration."""
from __future__ import annotations

import asyncio
import logging

from homeassistant.config_entries import ConfigEntry
Expand All @@ -20,7 +21,9 @@ async def _async_config_entry_updated(hass: HomeAssistant, entry: ConfigEntry) -
"""reconnect gateway"""
await hub.reconnect(entry)
"""Initialize gateway information and synchronize child device list to HA"""
await hub.init()
hass.async_create_task(
hub.init(entry)
)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Expand Down Expand Up @@ -48,7 +51,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await hub.connect()

"""Initialize gateway information and synchronize child device list to HA"""
await hub.init()
hass.async_create_task(
hub.init(entry)
)

"""Add an entry configuration change event listener to trigger the specified method
when the configuration changes"""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mhtzn/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get_connection_name(discovery_info):
return discovery_info.name.replace(f".{service_type}.", "")


def color_temp_to_rgb(color_temp) -> tuple[int, int, int] | None:
def color_temp_to_rgb(color_temp) -> tuple[int, int, int]:
if color_temp < 1000:
color_temp = 1000.0

Expand Down

0 comments on commit 6df6c19

Please sign in to comment.