Skip to content

Commit

Permalink
fix: Re-subscribe to all topic on MQTT reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Minims committed Sep 25, 2024
1 parent 53dd9bc commit 239197f
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"python.formatting.provider": "none",
"black-formatter.args": [
"-l 120"
]
],
"files.associations": {
"*.yaml": "home-assistant"
}
}
12 changes: 11 additions & 1 deletion myFox2Mqtt/business/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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"):
Expand Down
2 changes: 2 additions & 0 deletions myFox2Mqtt/business/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" MQTT Business"""

import json
import logging
from time import sleep
Expand All @@ -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):
Expand Down
25 changes: 13 additions & 12 deletions myFox2Mqtt/homeassistant/ha_discovery.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""HomeAssistant MQTT Auto Discover"""

import logging
from myfox.api.model import Site, Device

Expand Down Expand Up @@ -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}",
Expand Down Expand Up @@ -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')}",
Expand Down Expand Up @@ -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}",
Expand Down Expand Up @@ -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",
Expand Down
11 changes: 9 additions & 2 deletions myFox2Mqtt/mqtt/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"""
Expand Down
1 change: 1 addition & 0 deletions myFox2Mqtt/myfox/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""MyFox Api"""

import logging
import json
from json import JSONDecodeError
Expand Down
1 change: 1 addition & 0 deletions myFox2Mqtt/myfox/api/devices/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Device Managment"""

from typing import Union

from myfox.api.model import Device, Site
Expand Down
1 change: 1 addition & 0 deletions myFox2Mqtt/myfox/api/devices/category.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Devices Categories"""

from aenum import Enum, unique


Expand Down
1 change: 1 addition & 0 deletions myFox2Mqtt/myfox/sso/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""MyFox Sso"""

import base64
import json
import logging
Expand Down
2 changes: 1 addition & 1 deletion myFox2Mqtt/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utils package"""

import codecs
import logging
import logging.handlers
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 239197f

Please sign in to comment.