From b54dc0eef20e0c284a291c1752cce66947188768 Mon Sep 17 00:00:00 2001 From: jbouwh Date: Tue, 10 May 2022 07:30:36 +0000 Subject: [PATCH] stabilize device updates v0.1.1 --- custom_components/elro_connects/device.py | 27 +++++++++++++++---- custom_components/elro_connects/manifest.json | 2 +- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/custom_components/elro_connects/device.py b/custom_components/elro_connects/device.py index 2712540..833907a 100644 --- a/custom_components/elro_connects/device.py +++ b/custom_components/elro_connects/device.py @@ -29,6 +29,8 @@ _LOGGER = logging.getLogger(__name__) +MAX_RETRIES = 3 + DEVICE_MODELS = { ALARM_CO: "CO alarm", ALARM_FIRE: "Fire alarm", @@ -50,6 +52,9 @@ def __init__( self._coordinator = coordinator self._data: dict[int, dict] = {} self._connector_id = entry.data[CONF_CONNECTOR_ID] + self._retry_count = 0 + self._connected = False + K1.__init__( self, entry.data[CONF_HOST], @@ -59,11 +64,23 @@ def __init__( async def async_update(self) -> None: """Synchronize with the K1 connector.""" - await self.async_connect() - update_status = await self.async_process_command(GET_ALL_EQUIPMENT_STATUS) - self._data = update_status - update_names = await self.async_process_command(GET_DEVICE_NAMES) - update_state_data(self._data, update_names) + new_data: dict[int, dict] = {} + try: + self._retry_count += 1 + # Only connect the first time or if there were recent issues + if not self._connected: + await self.async_connect() + self._connected = True + update_status = await self.async_process_command(GET_ALL_EQUIPMENT_STATUS) + new_data = update_status + update_names = await self.async_process_command(GET_DEVICE_NAMES) + update_state_data(new_data, update_names) + self._retry_count = 0 + self._data = new_data + except K1.K1ConnectionError as err: + self._connected = False + if not self._data or self._retry_count >= MAX_RETRIES: + raise K1.K1ConnectionError(err) from err async def async_update_settings( self, hass: HomeAssistant, entry: ConfigEntry diff --git a/custom_components/elro_connects/manifest.json b/custom_components/elro_connects/manifest.json index bbc852a..2e6e44d 100644 --- a/custom_components/elro_connects/manifest.json +++ b/custom_components/elro_connects/manifest.json @@ -6,5 +6,5 @@ "requirements": ["lib-elro-connects==0.4.1"], "codeowners": ["@jbouwh"], "iot_class": "local_polling", - "version": "0.1.0" + "version": "0.1.1" }