2019-02-14 15:01:46 +00:00
|
|
|
"""Support for HomeKit Controller locks."""
|
2019-01-12 02:48:28 +00:00
|
|
|
import logging
|
|
|
|
|
2020-02-24 09:55:33 +00:00
|
|
|
from aiohomekit.model.characteristics import CharacteristicsTypes
|
2019-08-14 16:14:15 +00:00
|
|
|
|
2019-01-12 02:48:28 +00:00
|
|
|
from homeassistant.components.lock import LockDevice
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import ATTR_BATTERY_LEVEL, STATE_LOCKED, STATE_UNLOCKED
|
2020-01-29 21:59:45 +00:00
|
|
|
from homeassistant.core import callback
|
2019-01-12 02:48:28 +00:00
|
|
|
|
2019-03-26 06:49:51 +00:00
|
|
|
from . import KNOWN_DEVICES, HomeKitEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-01-12 02:48:28 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_JAMMED = "jammed"
|
2019-01-12 02:48:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CURRENT_STATE_MAP = {0: STATE_UNLOCKED, 1: STATE_LOCKED, 2: STATE_JAMMED, 3: None}
|
2019-01-12 02:48:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
TARGET_STATE_MAP = {STATE_UNLOCKED: 0, STATE_LOCKED: 1}
|
2019-01-12 02:48:28 +00:00
|
|
|
|
|
|
|
|
2019-05-13 06:56:05 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up Homekit lock."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hkid = config_entry.data["AccessoryPairingID"]
|
2019-05-13 06:56:05 +00:00
|
|
|
conn = hass.data[KNOWN_DEVICES][hkid]
|
|
|
|
|
2020-01-29 21:59:45 +00:00
|
|
|
@callback
|
2019-05-13 06:56:05 +00:00
|
|
|
def async_add_service(aid, service):
|
2019-07-31 19:25:30 +00:00
|
|
|
if service["stype"] != "lock-mechanism":
|
2019-05-13 06:56:05 +00:00
|
|
|
return False
|
2019-07-31 19:25:30 +00:00
|
|
|
info = {"aid": aid, "iid": service["iid"]}
|
2019-05-13 06:56:05 +00:00
|
|
|
async_add_entities([HomeKitLock(conn, info)], True)
|
|
|
|
return True
|
|
|
|
|
|
|
|
conn.add_listener(async_add_service)
|
2019-01-12 02:48:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HomeKitLock(HomeKitEntity, LockDevice):
|
|
|
|
"""Representation of a HomeKit Controller Lock."""
|
|
|
|
|
2019-01-28 16:21:20 +00:00
|
|
|
def get_characteristic_types(self):
|
|
|
|
"""Define the homekit characteristics the entity cares about."""
|
|
|
|
return [
|
|
|
|
CharacteristicsTypes.LOCK_MECHANISM_CURRENT_STATE,
|
|
|
|
CharacteristicsTypes.LOCK_MECHANISM_TARGET_STATE,
|
|
|
|
CharacteristicsTypes.BATTERY_LEVEL,
|
|
|
|
]
|
|
|
|
|
2019-01-12 02:48:28 +00:00
|
|
|
@property
|
|
|
|
def is_locked(self):
|
|
|
|
"""Return true if device is locked."""
|
2020-03-11 11:40:47 +00:00
|
|
|
value = self.service.value(CharacteristicsTypes.LOCK_MECHANISM_CURRENT_STATE)
|
|
|
|
return CURRENT_STATE_MAP[value] == STATE_LOCKED
|
2019-01-12 02:48:28 +00:00
|
|
|
|
2019-03-11 18:59:41 +00:00
|
|
|
async def async_lock(self, **kwargs):
|
2019-01-12 02:48:28 +00:00
|
|
|
"""Lock the device."""
|
2019-03-11 18:59:41 +00:00
|
|
|
await self._set_lock_state(STATE_LOCKED)
|
2019-01-12 02:48:28 +00:00
|
|
|
|
2019-03-11 18:59:41 +00:00
|
|
|
async def async_unlock(self, **kwargs):
|
2019-01-12 02:48:28 +00:00
|
|
|
"""Unlock the device."""
|
2019-03-11 18:59:41 +00:00
|
|
|
await self._set_lock_state(STATE_UNLOCKED)
|
2019-01-12 02:48:28 +00:00
|
|
|
|
2019-03-11 18:59:41 +00:00
|
|
|
async def _set_lock_state(self, state):
|
2019-01-12 02:48:28 +00:00
|
|
|
"""Send state command."""
|
2020-03-11 16:27:20 +00:00
|
|
|
await self.async_put_characteristics(
|
|
|
|
{CharacteristicsTypes.LOCK_MECHANISM_TARGET_STATE: TARGET_STATE_MAP[state]}
|
|
|
|
)
|
2019-01-12 02:48:28 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the optional state attributes."""
|
2020-03-11 11:40:47 +00:00
|
|
|
attributes = {}
|
|
|
|
|
|
|
|
battery_level = self.service.value(CharacteristicsTypes.BATTERY_LEVEL)
|
|
|
|
if battery_level:
|
|
|
|
attributes[ATTR_BATTERY_LEVEL] = battery_level
|
2019-01-12 02:48:28 +00:00
|
|
|
|
2020-03-11 11:40:47 +00:00
|
|
|
return attributes
|