Skip to content

Commit

Permalink
Changed do_lock and do_unlock methods to lock and unlock.
Browse files Browse the repository at this point in the history
Implemented state method.
Fixed locked method for demo interface.
Changed LockDevice to extend Entity instead of ToggleEntity
  • Loading branch information
miniconfig committed Nov 19, 2015
1 parent c78899c commit fa7391c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 48 deletions.
51 changes: 18 additions & 33 deletions homeassistant/components/lock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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)
Expand All @@ -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'))
Expand All @@ -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

Expand All @@ -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
11 changes: 7 additions & 4 deletions homeassistant/components/lock/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
17 changes: 6 additions & 11 deletions homeassistant/components/lock/wink.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 """
Expand All @@ -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)

0 comments on commit fa7391c

Please sign in to comment.