Migrate to voluptuous (#3292)

This commit is contained in:
Fabian Affolter 2016-09-11 09:22:49 +02:00 committed by GitHub
parent f341974b8b
commit ac9151af54

View File

@ -7,31 +7,62 @@ https://home-assistant.io/components/tellduslive/
import logging import logging
from datetime import timedelta from datetime import timedelta
from homeassistant.helpers import validate_config, discovery import voluptuous as vol
from homeassistant.util import Throttle
DOMAIN = "tellduslive" from homeassistant.helpers import discovery
from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
DOMAIN = 'tellduslive'
REQUIREMENTS = ['tellive-py==0.5.2'] REQUIREMENTS = ['tellive-py==0.5.2']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_PUBLIC_KEY = "public_key" CONF_PUBLIC_KEY = 'public_key'
CONF_PRIVATE_KEY = "private_key" CONF_PRIVATE_KEY = 'private_key'
CONF_TOKEN = "token" CONF_TOKEN = 'token'
CONF_TOKEN_SECRET = "token_secret" CONF_TOKEN_SECRET = 'token_secret'
MIN_TIME_BETWEEN_SWITCH_UPDATES = timedelta(minutes=1) MIN_TIME_BETWEEN_SWITCH_UPDATES = timedelta(minutes=1)
MIN_TIME_BETWEEN_SENSOR_UPDATES = timedelta(minutes=5) MIN_TIME_BETWEEN_SENSOR_UPDATES = timedelta(minutes=5)
NETWORK = None 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) @Throttle(MIN_TIME_BETWEEN_SWITCH_UPDATES)
def request_switches(): def request_switches():
"""Make request to online service.""" """Make request to online service."""
_LOGGER.debug("Updating switches from Telldus Live") _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. # Filter out any group of switches.
switches = {switch["id"]: switch for switch in switches switches = {switch["id"]: switch for switch in switches
if switch["type"] == "device"} if switch["type"] == "device"}
@ -42,11 +73,11 @@ def request_switches():
def request_sensors(): def request_sensors():
"""Make request to online service.""" """Make request to online service."""
_LOGGER.debug("Updating sensors from Telldus Live") _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. # 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 unit in units
for sensor in unit["data"]} for sensor in unit['data']}
return sensors return sensors
@ -68,10 +99,9 @@ class TelldusLiveData(object):
self._hass = hass self._hass = hass
self._config = config self._config = config
self._client = LiveClient(public_key=public_key, self._client = LiveClient(
private_key=private_key, public_key=public_key, private_key=private_key, access_token=token,
access_token=token, access_secret=token_secret)
access_secret=token_secret)
def validate_session(self): def validate_session(self):
"""Make a dummy request to see if the session is valid.""" """Make a dummy request to see if the session is valid."""
@ -112,9 +142,9 @@ class TelldusLiveData(object):
# | const.TELLSTICK_STOP # | const.TELLSTICK_STOP
default_params = {'supportedMethods': supported_methods, default_params = {'supportedMethods': supported_methods,
"includeValues": 1, 'includeValues': 1,
"includeScale": 1, 'includeScale': 1,
"includeIgnored": 0} 'includeIgnored': 0}
params.update(default_params) params.update(default_params)
# room for improvement: the telllive library doesn't seem to # room for improvement: the telllive library doesn't seem to
@ -149,15 +179,13 @@ class TelldusLiveData(object):
def update_sensors(self): def update_sensors(self):
"""Update local list of sensors.""" """Update local list of sensors."""
self._sensors = self.update_devices(self._sensors, self._sensors = self.update_devices(
request_sensors(), self._sensors, request_sensors(), 'sensor')
"sensor")
def update_switches(self): def update_switches(self):
"""Update local list of switches.""" """Update local list of switches."""
self._switches = self.update_devices(self._switches, self._switches = self.update_devices(
request_switches(), self._switches, request_switches(), 'switch')
"switch")
def _check_request(self, what, **params): def _check_request(self, what, **params):
"""Make request, check result if successful.""" """Make request, check result if successful."""
@ -174,42 +202,12 @@ class TelldusLiveData(object):
def turn_switch_on(self, switch_id): def turn_switch_on(self, switch_id):
"""Turn switch off.""" """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 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): def turn_switch_off(self, switch_id):
"""Turn switch on.""" """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 from tellive.live import const
self.get_switch(switch_id)["state"] = const.TELLSTICK_TURNOFF 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