From 81d48a7c472d9891fa115156c31f02fa98aad33e Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Mon, 1 Apr 2024 00:22:30 -0400 Subject: [PATCH] Retry syncing lock after failure --- .../lock_code_manager/binary_sensor.py | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/custom_components/lock_code_manager/binary_sensor.py b/custom_components/lock_code_manager/binary_sensor.py index c4c098d7..4cda5c40 100644 --- a/custom_components/lock_code_manager/binary_sensor.py +++ b/custom_components/lock_code_manager/binary_sensor.py @@ -1,9 +1,9 @@ -"""Sensor for lock_code_manager.""" +"""Binary sensor for lock_code_manager.""" from __future__ import annotations import asyncio -from datetime import datetime +from datetime import datetime, timedelta import logging from homeassistant.components.binary_sensor import ( @@ -46,6 +46,7 @@ from .providers import BaseLock _LOGGER = logging.getLogger(__name__) +SCAN_INTERVAL = timedelta(seconds=30) async def async_setup_entry( @@ -258,13 +259,14 @@ def __init__( key_name = ATTR_IN_SYNC.replace("_", " ") self._attr_name: str | None = f"{super()._attr_name} {key_name}" + self._attr_should_poll = True @property def icon(self) -> str | None: """Return icon.""" if self.is_on: return "mdi:sync" - return "mdi:sync-" + return "mdi:sync-off" @property def available(self) -> bool: @@ -273,6 +275,22 @@ def available(self) -> bool: int(self.slot_num) in self.coordinator.data ) + async def async_update(self) -> None: + """Update entity.""" + if ( + self._lock + and not self._lock.locked() + and self.is_on is False + and (state := self.hass.states.get(self.lock.lock.entity_id)) + and state.state not in (STATE_UNAVAILABLE, STATE_UNKNOWN) + ): + _LOGGER.error( + "Updating %s code slot %s because it is out of sync", + self.lock.lock.entity_id, + self.slot_num, + ) + await self._update_state() + def _get_entity_state(self, key: str) -> str | None: """Get entity state.""" if (state := self.hass.states.get(self._entity_id_map[key])) is None: