core/homeassistant/components/wink.py

171 lines
5.5 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-09-11 09:19:10 +00:00
import voluptuous as vol
from homeassistant.helpers import discovery
from homeassistant.const import (
CONF_ACCESS_TOKEN, ATTR_BATTERY_LEVEL, CONF_EMAIL, CONF_PASSWORD,
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
2016-06-29 21:16:53 +00:00
from homeassistant.helpers.entity import Entity
2016-09-11 09:19:10 +00:00
import homeassistant.helpers.config_validation as cv
2016-12-04 05:39:48 +00:00
REQUIREMENTS = ['python-wink==0.11.0', 'pubnubsub-handler==0.0.5']
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
CONF_CLIENT_ID = 'client_id'
CONF_CLIENT_SECRET = 'client_secret'
CONF_USER_AGENT = 'user_agent'
CONF_OATH = 'oath'
CONF_DEFINED_BOTH_MSG = 'Remove access token to use oath2.'
CONF_MISSING_OATH_MSG = 'Missing oath2 credentials.'
2016-09-11 09:19:10 +00:00
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Inclusive(CONF_EMAIL, CONF_OATH,
msg=CONF_MISSING_OATH_MSG): cv.string,
vol.Inclusive(CONF_PASSWORD, CONF_OATH,
msg=CONF_MISSING_OATH_MSG): cv.string,
vol.Inclusive(CONF_CLIENT_ID, CONF_OATH,
msg=CONF_MISSING_OATH_MSG): cv.string,
vol.Inclusive(CONF_CLIENT_SECRET, CONF_OATH,
msg=CONF_MISSING_OATH_MSG): cv.string,
vol.Exclusive(CONF_EMAIL, CONF_OATH,
msg=CONF_DEFINED_BOTH_MSG): cv.string,
vol.Exclusive(CONF_ACCESS_TOKEN, CONF_OATH,
msg=CONF_DEFINED_BOTH_MSG): cv.string,
vol.Optional(CONF_USER_AGENT, default=None): cv.string
})
2016-09-11 09:19:10 +00:00
}, extra=vol.ALLOW_EXTRA)
WINK_COMPONENTS = [
'binary_sensor', 'sensor', 'light', 'switch', 'lock', 'cover', 'climate'
]
2016-09-11 09:19:10 +00:00
def setup(hass, config):
"""Set up the Wink component."""
import pywink
from pubnubsubhandler import PubNubSubscriptionHandler
user_agent = config[DOMAIN].get(CONF_USER_AGENT)
if user_agent:
pywink.set_user_agent(user_agent)
access_token = config[DOMAIN].get(CONF_ACCESS_TOKEN)
if access_token:
pywink.set_bearer_token(access_token)
else:
email = config[DOMAIN][CONF_EMAIL]
password = config[DOMAIN][CONF_PASSWORD]
client_id = config[DOMAIN]['client_id']
client_secret = config[DOMAIN]['client_secret']
pywink.set_wink_credentials(email, password, client_id,
client_secret)
hass.data[DOMAIN] = {}
hass.data[DOMAIN]['entities'] = []
hass.data[DOMAIN]['pubnub'] = PubNubSubscriptionHandler(
pywink.get_subscription_key(),
pywink.wink_api_fetch)
def start_subscription(event):
"""Start the pubnub subscription."""
hass.data[DOMAIN]['pubnub'].subscribe()
hass.bus.listen(EVENT_HOMEASSISTANT_START, start_subscription)
def stop_subscription(event):
"""Stop the pubnub subscription."""
hass.data[DOMAIN]['pubnub'].unsubscribe()
hass.bus.listen(EVENT_HOMEASSISTANT_STOP, stop_subscription)
def force_update(call):
"""Force all devices to poll the Wink API."""
_LOGGER.info("Refreshing Wink states from API")
for entity in hass.data[DOMAIN]['entities']:
entity.update_ha_state(True)
hass.services.register(DOMAIN, 'Refresh state from Wink', force_update)
2016-09-20 07:05:54 +00:00
# Load components for the devices in Wink that we support
for component in WINK_COMPONENTS:
discovery.load_platform(hass, component, 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
def __init__(self, wink, hass):
2016-03-08 16:55:57 +00:00
"""Initialize the Wink device."""
2015-01-16 05:25:24 +00:00
self.wink = wink
self._battery = self.wink.battery_level
hass.data[DOMAIN]['pubnub'].add_subscription(
self.wink.pubnub_channel, self._pubnub_update)
hass.data[DOMAIN]['entities'].append(self)
def _pubnub_update(self, message):
try:
if message is None:
_LOGGER.error("Error on pubnub update for %s "
"polling API for current state", self.name)
self.update_ha_state(True)
else:
self.wink.pubnub_update(message)
self.update_ha_state()
except (ValueError, KeyError, AttributeError):
_LOGGER.error("Error in pubnub JSON for %s "
"polling API for current state", self.name)
self.update_ha_state(True)
2016-06-29 21:16:53 +00:00
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."""
if self.wink.battery_level is not None:
return self.wink.battery_level * 100