hass-core/homeassistant/components/lock/verisure.py
MartinHjelmare c56701baaf Refactor reproduce_state for scene component
* Add tests to reach full coverage for helpers/state.py.
* Refactor reproduce_state function in helpers/state.py. Add two dicts,
	as global constants, service_attributes and service_to_state. Use
	these in combination with the dict of services per domain from
	ServiceRegistry, to find the correct service to use in a scene state
	change.
* Use break statement in for loop, to break if service was selected
	to update state, in preference to update state attributes, ie state
	update takes precedence.
* Add ATTR_CODE and ATTR_CODE_FORMAT in const. Import these in
	alarm_control_panel and lock platforms instead of making duplicate
	constants in multiple modules.
* Use ATTR_MEDIA_CONTENT_TYPE and ATTR_MEDIA_CONTENT_ID in media_player
	platform in SERVICE_PLAY_MEDIA and play_media methods, instead of
	'media_type' and 'media_id'.
* Fix PEP257 in modified files.
2016-03-09 18:52:05 +01:00

84 lines
2.5 KiB
Python

"""
Interfaces with Verisure locks.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/verisure/
"""
import logging
from homeassistant.components.verisure import HUB as hub
from homeassistant.components.lock import LockDevice
from homeassistant.const import (
ATTR_CODE, STATE_LOCKED, STATE_UNKNOWN, STATE_UNLOCKED)
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Verisure platform."""
locks = []
if int(hub.config.get('locks', '1')):
hub.update_locks()
locks.extend([
VerisureDoorlock(device_id)
for device_id in hub.lock_status.keys()
])
add_devices(locks)
# pylint: disable=abstract-method
class VerisureDoorlock(LockDevice):
"""Representation of a Verisure doorlock."""
def __init__(self, device_id):
"""Initialize the lock."""
self._id = device_id
self._state = STATE_UNKNOWN
self._digits = int(hub.config.get('code_digits', '4'))
@property
def name(self):
"""Return the name of the lock."""
return 'Lock {}'.format(self._id)
@property
def state(self):
"""Return the state of the lock."""
return self._state
@property
def code_format(self):
"""Return the required six digit code."""
return '^\\d{%s}$' % self._digits
def update(self):
"""Update lock status."""
hub.update_locks()
if hub.lock_status[self._id].status == 'unlocked':
self._state = STATE_UNLOCKED
elif hub.lock_status[self._id].status == 'locked':
self._state = STATE_LOCKED
elif hub.lock_status[self._id].status != 'pending':
_LOGGER.error(
'Unknown lock state %s',
hub.lock_status[self._id].status)
@property
def is_locked(self):
"""Return true if lock is locked."""
return hub.lock_status[self._id].status
def unlock(self, **kwargs):
"""Send unlock command."""
hub.my_pages.lock.set(kwargs[ATTR_CODE], self._id, 'UNLOCKED')
_LOGGER.info('verisure doorlock unlocking')
hub.my_pages.lock.wait_while_pending()
self.update()
def lock(self, **kwargs):
"""Send lock command."""
hub.my_pages.lock.set(kwargs[ATTR_CODE], self._id, 'LOCKED')
_LOGGER.info('verisure doorlock locking')
hub.my_pages.lock.wait_while_pending()
self.update()