11"""Library specified for inels-mqtt."""
2- from collections import defaultdict
2+
3+ import copy
34import logging
45import time
56import uuid
6- import copy
7-
7+ from collections import defaultdict
88from datetime import datetime
99from typing import Any , Callable , Optional
1010
1111import paho .mqtt .client as mqtt
1212
1313from .const import (
14+ DEVICE_TYPE_DICT ,
15+ DISCOVERY_TIMEOUT_IN_SEC ,
16+ FRAGMENT_DEVICE_TYPE ,
17+ FRAGMENT_STATE ,
1418 MQTT_CLIENT_ID ,
1519 MQTT_HOST ,
1620 MQTT_PASSWORD ,
1721 MQTT_PORT ,
22+ MQTT_PROTOCOL ,
1823 MQTT_STATUS_TOPIC_PREFIX ,
1924 MQTT_TIMEOUT ,
2025 MQTT_TOTAL_CONNECTED_TOPIC ,
2126 MQTT_TOTAL_STATUS_TOPIC ,
2227 MQTT_TRANSPORT ,
23- MQTT_USERNAME ,
24- MQTT_PROTOCOL ,
2528 MQTT_TRANSPORTS ,
26- VERSION ,
27- DEVICE_TYPE_DICT ,
28- FRAGMENT_DEVICE_TYPE ,
29- FRAGMENT_STATE ,
29+ MQTT_USERNAME ,
3030 TOPIC_FRAGMENTS ,
31- DISCOVERY_TIMEOUT_IN_SEC ,
31+ VERSION ,
3232)
3333
3434__version__ = VERSION
@@ -56,13 +56,9 @@ def __init__(
5656 transport (str): transportation protocol. Can be used tcp or websockets, defaltut tcp
5757 debug (bool): flag for debuging mqtt comunication. Default False
5858 """
59- proto = (
60- config .get (MQTT_PROTOCOL ) if config .get (MQTT_PROTOCOL ) else mqtt .MQTTv311
61- )
59+ proto = config .get (MQTT_PROTOCOL ) if config .get (MQTT_PROTOCOL ) else mqtt .MQTTv311
6260
63- _t : str = (
64- config .get (MQTT_TRANSPORT ) if config .get (MQTT_TRANSPORT ) else "tcp"
65- ).lower ()
61+ _t : str = (config .get (MQTT_TRANSPORT ) if config .get (MQTT_TRANSPORT ) else "tcp" ).lower ()
6662
6763 if _t not in MQTT_TRANSPORTS :
6864 raise Exception
@@ -91,7 +87,7 @@ def __init__(
9187 _t = config .get (MQTT_TIMEOUT )
9288 self .__timeout = _t if _t is not None else __DISCOVERY_TIMEOUT__
9389
94- self .__listeners : dict [str , dict [str , Callable [[Any ], Any ]]] = defaultdict (lambda : dict ())
90+ self .__listeners : dict [str , dict [str , Callable [[Any ], Any ]]] = defaultdict (lambda : dict ())
9591 self .__is_subscribed_list = dict [str , bool ]()
9692 self .__last_values = dict [str , str ]()
9793 self .__try_connect = False
@@ -167,15 +163,15 @@ def test_connection(self) -> Optional[int]:
167163 self .disconnect ()
168164 except Exception as e :
169165 if isinstance (e , ConnectionRefusedError ):
170- self .__connection_error = 3 # cannot connect
166+ self .__connection_error = 3 # cannot connect
171167 else :
172- self .__connection_error = 6 # unknown
168+ self .__connection_error = 6 # unknown
173169
174170 return self .__connection_error
175171
176172 def subscribe_listener (self , topic : str , unique_id : str , fnc : Callable [[Any ], Any ]) -> None :
177173 """Append new item into the datachange listener."""
178- #if topic not in self.__listeners:
174+ # if topic not in self.__listeners:
179175 # self.__listeners[topic] = dict[str, Callable[[Any], Any]]()
180176
181177 stripped_topic = "/" .join (topic .split ("/" )[2 :])
@@ -422,7 +418,7 @@ def __on_discover(
422418 _LOGGER .info ("Device of type %s found [status].\n " , device_type )
423419 elif action == "connected" :
424420 if topic not in self .__discovered :
425- self .__discovered [topic ] = None # msg.payload
421+ self .__discovered [topic ] = None # msg.payload
426422 self .__last_values [msg .topic ] = msg .payload
427423 self .__is_subscribed_list [msg .topic ] = True
428424 _LOGGER .info ("Device of type %s found [connected].\n " , device_type )
@@ -457,13 +453,11 @@ def __on_message(
457453 if device_type in DEVICE_TYPE_DICT or device_type == "gw" :
458454 # keep last value
459455 self .__last_values [msg .topic ] = (
460- copy .copy (self .__messages [msg .topic ])
461- if msg .topic in self .__messages
462- else msg .payload
456+ copy .copy (self .__messages [msg .topic ]) if msg .topic in self .__messages else msg .payload
463457 )
464458 self .__messages [msg .topic ] = msg .payload
465459
466- if device_type == "gw" and message_type == "connected" :
460+ if device_type == "gw" and message_type == "connected" :
467461 mac = message_parts [2 ]
468462 for stripped_topic in self .__listeners :
469463 if stripped_topic .startswith (mac ):
@@ -481,7 +475,9 @@ def __on_message(
481475 def __notify_listeners (self , stripped_topic : str , is_connected_message : bool ) -> None :
482476 """Notify listeners for a specific topic."""
483477 if len (self .__listeners [stripped_topic ]) > 0 :
484- for unique_id in list (self .__listeners [stripped_topic ]): #prevents the dictionary increased in size during iteration exception
478+ for unique_id in list (
479+ self .__listeners [stripped_topic ]
480+ ): # prevents the dictionary increased in size during iteration exception
485481 self .__listeners [stripped_topic ][unique_id ](is_connected_message )
486482
487483 def __on_subscribe (
@@ -503,7 +499,7 @@ def __on_subscribe(
503499 properties (_type_, optional): Props from broker set.
504500 Defaults to None.
505501 """
506- #_LOGGER.info(mid)
502+ # _LOGGER.info(mid)
507503
508504 def __disconnect (self ) -> None :
509505 """Disconnecting from broker and stopping broker's loop"""
0 commit comments