Ben Dews c20e0b985a Add Lock capability to SmartThings platform (#20977)
* Bumped pysmartthings version to 0.6.1

* Added Lock to supported platforms

* Added SmartThings Lock component

* Updated lock to eagerly set state

* Updated requirements_all.txt & requirements_test_all.txt with pysmartthings==0.6.1

* Added SmartThings Lock tests

* Removed inapplicable comment

* Removed unused import (STATE_UNLOCKED)

* Populated device_state_attributes with values provided by SmartThings

* Condensed if_lock assertion function

* Updated gathered attributes

* Fixed typo

* Updated tests to use new setup_platform

* Updated assignment of device state attributes

* Updated tests to utilise the LOCK_DOMAIN constant where suitable

* Fixed false positive for Switch test: (test_unload_config_entry)

* Implemented constant to contain expected SmartThings state for is_locked check

* Improved allocation of State Attributes

* Improved allocation of state attributes

* Fixed lint error (was running lint checks against the wrong file, whoops)

* Added test for unloading lock config

* Use isinstance instead of type()

* Updated device state to explicitly check for is not None instead of a truthy value
2019-02-13 18:36:49 -06:00

74 lines
2.3 KiB
Python

"""
Support for locks through the SmartThings cloud API.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/smartthings.lock/
"""
from homeassistant.components.lock import LockDevice
from . import SmartThingsEntity
from .const import DATA_BROKERS, DOMAIN
DEPENDENCIES = ['smartthings']
ST_STATE_LOCKED = 'locked'
ST_LOCK_ATTR_MAP = {
'method': 'method',
'codeId': 'code_id',
'timeout': 'timeout'
}
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Platform uses config entry setup."""
pass
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Add locks for a config entry."""
broker = hass.data[DOMAIN][DATA_BROKERS][config_entry.entry_id]
async_add_entities(
[SmartThingsLock(device) for device in broker.devices.values()
if is_lock(device)])
def is_lock(device):
"""Determine if the device supports the lock capability."""
from pysmartthings import Capability
return Capability.lock in device.capabilities
class SmartThingsLock(SmartThingsEntity, LockDevice):
"""Define a SmartThings lock."""
async def async_lock(self, **kwargs):
"""Lock the device."""
await self._device.lock(set_status=True)
self.async_schedule_update_ha_state()
async def async_unlock(self, **kwargs):
"""Unlock the device."""
await self._device.unlock(set_status=True)
self.async_schedule_update_ha_state()
@property
def is_locked(self):
"""Return true if lock is locked."""
return self._device.status.lock == ST_STATE_LOCKED
@property
def device_state_attributes(self):
"""Return device specific state attributes."""
from pysmartthings import Attribute
state_attrs = {}
status = self._device.status.attributes[Attribute.lock]
if status.value:
state_attrs['lock_state'] = status.value
if isinstance(status.data, dict):
for st_attr, ha_attr in ST_LOCK_ATTR_MAP.items():
data_val = status.data.get(st_attr)
if data_val is not None:
state_attrs[ha_attr] = data_val
return state_attrs