""" Support for Wink hubs. For more details about this component, please refer to the documentation at https://home-assistant.io/components/wink/ """ import logging import time import json from datetime import timedelta import voluptuous as vol from homeassistant.helpers import discovery from homeassistant.helpers.event import track_time_interval from homeassistant.const import ( CONF_ACCESS_TOKEN, ATTR_BATTERY_LEVEL, CONF_EMAIL, CONF_PASSWORD, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP) from homeassistant.helpers.entity import Entity import homeassistant.helpers.config_validation as cv REQUIREMENTS = ['python-wink==1.2.4', 'pubnubsub-handler==1.0.2'] _LOGGER = logging.getLogger(__name__) CHANNELS = [] DOMAIN = 'wink' SUBSCRIPTION_HANDLER = None CONF_CLIENT_ID = 'client_id' CONF_CLIENT_SECRET = 'client_secret' CONF_USER_AGENT = 'user_agent' CONF_OATH = 'oath' CONF_APPSPOT = 'appspot' CONF_DEFINED_BOTH_MSG = 'Remove access token to use oath2.' CONF_MISSING_OATH_MSG = 'Missing oath2 credentials.' CONF_TOKEN_URL = "https://winkbearertoken.appspot.com/token" SERVICE_ADD_NEW_DEVICES = 'add_new_devices' SERVICE_REFRESH_STATES = 'refresh_state_from_wink' CONFIG_SCHEMA = vol.Schema({ DOMAIN: vol.Schema({ vol.Inclusive(CONF_EMAIL, CONF_APPSPOT, msg=CONF_MISSING_OATH_MSG): cv.string, vol.Inclusive(CONF_PASSWORD, CONF_APPSPOT, 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.Exclusive(CONF_ACCESS_TOKEN, CONF_APPSPOT, msg=CONF_DEFINED_BOTH_MSG): cv.string, vol.Optional(CONF_USER_AGENT, default=None): cv.string }) }, extra=vol.ALLOW_EXTRA) WINK_COMPONENTS = [ 'binary_sensor', 'sensor', 'light', 'switch', 'lock', 'cover', 'climate', 'fan', 'alarm_control_panel', 'scene' ] def setup(hass, config): """Set up the Wink component.""" import pywink import requests from pubnubsubhandler import PubNubSubscriptionHandler hass.data[DOMAIN] = {} hass.data[DOMAIN]['entities'] = [] hass.data[DOMAIN]['unique_ids'] = [] hass.data[DOMAIN]['entities'] = {} 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) client_id = config[DOMAIN].get('client_id') 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('