diff --git a/custom_components/ultraloq_ble/__init__.py b/custom_components/ultraloq_ble/__init__.py index b3ca997..f79e264 100644 --- a/custom_components/ultraloq_ble/__init__.py +++ b/custom_components/ultraloq_ble/__init__.py @@ -9,8 +9,6 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import ( - CONF_ZONE_METHOD, - DEFAULT_ZONE_METHOD, DOMAIN, LOGGER, PLATFORMS, diff --git a/custom_components/ultraloq_ble/const.py b/custom_components/ultraloq_ble/const.py index 02da5fb..397d453 100644 --- a/custom_components/ultraloq_ble/const.py +++ b/custom_components/ultraloq_ble/const.py @@ -9,7 +9,7 @@ LOGGER = logging.getLogger(__package__) -DEFAULT_SCAN_INTERVAL = 180 +DEFAULT_SCAN_INTERVAL = 300 DOMAIN = "ultraloq_ble" PLATFORMS = [Platform.LOCK] diff --git a/custom_components/ultraloq_ble/lock.py b/custom_components/ultraloq_ble/lock.py index 5cb95a0..51fc224 100644 --- a/custom_components/ultraloq_ble/lock.py +++ b/custom_components/ultraloq_ble/lock.py @@ -6,20 +6,19 @@ from bleak.backends.device import BLEDevice from utecio.lock import UtecBleLock -from utecio.enums import ULLockStatus -from .const import DEFAULT_SCAN_INTERVAL from homeassistant.components import bluetooth -from homeassistant.components.lock import LockEntity, LockEntityFeature +from homeassistant.components.lock import LockEntity from homeassistant.config_entries import ConfigEntry -from homeassistant.const import STATE_LOCKED, STATE_LOCKING, STATE_UNLOCKED +from homeassistant.const import ( + CONF_SCAN_INTERVAL, +) from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.event import async_call_later -from .const import DOMAIN, UTEC_LOCKDATA - -SCAN_INTERVAL = timedelta(seconds=DEFAULT_SCAN_INTERVAL) +from .const import DOMAIN, UTEC_LOCKDATA, DEFAULT_SCAN_INTERVAL, LOGGER async def async_setup_entry( @@ -28,28 +27,41 @@ async def async_setup_entry( """Set Up Ultraloq Lock Entities.""" data: list[UtecBleLock] = hass.data[DOMAIN][entry.entry_id][UTEC_LOCKDATA] - + scan_interval = timedelta( + seconds=entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) + ) entities = [] for lock in data: - add = UtecLock(lock) + add = UtecLock(hass, lock, scan_interval=scan_interval) entities.append(add) - async_add_entities(entities) + async_add_entities(new_entities=entities) class UtecLock(LockEntity): """Representation of Ultraloq Device.""" - def __init__(self, lock: UtecBleLock) -> None: + def __init__( + self, hass: HomeAssistant, lock: UtecBleLock, scan_interval: timedelta + ) -> None: """Initialize the Lock.""" + super().__init__() self.lock: UtecBleLock = lock self._attr_is_locked = True self.lock.async_device_callback = self.async_device_callback + self.scaninterval = scan_interval + self.update_track = None + + @property + def should_poll(self) -> bool: + return False async def async_device_callback(self, device: str) -> BLEDevice | Any: """Return BLEDevice from HA bleak instance if available.""" - ble_device = bluetooth.async_ble_device_from_address(self.hass, device) + ble_device = bluetooth.async_ble_device_from_address( + self.hass, device, connectable=True + ) return ble_device if ble_device else device # @property @@ -70,19 +82,48 @@ def name(self) -> str: return self.lock.name + async def async_added_to_hass(self): + self.update_track = async_call_later( + self.hass, timedelta(seconds=2), lambda Now: self.request_update() + ) + + async def async_will_remove_from_hass(self): + if self.update_track: + self.update_track() + + def request_update(self): + if self.update_track: + self.update_track() + + if self.enabled and self.hass and not self._update_staged: + self.schedule_update_ha_state(force_refresh=True) + self.update_track = async_call_later( + self.hass, self.scaninterval, lambda Now: self.request_update() + ) + async def async_update(self, **kwargs): """Update the lock.""" + LOGGER.debug("Updating %s with scan interval: %s", self.name, self.scaninterval) await self.lock.update() - self._attr_is_locked = self.lock.lock_status = ULLockStatus.LOCKED async def async_lock(self, **kwargs): """Lock the lock.""" self._attr_is_locked = True - self.schedule_update_ha_state(force_refresh=False) await self.lock.lock() + self.schedule_update_ha_state(force_refresh=False) async def async_unlock(self, **kwargs): """Unlock the lock.""" self._attr_is_locked = False - self.schedule_update_ha_state(force_refresh=False) await self.lock.unlock() + self.schedule_update_ha_state(force_refresh=False) + async_call_later( + self.hass, + timedelta(seconds=self.lock.autolock_time), + lambda Now: self._set_state_locked(), + ) + + def _set_state_locked(self): + LOGGER.debug("Autolock %s", self.name) + self._attr_is_locked = True + self.schedule_update_ha_state(force_refresh=False) diff --git a/custom_components/ultraloq_ble/sensor.py b/custom_components/ultraloq_ble/sensor.py new file mode 100644 index 0000000..e69de29 diff --git a/custom_components/ultraloq_ble/strings.json b/custom_components/ultraloq_ble/strings.json index e8b1727..d6257db 100644 --- a/custom_components/ultraloq_ble/strings.json +++ b/custom_components/ultraloq_ble/strings.json @@ -32,7 +32,7 @@ "step": { "init": { "data": { - "scan_interval": "Scan Interval (seconds):" + "scan_interval": "Poll Interval (seconds):" } } }