@@ -18,7 +18,6 @@ class EcoflowMQTTClient:
1818 def __init__ (self , mqtt_info : EcoflowMqttInfo , devices : dict [str , BaseDevice ]):
1919
2020 from ..devices import BaseDevice
21- # Status pour ne pas boucler
2221 self .__autorise = True
2322 self .__mqtt_info = mqtt_info
2423 self .__error_count = 0
@@ -30,7 +29,7 @@ def __init__(self, mqtt_info: EcoflowMqttInfo, devices: dict[str, BaseDevice]):
3029 self .__client .on_connect = self .on_connect
3130 self .__client .on_disconnect = self .on_disconnect
3231 self .__client .on_message = self .on_message
33- self .__client .on_socket_close = self .on_socket_close
32+ # self.__client.on_socket_close = self.on_socket_close
3433
3534 _LOGGER .info (f"Connecting to MQTT Broker { self .__mqtt_info .url } :{ self .__mqtt_info .port } with client id { self .__mqtt_info .client_id } and username { self .__mqtt_info .username } " )
3635 self .__client .connect (self .__mqtt_info .url , self .__mqtt_info .port , 30 )
@@ -65,15 +64,8 @@ def on_connect(self, client, userdata, flags, rc):
6564 topics = []
6665 for (sn , device ) in self .__devices .items ():
6766 _LOGGER .debug (f"Add Topics for { sn } " )
68-
69- if device .device_info .data_topic :
70- topics .append ((device .device_info .data_topic , 1 ))
71- if device .device_info .set_topic :
72- topics .append ((device .device_info .set_topic , 1 ))
73- if device .device_info .set_reply_topic :
74- topics .append ((device .device_info .set_reply_topic , 1 ))
75- if device .device_info .status_topic :
76- topics .append ((device .device_info .status_topic , 1 ))
67+ for topic in device .device_info .topics ():
68+ topics .append ((topic , 1 ))
7769
7870 self .__client .subscribe (topics )
7971 _LOGGER .info (f"Subscribed to MQTT topics { topics } " )
@@ -101,9 +93,9 @@ def on_connect(self, client, userdata, flags, rc):
10193 self .stop ()
10294
10395 return client
104-
105- def on_socket_close (self , client , userdata , socket ):
106- _LOGGER .error (f"Unexpected MQTT Socket disconnection : { str (socket )} " )
96+ #
97+ # def on_socket_close(self, client, userdata, socket):
98+ # _LOGGER.error(f"Unexpected MQTT Socket disconnection : {str(socket)}")
10799
108100 def on_disconnect (self , client , userdata , rc ):
109101 if rc != 0 :
@@ -115,21 +107,9 @@ def on_disconnect(self, client, userdata, rc):
115107
116108 def on_message (self , client , userdata , message ):
117109 try :
118- message_sn = None
119110 for (sn , device ) in self .__devices .items ():
120- TOPICS = [device .device_info .data_topic , device .device_info .set_topic , device .device_info .set_reply_topic ,
121- device .device_info .get_topic , device .device_info .get_reply_topic , device .device_info .status_topic ]
122- match message .topic :
123- case item if item in TOPICS :
124- message_sn = sn
125- break
126- case _:
127- pass
128- if message_sn :
129- _LOGGER .debug (f"Message for { message_sn } and Topic { message .payload } " )
130- self .__devices [message_sn ].update_data (message .payload , message .topic )
131- else :
132- _LOGGER .warn (f"No device for Topic { message .topic } " )
111+ if device .update_data (message .payload , message .topic ):
112+ _LOGGER .debug (f"Message for { sn } and Topic { message .topic } " )
133113 except UnicodeDecodeError as error :
134114 _LOGGER .error (f"UnicodeDecodeError: { error } . Ignoring message and waiting for the next one." )
135115
0 commit comments