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
|
|
|
import logging
|
|
|
|
|
|
|
|
from zigpy.zcl.foundation import Status
|
2019-08-02 10:05:23 +00:00
|
|
|
|
2019-06-07 15:16:34 +00:00
|
|
|
from homeassistant.components.lock import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
STATE_LOCKED,
|
2019-08-02 10:05:23 +00:00
|
|
|
STATE_UNLOCKED,
|
2019-07-31 19:25:30 +00:00
|
|
|
LockDevice,
|
|
|
|
)
|
2019-08-02 10:05:23 +00:00
|
|
|
from homeassistant.core import callback
|
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,
|
|
|
|
DATA_ZHA_DISPATCHERS,
|
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
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
""" The first state is Zigbee 'Not fully locked' """
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_LIST = [STATE_UNLOCKED, STATE_LOCKED, STATE_UNLOCKED]
|
2019-12-31 16:09:58 +00:00
|
|
|
STRICT_MATCH = functools.partial(ZHA_ENTITIES.strict_match, DOMAIN)
|
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
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Zigbee Home Automation Door Lock from config entry."""
|
2020-02-21 23:06:57 +00:00
|
|
|
entities_to_create = hass.data[DATA_ZHA][DOMAIN] = []
|
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
|
|
|
)
|
2019-06-07 15:16:34 +00:00
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS].append(unsub)
|
|
|
|
|
2019-12-31 16:09:58 +00:00
|
|
|
|
|
|
|
@STRICT_MATCH(channel_names=CHANNEL_DOORLOCK)
|
2019-06-07 15:16:34 +00:00
|
|
|
class ZhaDoorLock(ZhaEntity, LockDevice):
|
|
|
|
"""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()
|
|
|
|
await 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
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""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
|
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
|
|
|
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
|
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
|
|
|
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
|
2019-06-07 15:16:34 +00:00
|
|
|
def async_set_state(self, state):
|
|
|
|
"""Handle state update from channel."""
|
|
|
|
self._state = VALUE_TO_STATE.get(state, self._state)
|
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
|
|
|
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)
|