2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Verisure locks."""
|
2021-03-05 23:37:56 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
import asyncio
|
2021-03-05 23:37:56 +00:00
|
|
|
from typing import Any, Callable
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-04-25 16:02:41 +00:00
|
|
|
from homeassistant.components.lock import LockEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.const import ATTR_CODE, STATE_LOCKED, STATE_UNLOCKED
|
2021-03-05 23:37:56 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-03-11 18:41:01 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
from .const import CONF_CODE_DIGITS, CONF_DEFAULT_LOCK_CODE, CONF_LOCKS, DOMAIN, LOGGER
|
2021-03-14 09:38:09 +00:00
|
|
|
from .coordinator import VerisureDataUpdateCoordinator
|
2016-01-27 15:38:43 +00:00
|
|
|
|
|
|
|
|
2021-03-05 23:37:56 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: dict[str, Any],
|
2021-03-14 09:38:09 +00:00
|
|
|
add_entities: Callable[[list[VerisureDoorlock]], None],
|
2021-03-05 23:37:56 +00:00
|
|
|
discovery_info: dict[str, Any] | None = None,
|
|
|
|
) -> None:
|
2019-02-13 20:21:14 +00:00
|
|
|
"""Set up the Verisure lock platform."""
|
2021-03-11 18:41:01 +00:00
|
|
|
coordinator = hass.data[DOMAIN]
|
2016-01-27 15:38:43 +00:00
|
|
|
locks = []
|
2021-03-11 18:41:01 +00:00
|
|
|
if int(coordinator.config.get(CONF_LOCKS, 1)):
|
2019-07-31 19:25:30 +00:00
|
|
|
locks.extend(
|
|
|
|
[
|
2021-03-14 09:38:09 +00:00
|
|
|
VerisureDoorlock(coordinator, serial_number)
|
|
|
|
for serial_number in coordinator.data["locks"]
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
|
|
|
)
|
2017-06-26 20:30:25 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(locks)
|
2016-01-27 15:38:43 +00:00
|
|
|
|
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
class VerisureDoorlock(CoordinatorEntity, LockEntity):
|
2016-03-07 21:13:18 +00:00
|
|
|
"""Representation of a Verisure doorlock."""
|
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
coordinator: VerisureDataUpdateCoordinator
|
|
|
|
|
|
|
|
def __init__(
|
2021-03-14 09:38:09 +00:00
|
|
|
self, coordinator: VerisureDataUpdateCoordinator, serial_number: str
|
2021-03-11 18:41:01 +00:00
|
|
|
) -> None:
|
2017-04-24 03:16:52 +00:00
|
|
|
"""Initialize the Verisure lock."""
|
2021-03-11 18:41:01 +00:00
|
|
|
super().__init__(coordinator)
|
2021-03-14 09:38:09 +00:00
|
|
|
self.serial_number = serial_number
|
2019-01-24 07:20:20 +00:00
|
|
|
self._state = None
|
2021-03-11 18:41:01 +00:00
|
|
|
self._digits = coordinator.config.get(CONF_CODE_DIGITS)
|
|
|
|
self._default_lock_code = coordinator.config.get(CONF_DEFAULT_LOCK_CODE)
|
2016-01-27 15:38:43 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def name(self) -> str:
|
2016-03-07 21:13:18 +00:00
|
|
|
"""Return the name of the lock."""
|
2021-03-14 09:38:09 +00:00
|
|
|
return self.coordinator.data["locks"][self.serial_number]["area"]
|
2016-01-27 15:38:43 +00:00
|
|
|
|
2016-05-04 01:53:11 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def available(self) -> bool:
|
2016-05-04 01:53:11 +00:00
|
|
|
"""Return True if entity is available."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return (
|
2021-03-14 09:38:09 +00:00
|
|
|
super().available and self.serial_number in self.coordinator.data["locks"]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-05-04 01:53:11 +00:00
|
|
|
|
2016-08-10 02:37:46 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def changed_by(self) -> str | None:
|
2016-08-10 02:37:46 +00:00
|
|
|
"""Last change triggered by."""
|
2021-03-14 09:38:09 +00:00
|
|
|
return self.coordinator.data["locks"][self.serial_number].get("userString")
|
2016-08-10 02:37:46 +00:00
|
|
|
|
2016-01-27 15:38:43 +00:00
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def code_format(self) -> str:
|
2016-03-07 21:13:18 +00:00
|
|
|
"""Return the required six digit code."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "^\\d{%s}$" % self._digits
|
2016-01-27 15:38:43 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-05 23:37:56 +00:00
|
|
|
def is_locked(self) -> bool:
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Return true if lock is locked."""
|
2021-03-14 09:38:09 +00:00
|
|
|
return (
|
|
|
|
self.coordinator.data["locks"][self.serial_number]["lockedState"]
|
|
|
|
== "LOCKED"
|
2021-03-11 18:41:01 +00:00
|
|
|
)
|
2016-01-27 15:38:43 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
async def async_unlock(self, **kwargs) -> None:
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Send unlock command."""
|
2018-12-03 06:25:54 +00:00
|
|
|
code = kwargs.get(ATTR_CODE, self._default_lock_code)
|
|
|
|
if code is None:
|
2020-12-30 08:55:18 +00:00
|
|
|
LOGGER.error("Code required but none provided")
|
2018-12-03 06:25:54 +00:00
|
|
|
return
|
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
await self.async_set_lock_state(code, STATE_UNLOCKED)
|
2016-01-27 15:38:43 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
async def async_lock(self, **kwargs) -> None:
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Send lock command."""
|
2018-12-03 06:25:54 +00:00
|
|
|
code = kwargs.get(ATTR_CODE, self._default_lock_code)
|
|
|
|
if code is None:
|
2020-12-30 08:55:18 +00:00
|
|
|
LOGGER.error("Code required but none provided")
|
2018-12-03 06:25:54 +00:00
|
|
|
return
|
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
await self.async_set_lock_state(code, STATE_LOCKED)
|
2017-06-26 20:30:25 +00:00
|
|
|
|
2021-03-11 18:41:01 +00:00
|
|
|
async def async_set_lock_state(self, code: str, state: str) -> None:
|
2017-06-26 20:30:25 +00:00
|
|
|
"""Send set lock state command."""
|
2021-03-11 18:41:01 +00:00
|
|
|
target_state = "lock" if state == STATE_LOCKED else "unlock"
|
|
|
|
lock_state = await self.hass.async_add_executor_job(
|
2021-03-14 09:38:09 +00:00
|
|
|
self.coordinator.verisure.set_lock_state,
|
2021-03-11 18:41:01 +00:00
|
|
|
code,
|
2021-03-14 09:38:09 +00:00
|
|
|
self.serial_number,
|
2021-03-11 18:41:01 +00:00
|
|
|
target_state,
|
|
|
|
)
|
|
|
|
|
2020-12-30 08:55:18 +00:00
|
|
|
LOGGER.debug("Verisure doorlock %s", state)
|
2017-06-26 20:30:25 +00:00
|
|
|
transaction = {}
|
2020-07-03 18:33:12 +00:00
|
|
|
attempts = 0
|
2019-07-31 19:25:30 +00:00
|
|
|
while "result" not in transaction:
|
2021-03-11 18:41:01 +00:00
|
|
|
transaction = await self.hass.async_add_executor_job(
|
2021-03-14 09:38:09 +00:00
|
|
|
self.coordinator.verisure.get_lock_state_transaction,
|
2021-03-11 18:41:01 +00:00
|
|
|
lock_state["doorLockStateChangeTransactionId"],
|
|
|
|
)
|
2020-07-03 18:33:12 +00:00
|
|
|
attempts += 1
|
|
|
|
if attempts == 30:
|
|
|
|
break
|
|
|
|
if attempts > 1:
|
2021-03-11 18:41:01 +00:00
|
|
|
await asyncio.sleep(0.5)
|
2019-07-31 19:25:30 +00:00
|
|
|
if transaction["result"] == "OK":
|
2017-06-26 20:30:25 +00:00
|
|
|
self._state = state
|