From 5a86c8f94731820fc26cacf1cccfff7a11df7b18 Mon Sep 17 00:00:00 2001 From: William Scanlon Date: Sat, 22 Jul 2017 19:44:27 -0400 Subject: [PATCH 1/4] Support for Wink local control --- homeassistant/components/wink.py | 38 ++++++++++++++++++++++---------- requirements_all.txt | 2 +- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/wink.py b/homeassistant/components/wink.py index 1783d4bd79af5c..d66676338ee3d8 100644 --- a/homeassistant/components/wink.py +++ b/homeassistant/components/wink.py @@ -25,7 +25,7 @@ from homeassistant.helpers.entity import Entity import homeassistant.helpers.config_validation as cv -REQUIREMENTS = ['python-wink==1.3.1', 'pubnubsub-handler==1.0.2'] +REQUIREMENTS = ['python-wink==1.4.0', 'pubnubsub-handler==1.0.2'] _LOGGER = logging.getLogger(__name__) @@ -36,10 +36,10 @@ CONF_CLIENT_ID = 'client_id' CONF_CLIENT_SECRET = 'client_secret' CONF_USER_AGENT = 'user_agent' -CONF_OATH = 'oath' +CONF_OAUTH = 'oauth' +CONF_LOCAL_CONTROL = 'local_control' CONF_APPSPOT = 'appspot' -CONF_DEFINED_BOTH_MSG = 'Remove access token to use oath2.' -CONF_MISSING_OATH_MSG = 'Missing oath2 credentials.' +CONF_MISSING_OAUTH_MSG = 'Missing oauth2 credentials.' CONF_TOKEN_URL = "https://winkbearertoken.appspot.com/token" ATTR_ACCESS_TOKEN = 'access_token' @@ -64,15 +64,14 @@ CONFIG_SCHEMA = vol.Schema({ DOMAIN: vol.Schema({ vol.Inclusive(CONF_EMAIL, CONF_APPSPOT, - msg=CONF_MISSING_OATH_MSG): cv.string, + msg=CONF_MISSING_OAUTH_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, + msg=CONF_MISSING_OAUTH_MSG): cv.string, + vol.Inclusive(CONF_CLIENT_ID, CONF_OAUTH, + msg=CONF_MISSING_OAUTH_MSG): cv.string, + vol.Inclusive(CONF_CLIENT_SECRET, CONF_OAUTH, + msg=CONF_MISSING_OAUTH_MSG): cv.string, + vol.Optional(CONF_LOCAL_CONTROL, default=False): cv.boolean }) }, extra=vol.ALLOW_EXTRA) @@ -206,8 +205,11 @@ def _get_wink_token_from_web(): client_secret = config[DOMAIN].get(ATTR_CLIENT_SECRET) email = config[DOMAIN].get(CONF_EMAIL) password = config[DOMAIN].get(CONF_PASSWORD) + local_control = config[DOMAIN].get(CONF_LOCAL_CONTROL) if None not in [client_id, client_secret]: _LOGGER.info("Using legacy oauth authentication") + if not local_control: + pywink.disable_local_control() hass.data[DOMAIN]["oauth"]["client_id"] = client_id hass.data[DOMAIN]["oauth"]["client_secret"] = client_secret hass.data[DOMAIN]["oauth"]["email"] = email @@ -216,11 +218,14 @@ def _get_wink_token_from_web(): client_id, client_secret) elif None not in [email, password]: _LOGGER.info("Using web form authentication") + pywink.disable_local_contro() hass.data[DOMAIN]["oauth"]["email"] = email hass.data[DOMAIN]["oauth"]["password"] = password _get_wink_token_from_web() else: _LOGGER.info("Using new oauth authentication") + if not local_control: + pywink.disable_local_control() config_path = hass.config.path(WINK_CONFIG_FILE) if os.path.isfile(config_path): config_file = _read_config_file(config_path) @@ -304,6 +309,15 @@ def stop_subscription(event): hass.bus.listen(EVENT_HOMEASSISTANT_STOP, stop_subscription) + def save_credentials(event): + """Save currently set oauth credentials.""" + if hass.data[DOMAIN]["oauth"].get("email") is None: + config_path = hass.config.path(WINK_CONFIG_FILE) + _config = pywink.get_current_oauth_credentials() + _write_config_file(config_path, _config) + + hass.bus.listen(EVENT_HOMEASSISTANT_STOP, save_credentials) + def force_update(call): """Force all devices to poll the Wink API.""" _LOGGER.info("Refreshing Wink states from API") diff --git a/requirements_all.txt b/requirements_all.txt index 5bce15dbfd29b9..81c0f908d6f52e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -768,7 +768,7 @@ python-velbus==2.0.11 python-vlc==1.1.2 # homeassistant.components.wink -python-wink==1.3.1 +python-wink==1.4.0 # homeassistant.components.zwave python_openzwave==0.4.0.31 From db3b46705fb7c3797cea8e0b8a28492a36fc49b0 Mon Sep 17 00:00:00 2001 From: William Scanlon Date: Sat, 22 Jul 2017 23:00:20 -0400 Subject: [PATCH 2/4] Fixed typo --- homeassistant/components/wink.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/wink.py b/homeassistant/components/wink.py index d66676338ee3d8..480322c42812c2 100644 --- a/homeassistant/components/wink.py +++ b/homeassistant/components/wink.py @@ -218,7 +218,7 @@ def _get_wink_token_from_web(): client_id, client_secret) elif None not in [email, password]: _LOGGER.info("Using web form authentication") - pywink.disable_local_contro() + pywink.disable_local_control() hass.data[DOMAIN]["oauth"]["email"] = email hass.data[DOMAIN]["oauth"]["password"] = password _get_wink_token_from_web() From 2df2c4be7f447fee59f425f4d9b433e834d63e8f Mon Sep 17 00:00:00 2001 From: William Scanlon Date: Fri, 28 Jul 2017 08:46:33 -0400 Subject: [PATCH 3/4] Forward failed local API requests online --- homeassistant/components/wink.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/wink.py b/homeassistant/components/wink.py index 480322c42812c2..e6d60aa55687dc 100644 --- a/homeassistant/components/wink.py +++ b/homeassistant/components/wink.py @@ -25,7 +25,7 @@ from homeassistant.helpers.entity import Entity import homeassistant.helpers.config_validation as cv -REQUIREMENTS = ['python-wink==1.4.0', 'pubnubsub-handler==1.0.2'] +REQUIREMENTS = ['python-wink==1.4.2', 'pubnubsub-handler==1.0.2'] _LOGGER = logging.getLogger(__name__) From 0ce2b181ab0f4ba77dc06fbf818133c87635b25d Mon Sep 17 00:00:00 2001 From: William Scanlon Date: Fri, 28 Jul 2017 09:26:47 -0400 Subject: [PATCH 4/4] Updated requirements_all.txt --- requirements_all.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements_all.txt b/requirements_all.txt index 81c0f908d6f52e..84e3618d2a03fa 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -768,7 +768,7 @@ python-velbus==2.0.11 python-vlc==1.1.2 # homeassistant.components.wink -python-wink==1.4.0 +python-wink==1.4.2 # homeassistant.components.zwave python_openzwave==0.4.0.31