2015-11-17 15:17:57 +00:00
|
|
|
"""
|
|
|
|
Support for Wink locks.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/lock.wink/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2015-11-19 21:54:55 +00:00
|
|
|
from homeassistant.components.lock import LockDevice
|
2016-05-07 01:19:37 +00:00
|
|
|
from homeassistant.const import CONF_ACCESS_TOKEN, ATTR_BATTERY_LEVEL
|
2015-11-17 15:17:57 +00:00
|
|
|
|
2016-05-07 01:19:37 +00:00
|
|
|
REQUIREMENTS = ['python-wink==0.7.6']
|
2015-11-17 15:17:57 +00:00
|
|
|
|
2015-12-17 03:45:58 +00:00
|
|
|
|
2015-11-17 15:17:57 +00:00
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Setup the Wink platform."""
|
2015-11-17 15:17:57 +00:00
|
|
|
import pywink
|
|
|
|
|
|
|
|
if discovery_info is None:
|
|
|
|
token = config.get(CONF_ACCESS_TOKEN)
|
|
|
|
|
|
|
|
if token is None:
|
|
|
|
logging.getLogger(__name__).error(
|
|
|
|
"Missing wink access_token. "
|
|
|
|
"Get one at https://winkbearertoken.appspot.com/")
|
|
|
|
return
|
|
|
|
|
|
|
|
pywink.set_bearer_token(token)
|
|
|
|
|
|
|
|
add_devices(WinkLockDevice(lock) for lock in pywink.get_locks())
|
|
|
|
|
|
|
|
|
2015-11-19 21:54:55 +00:00
|
|
|
class WinkLockDevice(LockDevice):
|
2016-03-07 21:13:18 +00:00
|
|
|
"""Representation of a Wink lock."""
|
|
|
|
|
2015-11-17 15:17:57 +00:00
|
|
|
def __init__(self, wink):
|
2016-03-07 21:13:18 +00:00
|
|
|
"""Initialize the lock."""
|
2015-11-17 15:17:57 +00:00
|
|
|
self.wink = wink
|
2016-05-07 01:19:37 +00:00
|
|
|
self._battery = self.wink.battery_level
|
2015-11-17 15:17:57 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Return the id of this wink lock."""
|
2015-12-16 18:32:38 +00:00
|
|
|
return "{}.{}".format(self.__class__, self.wink.device_id())
|
2015-11-17 15:17:57 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Return the name of the lock if any."""
|
2015-11-17 15:17:57 +00:00
|
|
|
return self.wink.name()
|
|
|
|
|
|
|
|
def update(self):
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Update the state of the lock."""
|
2015-12-16 18:32:38 +00:00
|
|
|
self.wink.update_state()
|
2015-11-17 15:17:57 +00:00
|
|
|
|
|
|
|
@property
|
2015-11-20 21:34:27 +00:00
|
|
|
def is_locked(self):
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Return true if device is locked."""
|
2015-11-17 15:17:57 +00:00
|
|
|
return self.wink.state()
|
|
|
|
|
2016-03-15 11:29:49 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""True if connection == True."""
|
|
|
|
return self.wink.available
|
|
|
|
|
2016-02-17 10:50:36 +00:00
|
|
|
def lock(self, **kwargs):
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Lock the device."""
|
2015-12-16 18:32:38 +00:00
|
|
|
self.wink.set_state(True)
|
2015-11-17 15:17:57 +00:00
|
|
|
|
2016-02-17 10:50:36 +00:00
|
|
|
def unlock(self, **kwargs):
|
2016-02-28 11:03:47 +00:00
|
|
|
"""Unlock the device."""
|
2015-12-16 18:32:38 +00:00
|
|
|
self.wink.set_state(False)
|
2016-05-07 01:19:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
if self._battery:
|
|
|
|
return {
|
|
|
|
ATTR_BATTERY_LEVEL: self._battery_level,
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _battery_level(self):
|
|
|
|
"""Return the battery level."""
|
|
|
|
return self.wink.battery_level * 100
|