@@ -128,6 +128,7 @@ async def async_setup(self, tries: int = 0) -> bool:
128128 self .config_entry .data .get (HMIPC_AUTHTOKEN ),
129129 self .config_entry .data .get (HMIPC_NAME ),
130130 )
131+
131132 except HmipcConnectionError as err :
132133 raise ConfigEntryNotReady from err
133134 except Exception as err : # noqa: BLE001
@@ -210,41 +211,13 @@ def update_all(self) -> None:
210211 for device in self .home .devices :
211212 device .fire_update_event ()
212213
213- async def async_connect (self ) -> None :
214- """Start WebSocket connection."""
215- tries = 0
216- while True :
217- retry_delay = 2 ** min (tries , 8 )
218-
219- try :
220- await self .home .get_current_state_async ()
221- hmip_events = self .home .enable_events ()
222- self .home .set_on_connected_handler (self .ws_connected_handler )
223- self .home .set_on_disconnected_handler (self .ws_disconnected_handler )
224- tries = 0
225- await hmip_events
226- except HmipConnectionError :
227- _LOGGER .error (
228- (
229- "Error connecting to HomematicIP with HAP %s. "
230- "Retrying in %d seconds"
231- ),
232- self .config_entry .unique_id ,
233- retry_delay ,
234- )
235-
236- if self ._ws_close_requested :
237- break
238- self ._ws_close_requested = False
239- tries += 1
240-
241- try :
242- self ._retry_task = self .hass .async_create_task (
243- asyncio .sleep (retry_delay )
244- )
245- await self ._retry_task
246- except asyncio .CancelledError :
247- break
214+ async def async_connect (self , home : AsyncHome ) -> None :
215+ """Connect to HomematicIP Cloud Websocket."""
216+ await home .enable_events ()
217+
218+ home .set_on_connected_handler (self .ws_connected_handler )
219+ home .set_on_disconnected_handler (self .ws_disconnected_handler )
220+ home .set_on_reconnect_handler (self .ws_reconnected_handler )
248221
249222 async def async_reset (self ) -> bool :
250223 """Close the websocket connection."""
@@ -272,14 +245,22 @@ def shutdown(self, event) -> None:
272245
273246 async def ws_connected_handler (self ) -> None :
274247 """Handle websocket connected."""
275- _LOGGER .debug ( "WebSocket connection to HomematicIP established" )
248+ _LOGGER .info ( "Websocket connection to HomematicIP Cloud established" )
276249 if self ._ws_connection_closed .is_set ():
277250 await self .get_state ()
278251 self ._ws_connection_closed .clear ()
279252
280253 async def ws_disconnected_handler (self ) -> None :
281254 """Handle websocket disconnection."""
282- _LOGGER .warning ("WebSocket connection to HomematicIP closed" )
255+ _LOGGER .warning ("Websocket connection to HomematicIP Cloud closed" )
256+ self ._ws_connection_closed .set ()
257+
258+ async def ws_reconnected_handler (self , reason : str ) -> None :
259+ """Handle websocket reconnection."""
260+ _LOGGER .info (
261+ "Websocket connection to HomematicIP Cloud re-established due to reason: %s" ,
262+ reason ,
263+ )
283264 self ._ws_connection_closed .set ()
284265
285266 async def get_hap (
@@ -306,6 +287,6 @@ async def get_hap(
306287 home .on_update (self .async_update )
307288 home .on_create (self .async_create_entity )
308289
309- hass . loop . create_task ( self .async_connect () )
290+ await self .async_connect (home )
310291
311292 return home
0 commit comments