Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication in the UI #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down Expand Up @@ -53,6 +53,12 @@
key_language=CONF_LANGUAGE,
domain=DOMAIN)

LOGIN_PROMPT = (
'Please <a href="{url}">log in to SmartThinQ</a>. '
'The site will redirect you to a blank page. '
'Paste that URL here.'
)


def setup(hass, config):
if DOMAIN not in config:
Expand All @@ -62,22 +68,50 @@ 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()
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)

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):
Expand Down