From 3200b0092788e87d85defc9512b71a27222a6d05 Mon Sep 17 00:00:00 2001 From: Vyacheslav Scherbinin Date: Sun, 24 Dec 2023 12:01:58 +0700 Subject: [PATCH] Added "jammed" status for bad responses (eg 500) --- custom_components/elektronny_gorod/lock.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/custom_components/elektronny_gorod/lock.py b/custom_components/elektronny_gorod/lock.py index fef01d1..348443e 100644 --- a/custom_components/elektronny_gorod/lock.py +++ b/custom_components/elektronny_gorod/lock.py @@ -1,5 +1,6 @@ import asyncio from typing import Any +from aiohttp import ClientError from homeassistant.components.lock import LockEntity from homeassistant.config_entries import ConfigEntry @@ -17,6 +18,7 @@ from .coordinator import ElektronnyGorogDataUpdateCoordinator LOCK_UNLOCK_DELAY = 5 # Used to give a realistic lock/unlock experience in frontend +LOCK_JAMMED_DELAY = 2 async def async_setup_entry( hass: HomeAssistant, @@ -99,18 +101,23 @@ async def async_unlock(self, **kwargs: Any) -> None: LOGGER.info(f"Unlock {self.unique_id}") self._state = STATE_UNLOCKING self.async_write_ha_state() - await self._coordinator.open_lock(self._place_id, self._access_control_id, self._entrance_id) - self._state = STATE_UNLOCKED + try: + await self._coordinator.open_lock(self._place_id, self._access_control_id, self._entrance_id) + self._state = STATE_UNLOCKED + except ClientError: + self._state = STATE_JAMMED self.async_write_ha_state() - async def fake_timer_lock(self) -> None: - await asyncio.sleep(LOCK_UNLOCK_DELAY) + async def fake_timer_lock(self, delay) -> None: + await asyncio.sleep(delay) self._state = STATE_LOCKED self.async_write_ha_state() async def async_update(self) -> None: """Update lock state.""" if self._state == STATE_UNLOCKED: - await self.fake_timer_lock() + await self.fake_timer_lock(LOCK_UNLOCK_DELAY) + if self._state == STATE_JAMMED: + await self.fake_timer_lock(LOCK_JAMMED_DELAY) # self._lock_info = await self._coordinator.update_lock_state(self._place_id, self._access_control_id, self._entrance_id) # self._openable = self._lock_info["openable"]