Skip to content

Commit

Permalink
Autolock + utecio v+
Browse files Browse the repository at this point in the history
  • Loading branch information
maeneak committed Jan 23, 2024
1 parent 14de2f7 commit d827706
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
2 changes: 0 additions & 2 deletions custom_components/ultraloq_ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ultraloq_ble/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

LOGGER = logging.getLogger(__package__)

DEFAULT_SCAN_INTERVAL = 180
DEFAULT_SCAN_INTERVAL = 300
DOMAIN = "ultraloq_ble"
PLATFORMS = [Platform.LOCK]

Expand Down
71 changes: 56 additions & 15 deletions custom_components/ultraloq_ble/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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)
Empty file.
2 changes: 1 addition & 1 deletion custom_components/ultraloq_ble/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"step": {
"init": {
"data": {
"scan_interval": "Scan Interval (seconds):"
"scan_interval": "Poll Interval (seconds):"
}
}
}
Expand Down

0 comments on commit d827706

Please sign in to comment.