Skip to content

Commit

Permalink
stabilize device updates v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh committed May 10, 2022
1 parent f4ec4db commit b54dc0e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
27 changes: 22 additions & 5 deletions custom_components/elro_connects/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

_LOGGER = logging.getLogger(__name__)

MAX_RETRIES = 3

DEVICE_MODELS = {
ALARM_CO: "CO alarm",
ALARM_FIRE: "Fire alarm",
Expand All @@ -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],
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion custom_components/elro_connects/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit b54dc0e

Please sign in to comment.