2019-06-07 15:16:34 +00:00
|
|
|
"""Locks on Zigbee Home Automation networks."""
|
2019-12-31 16:09:58 +00:00
|
|
|
import functools
|
2019-06-07 15:16:34 +00:00
|
|
|
|
2021-03-27 19:23:40 +00:00
|
|
|
import voluptuous as vol
|
2019-06-07 15:16:34 +00:00
|
|
|
from zigpy.zcl.foundation import Status
|
2019-08-02 10:05:23 +00:00
|
|
|
|
2021-12-11 16:06:39 +00:00
|
|
|
from homeassistant.components.lock import STATE_LOCKED, STATE_UNLOCKED, LockEntity
|
2021-12-11 16:50:03 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-11 16:06:39 +00:00
|
|
|
from homeassistant.const import Platform
|
2021-12-11 16:50:03 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-03-27 19:23:40 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, entity_platform
|
2019-06-07 15:16:34 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2019-08-02 10:05:23 +00:00
|
|
|
|
2020-02-21 23:06:57 +00:00
|
|
|
from .core import discovery
|
2019-06-07 15:16:34 +00:00
|
|
|
from .core.const import (
|
2019-08-02 14:37:21 +00:00
|
|
|
CHANNEL_DOORLOCK,
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_ZHA,
|
2020-02-21 23:06:57 +00:00
|
|
|
SIGNAL_ADD_ENTITIES,
|
2019-07-31 19:25:30 +00:00
|
|
|
SIGNAL_ATTR_UPDATED,
|
2019-06-07 15:16:34 +00:00
|
|
|
)
|
2019-12-31 16:09:58 +00:00
|
|
|
from .core.registries import ZHA_ENTITIES
|
2019-06-07 15:16:34 +00:00
|
|
|
from .entity import ZhaEntity
|
|
|
|
|
2020-10-17 02:24:08 +00:00
|
|
|
# The first state is Zigbee 'Not fully locked'
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_LIST = [STATE_UNLOCKED, STATE_LOCKED, STATE_UNLOCKED]
|
2021-12-11 16:06:39 +00:00
|
|
|
STRICT_MATCH = functools.partial(ZHA_ENTITIES.strict_match, Platform.LOCK)
|
2019-06-07 15:16:34 +00:00
|
|
|
|
2019-10-07 15:17:39 +00:00
|
|
|
VALUE_TO_STATE = dict(enumerate(STATE_LIST))
|
2019-06-07 15:16:34 +00:00
|
|
|
|
2021-03-27 19:23:40 +00:00
|
|
|
SERVICE_SET_LOCK_USER_CODE = "set_lock_user_code"
|
|
|
|
SERVICE_ENABLE_LOCK_USER_CODE = "enable_lock_user_code"
|
|
|
|
SERVICE_DISABLE_LOCK_USER_CODE = "disable_lock_user_code"
|
|
|
|
SERVICE_CLEAR_LOCK_USER_CODE = "clear_lock_user_code"
|
|
|
|
|
2019-06-07 15:16:34 +00:00
|
|
|
|
2021-12-11 16:50:03 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: entity_platform.AddEntitiesCallback,
|
|
|
|
):
|
2019-06-07 15:16:34 +00:00
|
|
|
"""Set up the Zigbee Home Automation Door Lock from config entry."""
|
2021-12-11 16:06:39 +00:00
|
|
|
entities_to_create = hass.data[DATA_ZHA][Platform.LOCK]
|
2019-06-07 15:16:34 +00:00
|
|
|
|
|
|
|
unsub = async_dispatcher_connect(
|
2020-02-21 23:06:57 +00:00
|
|
|
hass,
|
|
|
|
SIGNAL_ADD_ENTITIES,
|
|
|
|
functools.partial(
|
|
|
|
discovery.async_add_entities, async_add_entities, entities_to_create
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-12-11 16:50:03 +00:00
|
|
|
config_entry.async_on_unload(unsub)
|
2019-06-07 15:16:34 +00:00
|
|
|
|
2021-05-03 16:34:28 +00:00
|
|
|
platform = entity_platform.async_get_current_platform()
|
2021-03-27 19:23:40 +00:00
|
|
|
|
|
|
|
platform.async_register_entity_service( # type: ignore
|
|
|
|
SERVICE_SET_LOCK_USER_CODE,
|
|
|
|
{
|
|
|
|
vol.Required("code_slot"): vol.Coerce(int),
|
|
|
|
vol.Required("user_code"): cv.string,
|
|
|
|
},
|
|
|
|
"async_set_lock_user_code",
|
|
|
|
)
|
|
|
|
|
|
|
|
platform.async_register_entity_service( # type: ignore
|
|
|
|
SERVICE_ENABLE_LOCK_USER_CODE,
|
|
|
|
{
|
|
|
|
vol.Required("code_slot"): vol.Coerce(int),
|
|
|
|
},
|
|
|
|
"async_enable_lock_user_code",
|
|
|
|
)
|
|
|
|
|
|
|
|
platform.async_register_entity_service( # type: ignore
|
|
|
|
SERVICE_DISABLE_LOCK_USER_CODE,
|
|
|
|
{
|
|
|
|
vol.Required("code_slot"): vol.Coerce(int),
|
|
|
|
},
|
|
|
|
"async_disable_lock_user_code",
|
|
|
|
)
|
|
|
|
|
|
|
|
platform.async_register_entity_service( # type: ignore
|
|
|
|
SERVICE_CLEAR_LOCK_USER_CODE,
|
|
|
|
{
|
|
|
|
vol.Required("code_slot"): vol.Coerce(int),
|
|
|
|
},
|
|
|
|
"async_clear_lock_user_code",
|
|
|
|
)
|
|
|
|
|
2019-12-31 16:09:58 +00:00
|
|
|
|
|
|
|
@STRICT_MATCH(channel_names=CHANNEL_DOORLOCK)
|
2020-04-25 16:02:41 +00:00
|
|
|
class ZhaDoorLock(ZhaEntity, LockEntity):
|
2019-06-07 15:16:34 +00:00
|
|
|
"""Representation of a ZHA lock."""
|
|
|
|
|
|
|
|
def __init__(self, unique_id, zha_device, channels, **kwargs):
|
|
|
|
"""Init this sensor."""
|
|
|
|
super().__init__(unique_id, zha_device, channels, **kwargs)
|
2019-08-02 10:05:23 +00:00
|
|
|
self._doorlock_channel = self.cluster_channels.get(CHANNEL_DOORLOCK)
|
2019-06-07 15:16:34 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Run when about to be added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
2020-07-20 14:04:57 +00:00
|
|
|
self.async_accept_signal(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._doorlock_channel, SIGNAL_ATTR_UPDATED, self.async_set_state
|
|
|
|
)
|
2019-06-07 15:16:34 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_restore_last_state(self, last_state):
|
|
|
|
"""Restore previous state."""
|
|
|
|
self._state = VALUE_TO_STATE.get(last_state.state, last_state.state)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_locked(self) -> bool:
|
|
|
|
"""Return true if entity is locked."""
|
|
|
|
if self._state is None:
|
|
|
|
return False
|
|
|
|
return self._state == STATE_LOCKED
|
|
|
|
|
|
|
|
@property
|
2021-03-11 19:16:26 +00:00
|
|
|
def extra_state_attributes(self):
|
2019-06-07 15:16:34 +00:00
|
|
|
"""Return state attributes."""
|
|
|
|
return self.state_attributes
|
|
|
|
|
|
|
|
async def async_lock(self, **kwargs):
|
|
|
|
"""Lock the lock."""
|
|
|
|
result = await self._doorlock_channel.lock_door()
|
|
|
|
if not isinstance(result, list) or result[0] is not Status.SUCCESS:
|
2019-07-30 19:19:24 +00:00
|
|
|
self.error("Error with lock_door: %s", result)
|
2019-06-07 15:16:34 +00:00
|
|
|
return
|
2020-03-11 11:17:53 +00:00
|
|
|
self.async_write_ha_state()
|
2019-06-07 15:16:34 +00:00
|
|
|
|
|
|
|
async def async_unlock(self, **kwargs):
|
|
|
|
"""Unlock the lock."""
|
|
|
|
result = await self._doorlock_channel.unlock_door()
|
|
|
|
if not isinstance(result, list) or result[0] is not Status.SUCCESS:
|
2019-07-30 19:19:24 +00:00
|
|
|
self.error("Error with unlock_door: %s", result)
|
2019-06-07 15:16:34 +00:00
|
|
|
return
|
2020-03-11 11:17:53 +00:00
|
|
|
self.async_write_ha_state()
|
2019-06-07 15:16:34 +00:00
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Attempt to retrieve state from the lock."""
|
|
|
|
await super().async_update()
|
|
|
|
await self.async_get_state()
|
|
|
|
|
2020-01-29 21:59:45 +00:00
|
|
|
@callback
|
2020-03-04 18:11:53 +00:00
|
|
|
def async_set_state(self, attr_id, attr_name, value):
|
2019-06-07 15:16:34 +00:00
|
|
|
"""Handle state update from channel."""
|
2020-03-04 18:11:53 +00:00
|
|
|
self._state = VALUE_TO_STATE.get(value, self._state)
|
2020-03-11 11:17:53 +00:00
|
|
|
self.async_write_ha_state()
|
2019-06-07 15:16:34 +00:00
|
|
|
|
|
|
|
async def async_get_state(self, from_cache=True):
|
|
|
|
"""Attempt to retrieve state from the lock."""
|
|
|
|
if self._doorlock_channel:
|
|
|
|
state = await self._doorlock_channel.get_attribute_value(
|
2019-07-31 19:25:30 +00:00
|
|
|
"lock_state", from_cache=from_cache
|
|
|
|
)
|
2019-06-07 15:16:34 +00:00
|
|
|
if state is not None:
|
|
|
|
self._state = VALUE_TO_STATE.get(state, self._state)
|
|
|
|
|
|
|
|
async def refresh(self, time):
|
|
|
|
"""Call async_get_state at an interval."""
|
|
|
|
await self.async_get_state(from_cache=False)
|
2021-03-27 19:23:40 +00:00
|
|
|
|
|
|
|
async def async_set_lock_user_code(self, code_slot: int, user_code: str) -> None:
|
|
|
|
"""Set the user_code to index X on the lock."""
|
|
|
|
if self._doorlock_channel:
|
|
|
|
await self._doorlock_channel.async_set_user_code(code_slot, user_code)
|
|
|
|
self.debug("User code at slot %s set", code_slot)
|
|
|
|
|
|
|
|
async def async_enable_lock_user_code(self, code_slot: int) -> None:
|
|
|
|
"""Enable user_code at index X on the lock."""
|
|
|
|
if self._doorlock_channel:
|
|
|
|
await self._doorlock_channel.async_enable_user_code(code_slot)
|
|
|
|
self.debug("User code at slot %s enabled", code_slot)
|
|
|
|
|
|
|
|
async def async_disable_lock_user_code(self, code_slot: int) -> None:
|
|
|
|
"""Disable user_code at index X on the lock."""
|
|
|
|
if self._doorlock_channel:
|
|
|
|
await self._doorlock_channel.async_disable_user_code(code_slot)
|
|
|
|
self.debug("User code at slot %s disabled", code_slot)
|
|
|
|
|
|
|
|
async def async_clear_lock_user_code(self, code_slot: int) -> None:
|
|
|
|
"""Clear the user_code at index X on the lock."""
|
|
|
|
if self._doorlock_channel:
|
|
|
|
await self._doorlock_channel.async_clear_user_code(code_slot)
|
|
|
|
self.debug("User code at slot %s cleared", code_slot)
|