2019-02-14 15:01:46 +00:00
|
|
|
"""Support for HomeKit Controller locks."""
|
2019-01-12 02:48:28 +00:00
|
|
|
import logging
|
|
|
|
|
2019-02-14 15:01:46 +00:00
|
|
|
from homeassistant.components.homekit_controller import (
|
|
|
|
KNOWN_ACCESSORIES, HomeKitEntity)
|
2019-01-12 02:48:28 +00:00
|
|
|
from homeassistant.components.lock import LockDevice
|
2019-02-14 15:01:46 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_BATTERY_LEVEL, STATE_LOCKED, STATE_UNLOCKED)
|
2019-01-12 02:48:28 +00:00
|
|
|
|
|
|
|
DEPENDENCIES = ['homekit_controller']
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
STATE_JAMMED = 'jammed'
|
|
|
|
|
|
|
|
CURRENT_STATE_MAP = {
|
|
|
|
0: STATE_UNLOCKED,
|
|
|
|
1: STATE_LOCKED,
|
|
|
|
2: STATE_JAMMED,
|
|
|
|
3: None,
|
|
|
|
}
|
|
|
|
|
|
|
|
TARGET_STATE_MAP = {
|
|
|
|
STATE_UNLOCKED: 0,
|
2019-02-14 15:01:46 +00:00
|
|
|
STATE_LOCKED: 1,
|
2019-01-12 02:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
|
|
|
"""Set up Homekit Lock support."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
|
2019-02-14 15:01:46 +00:00
|
|
|
add_entities([HomeKitLock(accessory, discovery_info)], True)
|
2019-01-12 02:48:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HomeKitLock(HomeKitEntity, LockDevice):
|
|
|
|
"""Representation of a HomeKit Controller Lock."""
|
|
|
|
|
|
|
|
def __init__(self, accessory, discovery_info):
|
|
|
|
"""Initialise the Lock."""
|
|
|
|
super().__init__(accessory, discovery_info)
|
|
|
|
self._state = None
|
|
|
|
self._name = discovery_info['model']
|
|
|
|
self._battery_level = None
|
|
|
|
|
2019-01-28 16:21:20 +00:00
|
|
|
def get_characteristic_types(self):
|
|
|
|
"""Define the homekit characteristics the entity cares about."""
|
|
|
|
# pylint: disable=import-error
|
|
|
|
from homekit.model.characteristics import CharacteristicsTypes
|
|
|
|
return [
|
|
|
|
CharacteristicsTypes.LOCK_MECHANISM_CURRENT_STATE,
|
|
|
|
CharacteristicsTypes.LOCK_MECHANISM_TARGET_STATE,
|
|
|
|
CharacteristicsTypes.BATTERY_LEVEL,
|
|
|
|
]
|
|
|
|
|
2019-01-28 20:27:26 +00:00
|
|
|
def _update_lock_mechanism_current_state(self, value):
|
|
|
|
self._state = CURRENT_STATE_MAP[value]
|
2019-01-12 02:48:28 +00:00
|
|
|
|
2019-01-28 20:27:26 +00:00
|
|
|
def _update_battery_level(self, value):
|
|
|
|
self._battery_level = value
|
2019-01-12 02:48:28 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of this device."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_locked(self):
|
|
|
|
"""Return true if device is locked."""
|
|
|
|
return self._state == STATE_LOCKED
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self._state is not None
|
|
|
|
|
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."""
|
|
|
|
characteristics = [{'aid': self._aid,
|
|
|
|
'iid': self._chars['lock-mechanism.target-state'],
|
|
|
|
'value': TARGET_STATE_MAP[state]}]
|
2019-03-11 18:59:41 +00:00
|
|
|
await self._accessory.put_characteristics(characteristics)
|
2019-01-12 02:48:28 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the optional state attributes."""
|
|
|
|
if self._battery_level is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return {
|
|
|
|
ATTR_BATTERY_LEVEL: self._battery_level,
|
|
|
|
}
|