core/homeassistant/components/wink.py

124 lines
3.7 KiB
Python
Raw Normal View History

"""
2016-03-07 17:49:31 +00:00
Support for Wink hubs.
2015-10-23 20:32:36 +00:00
For more details about this component, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/wink/
"""
import logging
2016-06-29 21:16:53 +00:00
import json
2016-09-11 09:19:10 +00:00
import voluptuous as vol
from homeassistant.helpers import discovery
2016-06-29 21:16:53 +00:00
from homeassistant.const import CONF_ACCESS_TOKEN, ATTR_BATTERY_LEVEL
from homeassistant.helpers.entity import Entity
2016-09-11 09:19:10 +00:00
import homeassistant.helpers.config_validation as cv
2016-09-20 07:05:54 +00:00
REQUIREMENTS = ['python-wink==0.7.15', 'pubnub==3.8.2']
2016-06-29 21:16:53 +00:00
2016-09-11 09:19:10 +00:00
_LOGGER = logging.getLogger(__name__)
2016-06-29 21:16:53 +00:00
CHANNELS = []
2016-09-11 09:19:10 +00:00
DOMAIN = 'wink'
2015-01-20 04:23:31 +00:00
2016-09-11 09:19:10 +00:00
SUBSCRIPTION_HANDLER = None
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
2016-09-20 07:05:54 +00:00
vol.Required(CONF_ACCESS_TOKEN): cv.string,
2016-09-11 09:19:10 +00:00
}),
}, extra=vol.ALLOW_EXTRA)
2016-09-11 09:19:10 +00:00
def setup(hass, config):
"""Setup the Wink component."""
import pywink
2016-06-29 21:16:53 +00:00
from pubnub import Pubnub
pywink.set_bearer_token(config[DOMAIN][CONF_ACCESS_TOKEN])
2016-06-29 21:16:53 +00:00
global SUBSCRIPTION_HANDLER
2016-09-11 09:19:10 +00:00
SUBSCRIPTION_HANDLER = Pubnub(
'N/A', pywink.get_subscription_key(), ssl_on=True)
2016-06-29 21:16:53 +00:00
SUBSCRIPTION_HANDLER.set_heartbeat(120)
2016-09-20 07:05:54 +00:00
# Load components for the devices in Wink that we support
for component_name, func_exists in (
('light', pywink.get_bulbs),
('switch', lambda: pywink.get_switches or pywink.get_sirens or
pywink.get_powerstrip_outlets),
('binary_sensor', pywink.get_sensors),
('sensor', lambda: pywink.get_sensors or pywink.get_eggtrays),
('lock', pywink.get_locks),
('cover', pywink.get_shades),
('cover', pywink.get_garage_doors)):
if func_exists():
2016-06-14 03:06:24 +00:00
discovery.load_platform(hass, component_name, DOMAIN, {}, config)
2015-01-11 22:21:44 +00:00
return True
2015-01-16 05:25:24 +00:00
2015-01-20 04:23:31 +00:00
2016-06-29 21:16:53 +00:00
class WinkDevice(Entity):
2016-09-11 09:19:10 +00:00
"""Representation a base Wink device."""
2016-03-08 16:55:57 +00:00
2015-01-16 05:25:24 +00:00
def __init__(self, wink):
2016-03-08 16:55:57 +00:00
"""Initialize the Wink device."""
2016-06-29 21:16:53 +00:00
from pubnub import Pubnub
2015-01-16 05:25:24 +00:00
self.wink = wink
self._battery = self.wink.battery_level
2016-06-29 21:16:53 +00:00
if self.wink.pubnub_channel in CHANNELS:
2016-09-11 09:19:10 +00:00
pubnub = Pubnub('N/A', self.wink.pubnub_key, ssl_on=True)
2016-06-29 21:16:53 +00:00
pubnub.set_heartbeat(120)
pubnub.subscribe(self.wink.pubnub_channel,
self._pubnub_update,
error=self._pubnub_error)
else:
CHANNELS.append(self.wink.pubnub_channel)
SUBSCRIPTION_HANDLER.subscribe(self.wink.pubnub_channel,
self._pubnub_update,
error=self._pubnub_error)
def _pubnub_update(self, message, channel):
self.wink.pubnub_update(json.loads(message))
self.update_ha_state()
def _pubnub_error(self, message):
2016-09-11 09:19:10 +00:00
_LOGGER.error("Error on pubnub update for " + self.wink.name())
2015-01-16 05:25:24 +00:00
@property
def unique_id(self):
2016-03-07 17:49:31 +00:00
"""Return the ID of this Wink device."""
2016-09-11 09:19:10 +00:00
return '{}.{}'.format(self.__class__, self.wink.device_id())
2015-01-16 05:25:24 +00:00
@property
def name(self):
2016-03-07 17:49:31 +00:00
"""Return the name of the device."""
2015-01-16 05:25:24 +00:00
return self.wink.name()
@property
def available(self):
"""True if connection == True."""
return self.wink.available
2015-01-16 05:25:24 +00:00
def update(self):
2016-03-07 17:49:31 +00:00
"""Update state of the device."""
self.wink.update_state()
2016-06-29 21:16:53 +00:00
@property
def should_poll(self):
"""Only poll if we are not subscribed to pubnub."""
return self.wink.pubnub_channel is None
@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