Changed do_lock and do_unlock methods to lock and unlock.

Implemented state method.
Fixed locked method for demo interface.
Changed LockDevice to extend Entity instead of ToggleEntity
This commit is contained in:
miniconfig 2015-11-19 16:54:55 -05:00
parent c78899c4f3
commit fa7391cdf6
3 changed files with 31 additions and 48 deletions

View File

@ -12,12 +12,12 @@ import os
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 @@ PROP_TO_ATTR = {
_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 setup(hass, config):
""" 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 setup(hass, config):
return True
class LockDevice(ToggleEntity):
class LockDevice(Entity):
""" Represents a lock within Home Assistant. """
# pylint: disable=no-self-use
@ -102,23 +102,8 @@ class LockDevice(ToggleEntity):
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

View File

@ -41,16 +41,19 @@ class DemoLock(LockDevice):
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()

View File

@ -8,8 +8,8 @@ https://home-assistant.io/components/lock.wink/
"""
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 @@ class WinkLockDevice(Entity):
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)