Skip to content

Commit

Permalink
Migrate to voluptuous (#3292)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabaff authored Sep 11, 2016
1 parent f341974 commit ac9151a
Showing 1 changed file with 55 additions and 57 deletions.
112 changes: 55 additions & 57 deletions homeassistant/components/tellduslive.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,62 @@
import logging
from datetime import timedelta

from homeassistant.helpers import validate_config, discovery
import voluptuous as vol

from homeassistant.helpers import discovery
from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv

DOMAIN = "tellduslive"
DOMAIN = 'tellduslive'

REQUIREMENTS = ['tellive-py==0.5.2']

_LOGGER = logging.getLogger(__name__)

CONF_PUBLIC_KEY = "public_key"
CONF_PRIVATE_KEY = "private_key"
CONF_TOKEN = "token"
CONF_TOKEN_SECRET = "token_secret"
CONF_PUBLIC_KEY = 'public_key'
CONF_PRIVATE_KEY = 'private_key'
CONF_TOKEN = 'token'
CONF_TOKEN_SECRET = 'token_secret'

MIN_TIME_BETWEEN_SWITCH_UPDATES = timedelta(minutes=1)
MIN_TIME_BETWEEN_SENSOR_UPDATES = timedelta(minutes=5)

NETWORK = None

CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_PUBLIC_KEY): cv.string,
vol.Required(CONF_PRIVATE_KEY): cv.string,
vol.Required(CONF_TOKEN): cv.string,
vol.Required(CONF_TOKEN_SECRET): cv.string,
}),
}, extra=vol.ALLOW_EXTRA)


def setup(hass, config):
"""Setup the Telldus Live component."""
# fixme: aquire app key and provide authentication using username+password

global NETWORK
NETWORK = TelldusLiveData(hass, config)

if not NETWORK.validate_session():
_LOGGER.error(
"Authentication Error: "
"Please make sure you have configured your keys "
"that can be aquired from https://api.telldus.com/keys/index")
return False

NETWORK.discover()

return True


@Throttle(MIN_TIME_BETWEEN_SWITCH_UPDATES)
def request_switches():
"""Make request to online service."""
_LOGGER.debug("Updating switches from Telldus Live")
switches = NETWORK.request("devices/list")["device"]
switches = NETWORK.request('devices/list')['device']
# Filter out any group of switches.
switches = {switch["id"]: switch for switch in switches
if switch["type"] == "device"}
Expand All @@ -42,11 +73,11 @@ def request_switches():
def request_sensors():
"""Make request to online service."""
_LOGGER.debug("Updating sensors from Telldus Live")
units = NETWORK.request("sensors/list")["sensor"]
units = NETWORK.request('sensors/list')['sensor']
# One unit can contain many sensors.
sensors = {unit["id"]+sensor["name"]: dict(unit, data=sensor)
sensors = {unit['id']+sensor['name']: dict(unit, data=sensor)
for unit in units
for sensor in unit["data"]}
for sensor in unit['data']}
return sensors


Expand All @@ -68,10 +99,9 @@ def __init__(self, hass, config):
self._hass = hass
self._config = config

self._client = LiveClient(public_key=public_key,
private_key=private_key,
access_token=token,
access_secret=token_secret)
self._client = LiveClient(
public_key=public_key, private_key=private_key, access_token=token,
access_secret=token_secret)

def validate_session(self):
"""Make a dummy request to see if the session is valid."""
Expand Down Expand Up @@ -112,9 +142,9 @@ def request(self, what, **params):
# | const.TELLSTICK_STOP

default_params = {'supportedMethods': supported_methods,
"includeValues": 1,
"includeScale": 1,
"includeIgnored": 0}
'includeValues': 1,
'includeScale': 1,
'includeIgnored': 0}
params.update(default_params)

# room for improvement: the telllive library doesn't seem to
Expand Down Expand Up @@ -149,15 +179,13 @@ def update_devices(self, local_devices, remote_devices, component_name):

def update_sensors(self):
"""Update local list of sensors."""
self._sensors = self.update_devices(self._sensors,
request_sensors(),
"sensor")
self._sensors = self.update_devices(
self._sensors, request_sensors(), 'sensor')

def update_switches(self):
"""Update local list of switches."""
self._switches = self.update_devices(self._switches,
request_switches(),
"switch")
self._switches = self.update_devices(
self._switches, request_switches(), 'switch')

def _check_request(self, what, **params):
"""Make request, check result if successful."""
Expand All @@ -174,42 +202,12 @@ def get_sensor(self, sensor_id):

def turn_switch_on(self, switch_id):
"""Turn switch off."""
if self._check_request("device/turnOn", id=switch_id):
if self._check_request('device/turnOn', id=switch_id):
from tellive.live import const
self.get_switch(switch_id)["state"] = const.TELLSTICK_TURNON
self.get_switch(switch_id)['state'] = const.TELLSTICK_TURNON

def turn_switch_off(self, switch_id):
"""Turn switch on."""
if self._check_request("device/turnOff", id=switch_id):
if self._check_request('device/turnOff', id=switch_id):
from tellive.live import const
self.get_switch(switch_id)["state"] = const.TELLSTICK_TURNOFF


def setup(hass, config):
"""Setup the Telldus Live component."""
# fixme: aquire app key and provide authentication using username+password
if not validate_config(config,
{DOMAIN: [CONF_PUBLIC_KEY,
CONF_PRIVATE_KEY,
CONF_TOKEN,
CONF_TOKEN_SECRET]},
_LOGGER):
_LOGGER.error(
"Configuration Error: "
"Please make sure you have configured your keys "
"that can be aquired from https://api.telldus.com/keys/index")
return False

global NETWORK
NETWORK = TelldusLiveData(hass, config)

if not NETWORK.validate_session():
_LOGGER.error(
"Authentication Error: "
"Please make sure you have configured your keys "
"that can be aquired from https://api.telldus.com/keys/index")
return False

NETWORK.discover()

return True
self.get_switch(switch_id)['state'] = const.TELLSTICK_TURNOFF

0 comments on commit ac9151a

Please sign in to comment.