From 7cf759cf173307a1daee30485e1a30a2458125b9 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 17 Dec 2019 17:17:30 -0500 Subject: [PATCH 1/2] Prototype URL-based login flow --- __init__.py | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/__init__.py b/__init__.py index d611675..45c1381 100644 --- a/__init__.py +++ b/__init__.py @@ -16,7 +16,7 @@ CONF_LANGUAGE = 'language' CONFIG_SCHEMA = vol.Schema({ DOMAIN: vol.Schema({ - vol.Required(CONF_TOKEN): cv.string, + CONF_TOKEN: cv.string, CONF_REGION: cv.string, CONF_LANGUAGE: cv.string, }) @@ -53,6 +53,12 @@ key_language=CONF_LANGUAGE, domain=DOMAIN) +LOGIN_PROMPT = ( + 'Please log in to SmartThinQ. ' + 'The site will redirect you to an error page. ' + 'Paste that URL here.' +) + def setup(hass, config): if DOMAIN not in config: @@ -62,22 +68,48 @@ def setup(hass, config): if KEY_SMARTTHINQ_DEVICES not in hass.data: hass.data[KEY_SMARTTHINQ_DEVICES] = [] - refresh_token = config[DOMAIN].get(CONF_TOKEN) - region = config[DOMAIN].get(CONF_REGION) - language = config[DOMAIN].get(CONF_LANGUAGE) + hass.data[CONF_TOKEN] = config[DOMAIN].get(CONF_TOKEN) + hass.data[CONF_REGION] = config[DOMAIN].get(CONF_REGION) + hass.data[CONF_LANGUAGE] = config[DOMAIN].get(CONF_LANGUAGE) + + if hass.data[CONF_TOKEN]: + finish_setup(hass, config) + else: + gateway = wideq.Gateway.discover( + hass.data[CONF_REGION], + hass.data[CONF_LANGUAGE], + ) + login_url = gateway.oauth_url() + + def setup_callback(data): + auth = wideq.Auth.from_url(gateway, data['url']) + hass.data[CONF_TOKEN] = auth.refresh_token + finish_setup(hass, config) + + configurator = hass.components.configurator + configurator.request_config( + 'SmartThinQ', + setup_callback, + description=LOGIN_PROMPT.format(url=login_url), + submit_caption='Save', + fields=[{"id": "url", "name": "URL", "type": ""}], + ) + + return True - client = wideq.Client.from_token(refresh_token, region, language) - hass.data[CONF_TOKEN] = refresh_token - hass.data[CONF_REGION] = region - hass.data[CONF_LANGUAGE] = language +def finish_setup(hass, config): + client = wideq.Client.from_token( + hass.data[CONF_TOKEN], + hass.data[CONF_REGION], + hass.data[CONF_LANGUAGE], + ) for device in client.devices: hass.data[KEY_SMARTTHINQ_DEVICES].append(device.id) for component in SMARTTHINQ_COMPONENTS: discovery.load_platform(hass, component, DOMAIN, {}, config) - return True class LGDevice(Entity): From a17091a724fd7f0c0fdb4296462740d174df4d45 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 17 Dec 2019 17:32:01 -0500 Subject: [PATCH 2/2] Debug messages --- __init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/__init__.py b/__init__.py index 45c1381..6db08e8 100644 --- a/__init__.py +++ b/__init__.py @@ -55,7 +55,7 @@ LOGIN_PROMPT = ( 'Please log in to SmartThinQ. ' - 'The site will redirect you to an error page. ' + 'The site will redirect you to a blank page. ' 'Paste that URL here.' ) @@ -80,9 +80,11 @@ def setup(hass, config): hass.data[CONF_LANGUAGE], ) login_url = gateway.oauth_url() + LOGGER.debug('Login URL: %s', login_url) def setup_callback(data): auth = wideq.Auth.from_url(gateway, data['url']) + LOGGER.debug('Found refresh token: %s', auth.refresh_token) hass.data[CONF_TOKEN] = auth.refresh_token finish_setup(hass, config)