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

[WIP] use v2 names, use state file #91

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,14 @@ You can also install it manually:
$ python3 example.py -c US -l en-US

For the `-c` and `-l` parameters, use your country and language code: SmartThinQ accounts are associated with a specific locale, so be sure to use the country you originally created your account with.
The script will ask you to open a browser, log in, and then paste the URL you're redirected to. It will then write a JSON file called `wideq_state.json`.

Look inside this file for a key called `"refresh_token"` and copy the value.
The script will ask you to open a browser, log in, and then paste the URL you're redirected to. It will then write a JSON file called `wideq_state.json`. Place the file in a directory accessible to Home Assistant.

3. Add a stanza to your Home Assistant `configuration.yaml` like this:

smartthinq:
token: [YOUR_TOKEN_HERE]
region: US
language: en-US
wideq_state: /config/wideq_state.json

Use your refresh token and country & language codes. If region and language are not provided, then 'US' and 'en-US' are default.
Start up Home Assistant and hope for the best.
Where `/config/wideq_state.json` is the path to the JSON file you copied.

Dishwasher Visualization Example
--------------------------------
Expand Down
50 changes: 31 additions & 19 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Support for LG Smartthinq devices.
"""
import logging
import os
import json
import wideq

import voluptuous as vol
Expand All @@ -14,11 +16,10 @@
DOMAIN = 'smartthinq'

CONF_LANGUAGE = 'language'
CONF_WIDEQ_STATE='wideq_state'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_TOKEN): cv.string,
CONF_REGION: cv.string,
CONF_LANGUAGE: cv.string,
vol.Required(CONF_WIDEQ_STATE): cv.string
})
}, extra=vol.ALLOW_EXTRA)

Expand Down Expand Up @@ -59,25 +60,36 @@ def setup(hass, config):
LOGGER.warning(DEPRECATION_WARNING)
return True


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)

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

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
state_file = config[DOMAIN].get(CONF_WIDEQ_STATE)

# Load the configuration from the state file
try:
with open(state_file) as f:
LOGGER.debug("State file found '%s'", os.path.abspath(state_file))
state = json.load(f)
except IOError:
state = {}
LOGGER.debug("No state file found (tried: '%s')",
os.path.abspath(state_file))

client = wideq.Client.load(state)
hass.data[CONF_WIDEQ_STATE] = state

while True:
try:
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
except wideq.NotLoggedInError:
LOGGER.info('Session expired.')
client.refresh()


class LGDevice(Entity):
Expand Down
18 changes: 6 additions & 12 deletions climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from homeassistant.components.climate import const as c_const
from custom_components.smartthinq import (
CONF_LANGUAGE, KEY_DEPRECATED_COUNTRY,
KEY_DEPRECATED_LANGUAGE, KEY_DEPRECATED_REFRESH_TOKEN)
KEY_DEPRECATED_LANGUAGE, KEY_DEPRECATED_REFRESH_TOKEN,
CONF_WIDEQ_STATE)

try:
from homeassistant.components.climate import ClimateEntity
Expand Down Expand Up @@ -70,16 +71,9 @@ def swing_modes_index():
def setup_platform(hass, config, add_devices, discovery_info=None):
import wideq

refresh_token = config.get(KEY_DEPRECATED_REFRESH_TOKEN) or \
hass.data.get(CONF_TOKEN)
country = config.get(KEY_DEPRECATED_COUNTRY) or \
hass.data.get(CONF_REGION)
language = config.get(KEY_DEPRECATED_LANGUAGE) or \
hass.data.get(CONF_LANGUAGE)

client = wideq.Client.load(hass.data[CONF_WIDEQ_STATE])
client.refresh()
fahrenheit = hass.config.units.temperature_unit != '°C'

client = wideq.Client.from_token(refresh_token, country, language)
add_devices(_ac_devices(hass, client, fahrenheit), True)


Expand Down Expand Up @@ -203,15 +197,15 @@ def hvac_modes(self):
import wideq
return [v for k, v in MODES.items()
if wideq.ACMode[k].value in
self._ac.model.value('SupportOpMode').options.values()] \
self._ac.model.value('airState.opMode').options.values()] \
+ [c_const.HVAC_MODE_OFF]

@property
def fan_modes(self):
import wideq
return [v for k, v in FAN_MODES.items()
if wideq.ACFanSpeed[k].value in
self._ac.model.value('SupportWindStrength').options.values()]
self._ac.model.value('airState.windStrength').options.values()]

@property
def swing_mode(self):
Expand Down
4 changes: 2 additions & 2 deletions sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the LG dishwasher entities"""

"""
refresh_token = hass.data[CONF_TOKEN]
region = hass.data[CONF_REGION]
language = hass.data[CONF_LANGUAGE]
Expand All @@ -56,7 +56,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):

if dishwashers:
add_entities(dishwashers, True)
return True
return True """
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this code still be commented?



class LGDishWasherDevice(LGDevice):
Expand Down