2015-01-11 07:47:23 +00:00
|
|
|
"""
|
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/
|
2015-01-11 07:47:23 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2017-05-04 20:17:35 +00:00
|
|
|
import time
|
|
|
|
import json
|
|
|
|
from datetime import timedelta
|
2015-01-11 07:47:23 +00:00
|
|
|
|
2016-09-11 09:19:10 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.helpers import discovery
|
2017-05-04 20:17:35 +00:00
|
|
|
from homeassistant.helpers.event import track_time_interval
|
2016-12-03 19:46:04 +00:00
|
|
|
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
|
2015-01-11 07:47:23 +00:00
|
|
|
|
2017-05-04 20:17:35 +00:00
|
|
|
REQUIREMENTS = ['python-wink==1.2.4', 'pubnubsub-handler==1.0.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 = []
|
2015-01-11 07:47:23 +00:00
|
|
|
|
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
|
2016-10-02 03:45:39 +00:00
|
|
|
CONF_CLIENT_ID = 'client_id'
|
|
|
|
CONF_CLIENT_SECRET = 'client_secret'
|
|
|
|
CONF_USER_AGENT = 'user_agent'
|
|
|
|
CONF_OATH = 'oath'
|
2017-01-25 05:11:18 +00:00
|
|
|
CONF_APPSPOT = 'appspot'
|
2016-10-02 03:45:39 +00:00
|
|
|
CONF_DEFINED_BOTH_MSG = 'Remove access token to use oath2.'
|
|
|
|
CONF_MISSING_OATH_MSG = 'Missing oath2 credentials.'
|
2017-01-25 05:11:18 +00:00
|
|
|
CONF_TOKEN_URL = "https://winkbearertoken.appspot.com/token"
|
2016-09-11 09:19:10 +00:00
|
|
|
|
2017-05-04 20:17:35 +00:00
|
|
|
SERVICE_ADD_NEW_DEVICES = 'add_new_devices'
|
|
|
|
SERVICE_REFRESH_STATES = 'refresh_state_from_wink'
|
|
|
|
|
2016-09-11 09:19:10 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
2017-01-25 05:11:18 +00:00
|
|
|
vol.Inclusive(CONF_EMAIL, CONF_APPSPOT,
|
2016-10-02 03:45:39 +00:00
|
|
|
msg=CONF_MISSING_OATH_MSG): cv.string,
|
2017-01-25 05:11:18 +00:00
|
|
|
vol.Inclusive(CONF_PASSWORD, CONF_APPSPOT,
|
2016-10-02 03:45:39 +00:00
|
|
|
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,
|
2017-01-25 05:11:18 +00:00
|
|
|
vol.Exclusive(CONF_ACCESS_TOKEN, CONF_APPSPOT,
|
|
|
|
msg=CONF_DEFINED_BOTH_MSG): cv.string,
|
2016-10-02 03:45:39 +00:00
|
|
|
vol.Optional(CONF_USER_AGENT, default=None): cv.string
|
|
|
|
})
|
2016-09-11 09:19:10 +00:00
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
2015-01-11 07:47:23 +00:00
|
|
|
|
2016-10-22 20:59:20 +00:00
|
|
|
WINK_COMPONENTS = [
|
2017-01-14 06:08:13 +00:00
|
|
|
'binary_sensor', 'sensor', 'light', 'switch', 'lock', 'cover', 'climate',
|
2017-03-11 18:18:29 +00:00
|
|
|
'fan', 'alarm_control_panel', 'scene'
|
2016-10-22 20:59:20 +00:00
|
|
|
]
|
|
|
|
|
2015-01-11 07:47:23 +00:00
|
|
|
|
2016-09-11 09:19:10 +00:00
|
|
|
def setup(hass, config):
|
2016-12-03 19:46:04 +00:00
|
|
|
"""Set up the Wink component."""
|
2015-07-20 07:41:57 +00:00
|
|
|
import pywink
|
2017-01-25 05:11:18 +00:00
|
|
|
import requests
|
2016-11-30 21:12:26 +00:00
|
|
|
from pubnubsubhandler import PubNubSubscriptionHandler
|
2016-10-02 03:45:39 +00:00
|
|
|
|
2017-05-04 20:17:35 +00:00
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
hass.data[DOMAIN]['entities'] = []
|
|
|
|
hass.data[DOMAIN]['unique_ids'] = []
|
2017-05-13 18:09:00 +00:00
|
|
|
hass.data[DOMAIN]['entities'] = {}
|
2017-05-04 20:17:35 +00:00
|
|
|
|
2016-11-30 21:12:26 +00:00
|
|
|
user_agent = config[DOMAIN].get(CONF_USER_AGENT)
|
2016-10-02 03:45:39 +00:00
|
|
|
|
|
|
|
if user_agent:
|
|
|
|
pywink.set_user_agent(user_agent)
|
|
|
|
|
|
|
|
access_token = config[DOMAIN].get(CONF_ACCESS_TOKEN)
|
2017-01-25 05:11:18 +00:00
|
|
|
client_id = config[DOMAIN].get('client_id')
|
2016-10-02 03:45:39 +00:00
|
|
|
|
2017-05-04 20:17:35 +00:00
|
|
|
def _get_wink_token_from_web():
|
|
|
|
email = hass.data[DOMAIN]["oath"]["email"]
|
|
|
|
password = hass.data[DOMAIN]["oath"]["password"]
|
|
|
|
|
|
|
|
payload = {'username': email, 'password': password}
|
|
|
|
token_response = requests.post(CONF_TOKEN_URL, data=payload)
|
|
|
|
try:
|
|
|
|
token = token_response.text.split(':')[1].split()[0].rstrip('<br')
|
|
|
|
except IndexError:
|
|
|
|
_LOGGER.error("Error getting token. Please check email/password.")
|
|
|
|
return False
|
|
|
|
pywink.set_bearer_token(token)
|
|
|
|
|
2016-10-02 03:45:39 +00:00
|
|
|
if access_token:
|
|
|
|
pywink.set_bearer_token(access_token)
|
2017-01-25 05:11:18 +00:00
|
|
|
elif client_id:
|
2016-10-02 03:45:39 +00:00
|
|
|
email = config[DOMAIN][CONF_EMAIL]
|
|
|
|
password = config[DOMAIN][CONF_PASSWORD]
|
|
|
|
client_id = config[DOMAIN]['client_id']
|
|
|
|
client_secret = config[DOMAIN]['client_secret']
|
2017-05-04 20:17:35 +00:00
|
|
|
pywink.set_wink_credentials(email, password, client_id, client_secret)
|
|
|
|
hass.data[DOMAIN]['oath'] = {"email": email,
|
|
|
|
"password": password,
|
|
|
|
"client_id": client_id,
|
|
|
|
"client_secret": client_secret}
|
2017-01-25 05:11:18 +00:00
|
|
|
else:
|
|
|
|
email = config[DOMAIN][CONF_EMAIL]
|
|
|
|
password = config[DOMAIN][CONF_PASSWORD]
|
2017-05-04 20:17:35 +00:00
|
|
|
hass.data[DOMAIN]['oath'] = {"email": email, "password": password}
|
|
|
|
_get_wink_token_from_web()
|
2016-10-02 03:45:39 +00:00
|
|
|
|
2016-11-30 21:12:26 +00:00
|
|
|
hass.data[DOMAIN]['pubnub'] = PubNubSubscriptionHandler(
|
2017-05-04 20:17:35 +00:00
|
|
|
pywink.get_subscription_key())
|
|
|
|
|
|
|
|
def keep_alive_call(event_time):
|
|
|
|
"""Call the Wink API endpoints to keep PubNub working."""
|
|
|
|
_LOGGER.info("Getting a new Wink token.")
|
|
|
|
if hass.data[DOMAIN]["oath"].get("client_id") is not None:
|
|
|
|
_email = hass.data[DOMAIN]["oath"]["email"]
|
|
|
|
_password = hass.data[DOMAIN]["oath"]["password"]
|
|
|
|
_client_id = hass.data[DOMAIN]["oath"]["client_id"]
|
|
|
|
_client_secret = hass.data[DOMAIN]["oath"]["client_secret"]
|
|
|
|
pywink.set_wink_credentials(_email, _password, _client_id,
|
|
|
|
_client_secret)
|
|
|
|
else:
|
|
|
|
_LOGGER.info("Getting a new Wink token.")
|
|
|
|
_get_wink_token_from_web()
|
|
|
|
time.sleep(1)
|
|
|
|
_LOGGER.info("Polling the Wink API to keep PubNub updates flowing.")
|
|
|
|
_LOGGER.debug(str(json.dumps(pywink.wink_api_fetch())))
|
|
|
|
time.sleep(1)
|
|
|
|
_LOGGER.debug(str(json.dumps(pywink.get_user())))
|
|
|
|
|
|
|
|
# Call the Wink API every hour to keep PubNub updates flowing
|
|
|
|
if access_token is None:
|
|
|
|
track_time_interval(hass, keep_alive_call, timedelta(minutes=120))
|
2016-11-30 21:12:26 +00:00
|
|
|
|
|
|
|
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."""
|
2016-12-03 19:46:04 +00:00
|
|
|
_LOGGER.info("Refreshing Wink states from API")
|
2017-05-13 18:09:00 +00:00
|
|
|
for entity_list in hass.data[DOMAIN]['entities'].values():
|
2017-05-04 20:17:35 +00:00
|
|
|
# Throttle the calls to Wink API
|
2017-05-13 18:09:00 +00:00
|
|
|
for entity in entity_list:
|
|
|
|
time.sleep(1)
|
|
|
|
entity.schedule_update_ha_state(True)
|
2017-05-04 20:17:35 +00:00
|
|
|
hass.services.register(DOMAIN, SERVICE_REFRESH_STATES, force_update)
|
2015-01-11 07:47:23 +00:00
|
|
|
|
2017-02-02 06:43:12 +00:00
|
|
|
def pull_new_devices(call):
|
|
|
|
"""Pull new devices added to users Wink account since startup."""
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.info("Getting new devices from Wink API")
|
2017-02-02 06:43:12 +00:00
|
|
|
for component in WINK_COMPONENTS:
|
|
|
|
discovery.load_platform(hass, component, DOMAIN, {}, config)
|
2017-05-04 20:17:35 +00:00
|
|
|
hass.services.register(DOMAIN, SERVICE_ADD_NEW_DEVICES, pull_new_devices)
|
2017-02-02 06:43:12 +00:00
|
|
|
|
2016-09-20 07:05:54 +00:00
|
|
|
# Load components for the devices in Wink that we support
|
2016-10-22 20:59:20 +00:00
|
|
|
for component in WINK_COMPONENTS:
|
2017-05-13 18:09:00 +00:00
|
|
|
hass.data[DOMAIN]['entities'][component] = []
|
2016-10-22 20:59:20 +00:00
|
|
|
discovery.load_platform(hass, component, DOMAIN, {}, config)
|
2016-11-30 21:12:26 +00:00
|
|
|
|
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
|
|
|
|
2016-11-30 21:12:26 +00:00
|
|
|
def __init__(self, wink, hass):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Initialize the Wink device."""
|
2017-01-25 05:11:18 +00:00
|
|
|
self.hass = hass
|
2015-01-16 05:25:24 +00:00
|
|
|
self.wink = wink
|
2016-11-30 21:12:26 +00:00
|
|
|
hass.data[DOMAIN]['pubnub'].add_subscription(
|
2016-12-03 19:46:04 +00:00
|
|
|
self.wink.pubnub_channel, self._pubnub_update)
|
2017-02-02 06:43:12 +00:00
|
|
|
hass.data[DOMAIN]['unique_ids'].append(self.wink.object_id() +
|
|
|
|
self.wink.name())
|
2016-11-30 21:12:26 +00:00
|
|
|
|
|
|
|
def _pubnub_update(self, message):
|
2016-11-06 15:27:15 +00:00
|
|
|
try:
|
2016-11-30 21:12:26 +00:00
|
|
|
if message is None:
|
2016-12-03 19:46:04 +00:00
|
|
|
_LOGGER.error("Error on pubnub update for %s "
|
|
|
|
"polling API for current state", self.name)
|
2017-03-04 23:10:36 +00:00
|
|
|
self.schedule_update_ha_state(True)
|
2016-11-30 21:12:26 +00:00
|
|
|
else:
|
|
|
|
self.wink.pubnub_update(message)
|
2017-03-04 23:10:36 +00:00
|
|
|
self.schedule_update_ha_state()
|
2016-11-30 21:12:26 +00:00
|
|
|
except (ValueError, KeyError, AttributeError):
|
2016-12-03 19:46:04 +00:00
|
|
|
_LOGGER.error("Error in pubnub JSON for %s "
|
|
|
|
"polling API for current state", self.name)
|
2017-03-04 23:10:36 +00:00
|
|
|
self.schedule_update_ha_state(True)
|
2016-06-29 21:16:53 +00:00
|
|
|
|
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()
|
|
|
|
|
2016-03-15 11:29:49 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Return true if connection == True."""
|
2017-01-25 05:11:18 +00:00
|
|
|
return self.wink.available()
|
2016-03-15 11:29:49 +00:00
|
|
|
|
2015-01-16 05:25:24 +00:00
|
|
|
def update(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Update state of the device."""
|
2015-12-16 18:32:38 +00:00
|
|
|
self.wink.update_state()
|
2016-05-07 01:19:37 +00:00
|
|
|
|
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
|
|
|
|
|
2016-05-07 01:19:37 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
2017-02-02 06:43:12 +00:00
|
|
|
attributes = {}
|
2017-02-19 04:00:27 +00:00
|
|
|
battery = self._battery_level
|
|
|
|
if battery:
|
|
|
|
attributes[ATTR_BATTERY_LEVEL] = battery
|
|
|
|
man_dev_model = self._manufacturer_device_model
|
|
|
|
if man_dev_model:
|
|
|
|
attributes["manufacturer_device_model"] = man_dev_model
|
|
|
|
man_dev_id = self._manufacturer_device_id
|
|
|
|
if man_dev_id:
|
|
|
|
attributes["manufacturer_device_id"] = man_dev_id
|
|
|
|
dev_man = self._device_manufacturer
|
|
|
|
if dev_man:
|
|
|
|
attributes["device_manufacturer"] = dev_man
|
|
|
|
model_name = self._model_name
|
|
|
|
if model_name:
|
|
|
|
attributes["model_name"] = model_name
|
|
|
|
tamper = self._tamper
|
|
|
|
if tamper is not None:
|
|
|
|
attributes["tamper_detected"] = tamper
|
2017-02-02 06:43:12 +00:00
|
|
|
return attributes
|
2016-05-07 01:19:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _battery_level(self):
|
|
|
|
"""Return the battery level."""
|
2017-01-25 05:11:18 +00:00
|
|
|
if self.wink.battery_level() is not None:
|
|
|
|
return self.wink.battery_level() * 100
|
2017-02-02 06:43:12 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _manufacturer_device_model(self):
|
|
|
|
"""Return the manufacturer device model."""
|
|
|
|
return self.wink.manufacturer_device_model()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _manufacturer_device_id(self):
|
|
|
|
"""Return the manufacturer device id."""
|
|
|
|
return self.wink.manufacturer_device_id()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _device_manufacturer(self):
|
|
|
|
"""Return the device manufacturer."""
|
|
|
|
return self.wink.device_manufacturer()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _model_name(self):
|
|
|
|
"""Return the model name."""
|
|
|
|
return self.wink.model_name()
|
2017-02-19 04:00:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _tamper(self):
|
|
|
|
"""Return the devices tamper status."""
|
|
|
|
if hasattr(self.wink, 'tamper_detected'):
|
|
|
|
return self.wink.tamper_detected()
|
|
|
|
else:
|
|
|
|
return None
|