2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Verisure locks."""
|
2016-01-27 15:38:43 +00:00
|
|
|
import logging
|
2019-03-21 05:56:46 +00:00
|
|
|
from time import sleep, time
|
|
|
|
|
2016-01-27 15:38:43 +00:00
|
|
|
from homeassistant.components.lock import LockDevice
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.const import ATTR_CODE, STATE_LOCKED, STATE_UNLOCKED
|
|
|
|
|
|
|
|
from . import CONF_CODE_DIGITS, CONF_DEFAULT_LOCK_CODE, CONF_LOCKS, HUB as hub
|
2016-01-27 15:38:43 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2019-02-13 20:21:14 +00:00
|
|
|
"""Set up the Verisure lock platform."""
|
2016-01-27 15:38:43 +00:00
|
|
|
locks = []
|
2016-09-07 01:18:34 +00:00
|
|
|
if int(hub.config.get(CONF_LOCKS, 1)):
|
2017-06-26 20:30:25 +00:00
|
|
|
hub.update_overview()
|
2016-02-27 20:50:19 +00:00
|
|
|
locks.extend([
|
2017-06-26 20:30:25 +00:00
|
|
|
VerisureDoorlock(device_label)
|
|
|
|
for device_label in hub.get(
|
|
|
|
"$.doorLockStatusList[*].deviceLabel")])
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(locks)
|
2016-01-27 15:38:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VerisureDoorlock(LockDevice):
|
2016-03-07 21:13:18 +00:00
|
|
|
"""Representation of a Verisure doorlock."""
|
|
|
|
|
2017-06-26 20:30:25 +00:00
|
|
|
def __init__(self, device_label):
|
2017-04-24 03:16:52 +00:00
|
|
|
"""Initialize the Verisure lock."""
|
2017-06-26 20:30:25 +00:00
|
|
|
self._device_label = device_label
|
2019-01-24 07:20:20 +00:00
|
|
|
self._state = None
|
2016-09-07 01:18:34 +00:00
|
|
|
self._digits = hub.config.get(CONF_CODE_DIGITS)
|
2016-08-10 02:37:46 +00:00
|
|
|
self._changed_by = None
|
2017-06-26 20:30:25 +00:00
|
|
|
self._change_timestamp = 0
|
2018-12-03 06:25:54 +00:00
|
|
|
self._default_lock_code = hub.config.get(CONF_DEFAULT_LOCK_CODE)
|
2016-01-27 15:38:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 21:13:18 +00:00
|
|
|
"""Return the name of the lock."""
|
2017-06-26 20:30:25 +00:00
|
|
|
return hub.get_first(
|
|
|
|
"$.doorLockStatusList[?(@.deviceLabel=='%s')].area",
|
2017-06-30 06:53:14 +00:00
|
|
|
self._device_label)
|
2016-01-27 15:38:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-07 21:13:18 +00:00
|
|
|
"""Return the state of the lock."""
|
2016-01-27 15:38:43 +00:00
|
|
|
return self._state
|
|
|
|
|
2016-05-04 01:53:11 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
2017-06-26 20:30:25 +00:00
|
|
|
return hub.get_first(
|
|
|
|
"$.doorLockStatusList[?(@.deviceLabel=='%s')]",
|
|
|
|
self._device_label) is not None
|
2016-05-04 01:53:11 +00:00
|
|
|
|
2016-08-10 02:37:46 +00:00
|
|
|
@property
|
|
|
|
def changed_by(self):
|
|
|
|
"""Last change triggered by."""
|
|
|
|
return self._changed_by
|
|
|
|
|
2016-01-27 15:38:43 +00:00
|
|
|
@property
|
|
|
|
def code_format(self):
|
2016-03-07 21:13:18 +00:00
|
|
|
"""Return the required six digit code."""
|
2016-02-27 20:50:19 +00:00
|
|
|
return '^\\d{%s}$' % self._digits
|
2016-01-27 15:38:43 +00:00
|
|
|
|
|
|
|
def update(self):
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Update lock status."""
|
2017-06-26 20:30:25 +00:00
|
|
|
if time() - self._change_timestamp < 10:
|
|
|
|
return
|
|
|
|
hub.update_overview()
|
|
|
|
status = hub.get_first(
|
|
|
|
"$.doorLockStatusList[?(@.deviceLabel=='%s')].lockedState",
|
|
|
|
self._device_label)
|
|
|
|
if status == 'UNLOCKED':
|
2019-02-08 13:35:38 +00:00
|
|
|
self._state = STATE_UNLOCKED
|
2017-06-26 20:30:25 +00:00
|
|
|
elif status == 'LOCKED':
|
2016-01-27 15:38:43 +00:00
|
|
|
self._state = STATE_LOCKED
|
2017-06-26 20:30:25 +00:00
|
|
|
elif status != 'PENDING':
|
|
|
|
_LOGGER.error('Unknown lock state %s', status)
|
|
|
|
self._changed_by = hub.get_first(
|
|
|
|
"$.doorLockStatusList[?(@.deviceLabel=='%s')].userString",
|
|
|
|
self._device_label)
|
2016-01-27 15:38:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_locked(self):
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Return true if lock is locked."""
|
2017-06-26 20:30:25 +00:00
|
|
|
return self._state == STATE_LOCKED
|
2016-01-27 15:38:43 +00:00
|
|
|
|
|
|
|
def unlock(self, **kwargs):
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Send unlock command."""
|
2019-01-24 07:20:20 +00:00
|
|
|
if self._state is None:
|
2017-06-26 20:30:25 +00:00
|
|
|
return
|
2018-12-03 06:25:54 +00:00
|
|
|
|
|
|
|
code = kwargs.get(ATTR_CODE, self._default_lock_code)
|
|
|
|
if code is None:
|
|
|
|
_LOGGER.error("Code required but none provided")
|
|
|
|
return
|
|
|
|
|
|
|
|
self.set_lock_state(code, STATE_UNLOCKED)
|
2016-01-27 15:38:43 +00:00
|
|
|
|
|
|
|
def lock(self, **kwargs):
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Send lock command."""
|
2017-06-26 20:30:25 +00:00
|
|
|
if self._state == STATE_LOCKED:
|
|
|
|
return
|
2018-12-03 06:25:54 +00:00
|
|
|
|
|
|
|
code = kwargs.get(ATTR_CODE, self._default_lock_code)
|
|
|
|
if code is None:
|
|
|
|
_LOGGER.error("Code required but none provided")
|
|
|
|
return
|
|
|
|
|
|
|
|
self.set_lock_state(code, STATE_LOCKED)
|
2017-06-26 20:30:25 +00:00
|
|
|
|
|
|
|
def set_lock_state(self, code, state):
|
|
|
|
"""Send set lock state command."""
|
|
|
|
lock_state = 'lock' if state == STATE_LOCKED else 'unlock'
|
|
|
|
transaction_id = hub.session.set_lock_state(
|
|
|
|
code,
|
|
|
|
self._device_label,
|
|
|
|
lock_state)['doorLockStateChangeTransactionId']
|
|
|
|
_LOGGER.debug("Verisure doorlock %s", state)
|
|
|
|
transaction = {}
|
|
|
|
while 'result' not in transaction:
|
|
|
|
sleep(0.5)
|
|
|
|
transaction = hub.session.get_lock_state_transaction(
|
|
|
|
transaction_id)
|
|
|
|
if transaction['result'] == 'OK':
|
|
|
|
self._state = state
|
|
|
|
self._change_timestamp = time()
|