@@ -71,15 +71,18 @@ def __init__(self, hass, config) -> None:
7171 self ._shutdown = False
7272 self ._fails = 0
7373
74+ def connected (self ):
75+ return self .device_client is not None
76+
7477 def passcode (self ):
7578 if self .device_client is not None :
7679 return self .device_client .passcode
77- return "unknown"
80+ return None
7881
7982 def unique_name (self ):
8083 if self .device_client is not None :
8184 return self .device_client .get_unique_name ()
82- return "unknown"
85+ return None
8386
8487 def measurement (self ):
8588 if self .device_client is not None :
@@ -112,19 +115,27 @@ def shutdown(event):
112115 # least every 4 seconds the device side closes the
113116 # connection.
114117 while True :
118+ self .device_client = None
119+
115120 _LOGGER .info (f"Attempting to connect to device at { self .host } " )
116- self .device_client = device .Device (self .host )
121+ device_client = device .Device (self .host )
122+
117123 try :
118- if not self .device_client .run (once = True ):
119- _LOGGER .info (f"Device found but no measurement was received, reconnecting in { ERROR_RECONNECT_INTERVAL } seconds" )
124+ if not device_client .run (once = True ):
125+ _LOGGER .info (
126+ f"Device found but no measurement was received, reconnecting in { ERROR_RECONNECT_INTERVAL } seconds" )
120127 time .sleep (ERROR_RECONNECT_INTERVAL )
121128 continue
129+
122130 except Exception as e :
123- _LOGGER .info (f"Error connecting to device at { self .host } : { str (e )} " )
124- _LOGGER .info (f"Retrying connection in { ERROR_RECONNECT_INTERVAL } seconds" )
131+ _LOGGER .info (
132+ f"Error connecting to device at { self .host } : { str (e )} " )
133+ _LOGGER .info (
134+ f"Retrying connection in { ERROR_RECONNECT_INTERVAL } seconds" )
125135 time .sleep (ERROR_RECONNECT_INTERVAL )
126136 continue
127137
138+ self .device_client = device_client
128139 _LOGGER .debug ("Registering callbacks" )
129140 self .device_client .register_callback (self .dispatcher_new_data )
130141 self .device_client .register_callback (self .reset_fail_counter )
@@ -145,7 +156,8 @@ def shutdown(event):
145156 if error_mapping >= len (ERROR_ITERVAL_MAPPING ):
146157 error_mapping = len (ERROR_ITERVAL_MAPPING ) - 1
147158 sleep_time = ERROR_ITERVAL_MAPPING [error_mapping ]
148- _LOGGER .info (f"Sleeping { str (sleep_time )} s for failure #{ str (self ._fails )} " )
159+ _LOGGER .info (
160+ f"Sleeping { str (sleep_time )} s for failure #{ str (self ._fails )} " )
149161 self .device_client .reset_socket ()
150162 time .sleep (sleep_time )
151163
0 commit comments