diff --git a/homeassistant/components/lock/__init__.py b/homeassistant/components/lock/__init__.py index 88768208c1ed0d..613410dfd07508 100644 --- a/homeassistant/components/lock/__init__.py +++ b/homeassistant/components/lock/__init__.py @@ -12,12 +12,12 @@ from homeassistant.config import load_yaml_config_file from homeassistant.helpers.entity_component import EntityComponent -from homeassistant.helpers.entity import ToggleEntity +from homeassistant.helpers.entity import Entity from homeassistant.const import ( - STATE_LOCKED, SERVICE_LOCK, SERVICE_UNLOCK, ATTR_ENTITY_ID) -from homeassistant.components import ( - group, wink) + STATE_LOCKED, STATE_UNLOCKED, STATE_UNKNOWN, SERVICE_LOCK, SERVICE_UNLOCK, + ATTR_ENTITY_ID) +from homeassistant.components import (group, wink) DOMAIN = 'lock' DEPENDENCIES = [] @@ -44,19 +44,19 @@ _LOGGER = logging.getLogger(__name__) -def is_locked(hass, entity_id=None): +def locked(hass, entity_id=None): """ Returns if the lock is locked based on the statemachine. """ entity_id = entity_id or ENTITY_ID_ALL_LOCKS return hass.states.is_state(entity_id, STATE_LOCKED) -def do_lock(hass, entity_id=None): +def lock(hass, entity_id=None): """ Locks all or specified locks. """ data = {ATTR_ENTITY_ID: entity_id} if entity_id else None hass.services.call(DOMAIN, SERVICE_LOCK, data) -def do_unlock(hass, entity_id=None): +def unlock(hass, entity_id=None): """ Unlocks all or specified locks. """ data = {ATTR_ENTITY_ID: entity_id} if entity_id else None hass.services.call(DOMAIN, SERVICE_UNLOCK, data) @@ -73,14 +73,14 @@ def handle_lock_service(service): """ Handles calls to the lock services. """ target_locks = component.extract_from_service(service) - for lock in target_locks: + for item in target_locks: if service.service == SERVICE_LOCK: - lock.do_lock() + item.lock() else: - lock.do_unlock() + item.unlock() - if lock.should_poll: - lock.update_ha_state(True) + if item.should_poll: + item.update_ha_state(True) descriptions = load_yaml_config_file( os.path.join(os.path.dirname(__file__), 'services.yaml')) @@ -92,7 +92,7 @@ def handle_lock_service(service): return True -class LockDevice(ToggleEntity): +class LockDevice(Entity): """ Represents a lock within Home Assistant. """ # pylint: disable=no-self-use @@ -102,23 +102,8 @@ def locked(self): return None @property - def device_state_attributes(self): - """ Returns device specific state attributes. """ - return None - - @property - def state_attributes(self): - """ Returns optional state attributes. """ - data = {} - - for prop, attr in PROP_TO_ATTR.items(): - value = getattr(self, prop) - if value: - data[attr] = value - - device_attr = self.device_state_attributes - - if device_attr is not None: - data.update(device_attr) - - return data + def state(self): + is_locked = self.locked + if is_locked is None: + return STATE_UNKNOWN + return STATE_LOCKED if is_locked else STATE_UNLOCKED diff --git a/homeassistant/components/lock/demo.py b/homeassistant/components/lock/demo.py index 76eea0104fb26c..67e7032849fd50 100644 --- a/homeassistant/components/lock/demo.py +++ b/homeassistant/components/lock/demo.py @@ -41,16 +41,19 @@ def icon(self): return self._icon @property - def is_locked(self): + def locked(self): """ True if device is locked. """ - return self._state + if self._state == STATE_LOCKED: + return True + else: + return False - def do_lock(self, **kwargs): + def lock(self, **kwargs): """ Lock the device. """ self._state = STATE_LOCKED self.update_ha_state() - def do_unlock(self, **kwargs): + def unlock(self, **kwargs): """ Unlock the device. """ self._state = STATE_UNLOCKED self.update_ha_state() diff --git a/homeassistant/components/lock/wink.py b/homeassistant/components/lock/wink.py index 059c3a56611fbd..5e463f49a8d296 100644 --- a/homeassistant/components/lock/wink.py +++ b/homeassistant/components/lock/wink.py @@ -8,8 +8,8 @@ """ import logging -from homeassistant.helpers.entity import Entity -from homeassistant.const import CONF_ACCESS_TOKEN, STATE_LOCKED, STATE_UNLOCKED +from homeassistant.components.lock import LockDevice +from homeassistant.const import CONF_ACCESS_TOKEN REQUIREMENTS = ['https://github.com/balloob/python-wink/archive/' '9eb39eaba0717922815e673ad1114c685839d890.zip' @@ -34,17 +34,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None): add_devices(WinkLockDevice(lock) for lock in pywink.get_locks()) -class WinkLockDevice(Entity): +class WinkLockDevice(LockDevice): """ Represents a Wink lock. """ def __init__(self, wink): self.wink = wink - @property - def state(self): - """ Returns the state. """ - return STATE_LOCKED if self.is_locked else STATE_UNLOCKED - @property def unique_id(self): """ Returns the id of this wink lock """ @@ -60,14 +55,14 @@ def update(self): self.wink.updateState() @property - def is_locked(self): + def locked(self): """ True if device is locked. """ return self.wink.state() - def do_lock(self): + def lock(self): """ Lock the device. """ self.wink.setState(True) - def do_unlock(self): + def unlock(self): """ Unlock the device. """ self.wink.setState(False)