Skip to content

Commit

Permalink
Fix mqtt subscribe race-condition
Browse files Browse the repository at this point in the history
  • Loading branch information
kbialek committed Dec 22, 2023
1 parent 3facf4c commit 1b89b69
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/deye_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ def __init__(self, config: DeyeConfig):
set_time_processor = DeyeSetTimeProcessor(self.modbus)
time_of_use_service = DeyeTimeOfUseService(config, mqtt_client, self.sensors, self.modbus)

command_handlers = [
DeyeActivePowerRegulationCommandHandler(config, mqtt_client, self.modbus),
time_of_use_service,
]
DeyeMqttSubscriber(config, mqtt_client, command_handlers)

all_processors = [mqtt_publisher, set_time_processor, time_of_use_service]
self.processors = [
p for p in all_processors if p.get_id() in config.active_processors
] + plugin_loader.get_event_processors()
for p in self.processors:
p.initialize()

command_handlers = [
DeyeActivePowerRegulationCommandHandler(config, mqtt_client, self.modbus),
time_of_use_service,
]
DeyeMqttSubscriber(config, mqtt_client, command_handlers)

self.__log.info(
'Feature "Report metrics over MQTT": {}'.format(
"enabled" if mqtt_publisher.get_id() in config.active_processors else "disabled"
Expand Down
2 changes: 1 addition & 1 deletion src/deye_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DeyeMqttClient:
def __init__(self, config: DeyeConfig):
self.__log = logging.getLogger(DeyeMqttClient.__name__)
self.__mqtt_client = paho.Client(
client_id=f"deye-inverter-{config.logger.serial_number}", reconnect_on_failure=True
client_id=f"deye-inverter-{config.logger.serial_number}", reconnect_on_failure=True, clean_session=True
)
self.__mqtt_client.enable_logger()
if config.mqtt.tls.enabled:
Expand Down
2 changes: 2 additions & 0 deletions src/deye_timeofuse_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def get_id(self):
return "time_of_use"

def initialize(self):
if self.__sensor_map:
return
for sensor in self.__sensors:
self._subscribe(sensor.mqtt_topic_suffix, self.handle_command)
self.__sensor_map[sensor.mqtt_topic_suffix] = sensor
Expand Down

0 comments on commit 1b89b69

Please sign in to comment.