mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
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:
parent
c78899c4f3
commit
fa7391cdf6
@ -12,12 +12,12 @@ import os
|
|||||||
|
|
||||||
from homeassistant.config import load_yaml_config_file
|
from homeassistant.config import load_yaml_config_file
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
from homeassistant.helpers.entity import ToggleEntity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
STATE_LOCKED, SERVICE_LOCK, SERVICE_UNLOCK, ATTR_ENTITY_ID)
|
STATE_LOCKED, STATE_UNLOCKED, STATE_UNKNOWN, SERVICE_LOCK, SERVICE_UNLOCK,
|
||||||
from homeassistant.components import (
|
ATTR_ENTITY_ID)
|
||||||
group, wink)
|
from homeassistant.components import (group, wink)
|
||||||
|
|
||||||
DOMAIN = 'lock'
|
DOMAIN = 'lock'
|
||||||
DEPENDENCIES = []
|
DEPENDENCIES = []
|
||||||
@ -44,19 +44,19 @@ PROP_TO_ATTR = {
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_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. """
|
""" Returns if the lock is locked based on the statemachine. """
|
||||||
entity_id = entity_id or ENTITY_ID_ALL_LOCKS
|
entity_id = entity_id or ENTITY_ID_ALL_LOCKS
|
||||||
return hass.states.is_state(entity_id, STATE_LOCKED)
|
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. """
|
""" Locks all or specified locks. """
|
||||||
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
|
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
|
||||||
hass.services.call(DOMAIN, SERVICE_LOCK, data)
|
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. """
|
""" Unlocks all or specified locks. """
|
||||||
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
|
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
|
||||||
hass.services.call(DOMAIN, SERVICE_UNLOCK, data)
|
hass.services.call(DOMAIN, SERVICE_UNLOCK, data)
|
||||||
@ -73,14 +73,14 @@ def setup(hass, config):
|
|||||||
""" Handles calls to the lock services. """
|
""" Handles calls to the lock services. """
|
||||||
target_locks = component.extract_from_service(service)
|
target_locks = component.extract_from_service(service)
|
||||||
|
|
||||||
for lock in target_locks:
|
for item in target_locks:
|
||||||
if service.service == SERVICE_LOCK:
|
if service.service == SERVICE_LOCK:
|
||||||
lock.do_lock()
|
item.lock()
|
||||||
else:
|
else:
|
||||||
lock.do_unlock()
|
item.unlock()
|
||||||
|
|
||||||
if lock.should_poll:
|
if item.should_poll:
|
||||||
lock.update_ha_state(True)
|
item.update_ha_state(True)
|
||||||
|
|
||||||
descriptions = load_yaml_config_file(
|
descriptions = load_yaml_config_file(
|
||||||
os.path.join(os.path.dirname(__file__), 'services.yaml'))
|
os.path.join(os.path.dirname(__file__), 'services.yaml'))
|
||||||
@ -92,7 +92,7 @@ def setup(hass, config):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class LockDevice(ToggleEntity):
|
class LockDevice(Entity):
|
||||||
""" Represents a lock within Home Assistant. """
|
""" Represents a lock within Home Assistant. """
|
||||||
# pylint: disable=no-self-use
|
# pylint: disable=no-self-use
|
||||||
|
|
||||||
@ -102,23 +102,8 @@ class LockDevice(ToggleEntity):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def state(self):
|
||||||
""" Returns device specific state attributes. """
|
is_locked = self.locked
|
||||||
return None
|
if is_locked is None:
|
||||||
|
return STATE_UNKNOWN
|
||||||
@property
|
return STATE_LOCKED if is_locked else STATE_UNLOCKED
|
||||||
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
|
|
||||||
|
@ -41,16 +41,19 @@ class DemoLock(LockDevice):
|
|||||||
return self._icon
|
return self._icon
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_locked(self):
|
def locked(self):
|
||||||
""" True if device is locked. """
|
""" 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. """
|
""" Lock the device. """
|
||||||
self._state = STATE_LOCKED
|
self._state = STATE_LOCKED
|
||||||
self.update_ha_state()
|
self.update_ha_state()
|
||||||
|
|
||||||
def do_unlock(self, **kwargs):
|
def unlock(self, **kwargs):
|
||||||
""" Unlock the device. """
|
""" Unlock the device. """
|
||||||
self._state = STATE_UNLOCKED
|
self._state = STATE_UNLOCKED
|
||||||
self.update_ha_state()
|
self.update_ha_state()
|
||||||
|
@ -8,8 +8,8 @@ https://home-assistant.io/components/lock.wink/
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.components.lock import LockDevice
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN, STATE_LOCKED, STATE_UNLOCKED
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
||||||
|
|
||||||
REQUIREMENTS = ['https://github.com/balloob/python-wink/archive/'
|
REQUIREMENTS = ['https://github.com/balloob/python-wink/archive/'
|
||||||
'9eb39eaba0717922815e673ad1114c685839d890.zip'
|
'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())
|
add_devices(WinkLockDevice(lock) for lock in pywink.get_locks())
|
||||||
|
|
||||||
|
|
||||||
class WinkLockDevice(Entity):
|
class WinkLockDevice(LockDevice):
|
||||||
""" Represents a Wink lock. """
|
""" Represents a Wink lock. """
|
||||||
|
|
||||||
def __init__(self, wink):
|
def __init__(self, wink):
|
||||||
self.wink = wink
|
self.wink = wink
|
||||||
|
|
||||||
@property
|
|
||||||
def state(self):
|
|
||||||
""" Returns the state. """
|
|
||||||
return STATE_LOCKED if self.is_locked else STATE_UNLOCKED
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
""" Returns the id of this wink lock """
|
""" Returns the id of this wink lock """
|
||||||
@ -60,14 +55,14 @@ class WinkLockDevice(Entity):
|
|||||||
self.wink.updateState()
|
self.wink.updateState()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_locked(self):
|
def locked(self):
|
||||||
""" True if device is locked. """
|
""" True if device is locked. """
|
||||||
return self.wink.state()
|
return self.wink.state()
|
||||||
|
|
||||||
def do_lock(self):
|
def lock(self):
|
||||||
""" Lock the device. """
|
""" Lock the device. """
|
||||||
self.wink.setState(True)
|
self.wink.setState(True)
|
||||||
|
|
||||||
def do_unlock(self):
|
def unlock(self):
|
||||||
""" Unlock the device. """
|
""" Unlock the device. """
|
||||||
self.wink.setState(False)
|
self.wink.setState(False)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user