From 239197fde7963a0fe915c682343a3ee0d1140fbf Mon Sep 17 00:00:00 2001 From: Minims Date: Wed, 25 Sep 2024 21:32:22 +0200 Subject: [PATCH] fix: Re-subscribe to all topic on MQTT reconnect --- .vscode/settings.json | 5 ++++- myFox2Mqtt/business/__init__.py | 12 +++++++++++- myFox2Mqtt/business/mqtt.py | 2 ++ myFox2Mqtt/homeassistant/ha_discovery.py | 25 ++++++++++++------------ myFox2Mqtt/mqtt/__init__.py | 11 +++++++++-- myFox2Mqtt/myfox/api/__init__.py | 1 + myFox2Mqtt/myfox/api/devices/base.py | 1 + myFox2Mqtt/myfox/api/devices/category.py | 1 + myFox2Mqtt/myfox/sso/__init__.py | 1 + myFox2Mqtt/utils/__init__.py | 2 +- 10 files changed, 44 insertions(+), 17 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index c968dee..9d56613 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,5 +8,8 @@ "python.formatting.provider": "none", "black-formatter.args": [ "-l 120" - ] + ], + "files.associations": { + "*.yaml": "home-assistant" + } } \ No newline at end of file diff --git a/myFox2Mqtt/business/__init__.py b/myFox2Mqtt/business/__init__.py index 9361886..2738602 100644 --- a/myFox2Mqtt/business/__init__.py +++ b/myFox2Mqtt/business/__init__.py @@ -19,7 +19,7 @@ DEVICE_CAPABILITIES, ALARM_STATUS, ) -from business.mqtt import mqtt_publish +from business.mqtt import mqtt_publish, SUBSCRIBE_TOPICS from mqtt import MQTTClient LOGGER = logging.getLogger(__name__) @@ -58,6 +58,7 @@ def ha_sites_config( retain=True, ) mqtt_client.client.subscribe(site_config.get("config").get("command_topic")) + SUBSCRIBE_TOPICS.append(site_config.get("config").get("command_topic")) history = ha_discovery_history( site=my_site, @@ -89,6 +90,7 @@ def ha_sites_config( retain=True, ) mqtt_client.client.subscribe(play_scenario.get("config").get("command_topic")) + SUBSCRIBE_TOPICS.append(play_scenario.get("config").get("command_topic")) def ha_devices_config( @@ -152,6 +154,7 @@ def ha_devices_config( ) if device_config.get("config").get("command_topic"): mqtt_client.client.subscribe(device_config.get("config").get("command_topic")) + SUBSCRIBE_TOPICS.append(device_config.get("config").get("command_topic")) if "Myfox HC2" in device.device_definition.get( "device_definition_label" @@ -184,6 +187,7 @@ def ha_devices_config( retain=True, ) mqtt_client.client.subscribe(reboot.get("config").get("command_topic")) + SUBSCRIBE_TOPICS.append(reboot.get("config").get("command_topic")) halt = ha_discovery_devices( site_id=site_id, @@ -198,6 +202,8 @@ def ha_devices_config( retain=True, ) mqtt_client.client.subscribe(halt.get("config").get("command_topic")) + SUBSCRIBE_TOPICS.append(halt.get("config").get("command_topic")) + # Manual Snapshot device_config = ha_discovery_devices( site_id=site_id, @@ -213,6 +219,7 @@ def ha_devices_config( ) if device_config.get("config").get("command_topic"): mqtt_client.client.subscribe(device_config.get("config").get("command_topic")) + SUBSCRIBE_TOPICS.append(device_config.get("config").get("command_topic")) # Temperature for temperature_device in temperature_devices: @@ -301,6 +308,7 @@ def ha_devices_config( retain=True, ) mqtt_client.client.subscribe(shutter.get("config").get("command_topic")) + SUBSCRIBE_TOPICS.append(shutter.get("config").get("command_topic")) # Gate for gate_device in gate_devices: @@ -319,6 +327,7 @@ def ha_devices_config( retain=True, ) mqtt_client.client.subscribe(gate.get("config").get("command_topic")) + SUBSCRIBE_TOPICS.append(gate.get("config").get("command_topic")) # Sockets for socket_device in socket_devices: @@ -337,6 +346,7 @@ def ha_devices_config( retain=True, ) mqtt_client.client.subscribe(socket.get("config").get("command_topic")) + SUBSCRIBE_TOPICS.append(socket.get("config").get("command_topic")) # Works with Websockets if "Télécommande 4 boutons" in device.device_definition.get("device_definition_label"): diff --git a/myFox2Mqtt/business/mqtt.py b/myFox2Mqtt/business/mqtt.py index 7631057..0ab7cbd 100644 --- a/myFox2Mqtt/business/mqtt.py +++ b/myFox2Mqtt/business/mqtt.py @@ -1,4 +1,5 @@ """ MQTT Business""" + import json import logging from time import sleep @@ -8,6 +9,7 @@ from myfox.api import MyFoxApi, ACTION_LIST LOGGER = logging.getLogger(__name__) +SUBSCRIBE_TOPICS = [] def mqtt_publish(mqtt_client, topic, payload, qos=0, retain=True, is_json=True): diff --git a/myFox2Mqtt/homeassistant/ha_discovery.py b/myFox2Mqtt/homeassistant/ha_discovery.py index 630a3ef..694d97d 100644 --- a/myFox2Mqtt/homeassistant/ha_discovery.py +++ b/myFox2Mqtt/homeassistant/ha_discovery.py @@ -1,4 +1,5 @@ """HomeAssistant MQTT Auto Discover""" + import logging from myfox.api.model import Site, Device @@ -667,9 +668,9 @@ def ha_discovery_alarm(site: Site, mqtt_config: dict, homeassistant_config: dict } command_topic = f"{mqtt_config.get('topic_prefix', 'myFox2mqtt')}/{site.siteId}/command" - site_config[ - "topic" - ] = f"{mqtt_config.get('ha_discover_prefix', 'homeassistant')}/alarm_control_panel/{site.siteId}/alarm/config" + site_config["topic"] = ( + f"{mqtt_config.get('ha_discover_prefix', 'homeassistant')}/alarm_control_panel/{site.siteId}/alarm/config" + ) site_config["config"] = { "name": site.label, "unique_id": f"{site.siteId}_{site.label}", @@ -757,9 +758,9 @@ def ha_discovery_scenario_actions(site: Site, scenario: dict, mqtt_config: dict) command_topic = ( f"{mqtt_config.get('topic_prefix', 'myFox2mqtt')}/{site.siteId}/{scenario.get('scenarioId')}/command" ) - site_config[ - "topic" - ] = f"{mqtt_config.get('ha_discover_prefix', 'homeassistant')}/button/{site.siteId}/{scenario.get('scenarioId')}/config" + site_config["topic"] = ( + f"{mqtt_config.get('ha_discover_prefix', 'homeassistant')}/button/{site.siteId}/{scenario.get('scenarioId')}/config" + ) site_config["config"] = { "name": scenario.get("label"), "unique_id": f"{site.siteId}_{scenario.get('label')}", @@ -792,9 +793,9 @@ def ha_discovery_devices( command_topic = ( f"{mqtt_config.get('topic_prefix', 'myFox2mqtt')}/{site_id}/{device.device_id}/{sensor_name}/command" ) - device_config[ - "topic" - ] = f"{mqtt_config.get('ha_discover_prefix', 'homeassistant')}/{device_type}/{site_id}_{device.device_id}/{sensor_name}/config" + device_config["topic"] = ( + f"{mqtt_config.get('ha_discover_prefix', 'homeassistant')}/{device_type}/{site_id}_{device.device_id}/{sensor_name}/config" + ) device_config["config"] = { "name": sensor_name, "unique_id": f"{device.device_id}_{sensor_name}", @@ -848,9 +849,9 @@ def ha_discovery_cameras( "sw_version": "Unknown", } - camera_config[ - "topic" - ] = f"{mqtt_config.get('ha_discover_prefix', 'homeassistant')}/camera/{site_id}_{device.device_id}/snapshot/config" + camera_config["topic"] = ( + f"{mqtt_config.get('ha_discover_prefix', 'homeassistant')}/camera/{site_id}_{device.device_id}/snapshot/config" + ) camera_config["config"] = { "name": "snapshot", "unique_id": f"{device.device_id}_snapshot", diff --git a/myFox2Mqtt/mqtt/__init__.py b/myFox2Mqtt/mqtt/__init__.py index 43345d7..69da75d 100644 --- a/myFox2Mqtt/mqtt/__init__.py +++ b/myFox2Mqtt/mqtt/__init__.py @@ -1,11 +1,12 @@ """MQTT""" + import json import logging import ssl from time import sleep import paho.mqtt.client as mqtt -from business.mqtt import consume_mqtt_message +from business.mqtt import consume_mqtt_message, SUBSCRIBE_TOPICS from exceptions import MyFoxInitError from homeassistant.ha_discovery import ALARM_STATUS from myfox.api import MyFoxApi @@ -39,7 +40,13 @@ def __init__(self, config, api, publish_delay=1): def on_connect(self, mqttc, obj, flags, rc): # pylint: disable=unused-argument,invalid-name """MQTT on_connect""" - LOGGER.debug(f"Connected: {rc}") + if rc == 0: + LOGGER.info(f"Connected: {rc}") + for topic in SUBSCRIBE_TOPICS: + LOGGER.info(f"Subscribing to: {topic}") + self.client.subscribe(topic) + else: + LOGGER.info(f"Not Connected: {rc}") def on_message(self, mqttc, obj, msg): # pylint: disable=unused-argument """MQTT on_message""" diff --git a/myFox2Mqtt/myfox/api/__init__.py b/myFox2Mqtt/myfox/api/__init__.py index 6740439..ff68613 100644 --- a/myFox2Mqtt/myfox/api/__init__.py +++ b/myFox2Mqtt/myfox/api/__init__.py @@ -1,4 +1,5 @@ """MyFox Api""" + import logging import json from json import JSONDecodeError diff --git a/myFox2Mqtt/myfox/api/devices/base.py b/myFox2Mqtt/myfox/api/devices/base.py index a695a62..35f2e85 100644 --- a/myFox2Mqtt/myfox/api/devices/base.py +++ b/myFox2Mqtt/myfox/api/devices/base.py @@ -1,4 +1,5 @@ """Device Managment""" + from typing import Union from myfox.api.model import Device, Site diff --git a/myFox2Mqtt/myfox/api/devices/category.py b/myFox2Mqtt/myfox/api/devices/category.py index aae5cbc..7be7298 100644 --- a/myFox2Mqtt/myfox/api/devices/category.py +++ b/myFox2Mqtt/myfox/api/devices/category.py @@ -1,4 +1,5 @@ """Devices Categories""" + from aenum import Enum, unique diff --git a/myFox2Mqtt/myfox/sso/__init__.py b/myFox2Mqtt/myfox/sso/__init__.py index cbb890b..5352eed 100644 --- a/myFox2Mqtt/myfox/sso/__init__.py +++ b/myFox2Mqtt/myfox/sso/__init__.py @@ -1,4 +1,5 @@ """MyFox Sso""" + import base64 import json import logging diff --git a/myFox2Mqtt/utils/__init__.py b/myFox2Mqtt/utils/__init__.py index eae68ef..718cd83 100644 --- a/myFox2Mqtt/utils/__init__.py +++ b/myFox2Mqtt/utils/__init__.py @@ -1,4 +1,5 @@ """Utils package""" + import codecs import logging import logging.handlers @@ -32,7 +33,6 @@ def setup_logger(filename: str, debug: bool = False) -> None: ) - def read_config_file(config_file: str) -> Dict[str, Any]: """Read config file