Avoid Shelly RPC reconnect during device shutdown (#117702)

This commit is contained in:
Shay Levy 2024-05-19 02:05:51 +03:00 committed by GitHub
parent c59010c499
commit bfc52b9fab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -362,6 +362,7 @@ class ShellyBlockCoordinator(ShellyCoordinatorBase[BlockDevice]):
self, device_: BlockDevice, update_type: BlockUpdateType self, device_: BlockDevice, update_type: BlockUpdateType
) -> None: ) -> None:
"""Handle device update.""" """Handle device update."""
LOGGER.debug("Shelly %s handle update, type: %s", self.name, update_type)
if update_type is BlockUpdateType.ONLINE: if update_type is BlockUpdateType.ONLINE:
self.entry.async_create_background_task( self.entry.async_create_background_task(
self.hass, self.hass,
@ -596,7 +597,7 @@ class ShellyRpcCoordinator(ShellyCoordinatorBase[RpcDevice]):
if not await self._async_device_connect_task(): if not await self._async_device_connect_task():
raise UpdateFailed("Device reconnect error") raise UpdateFailed("Device reconnect error")
async def _async_disconnected(self) -> None: async def _async_disconnected(self, reconnect: bool) -> None:
"""Handle device disconnected.""" """Handle device disconnected."""
# Sleeping devices send data and disconnect # Sleeping devices send data and disconnect
# There are no disconnect events for sleeping devices # There are no disconnect events for sleeping devices
@ -608,8 +609,8 @@ class ShellyRpcCoordinator(ShellyCoordinatorBase[RpcDevice]):
return return
self.connected = False self.connected = False
self._async_run_disconnected_events() self._async_run_disconnected_events()
# Try to reconnect right away if hass is not stopping # Try to reconnect right away if triggered by disconnect event
if not self.hass.is_stopping: if reconnect:
await self.async_request_refresh() await self.async_request_refresh()
@callback @callback
@ -661,6 +662,7 @@ class ShellyRpcCoordinator(ShellyCoordinatorBase[RpcDevice]):
self, device_: RpcDevice, update_type: RpcUpdateType self, device_: RpcDevice, update_type: RpcUpdateType
) -> None: ) -> None:
"""Handle device update.""" """Handle device update."""
LOGGER.debug("Shelly %s handle update, type: %s", self.name, update_type)
if update_type is RpcUpdateType.ONLINE: if update_type is RpcUpdateType.ONLINE:
self.entry.async_create_background_task( self.entry.async_create_background_task(
self.hass, self.hass,
@ -676,7 +678,7 @@ class ShellyRpcCoordinator(ShellyCoordinatorBase[RpcDevice]):
elif update_type is RpcUpdateType.DISCONNECTED: elif update_type is RpcUpdateType.DISCONNECTED:
self.entry.async_create_background_task( self.entry.async_create_background_task(
self.hass, self.hass,
self._async_disconnected(), self._async_disconnected(True),
"rpc device disconnected", "rpc device disconnected",
eager_start=True, eager_start=True,
) )
@ -706,7 +708,7 @@ class ShellyRpcCoordinator(ShellyCoordinatorBase[RpcDevice]):
await self.async_shutdown_device_and_start_reauth() await self.async_shutdown_device_and_start_reauth()
return return
await self.device.shutdown() await self.device.shutdown()
await self._async_disconnected() await self._async_disconnected(False)
async def _handle_ha_stop(self, _event: Event) -> None: async def _handle_ha_stop(self, _event: Event) -> None:
"""Handle Home Assistant stopping.""" """Handle Home Assistant stopping."""