Skip to content

Commit

Permalink
Add voluptuous
Browse files Browse the repository at this point in the history
.# Please enter the commit message for your changes. Lines starting
  • Loading branch information
pavoni committed Aug 19, 2016
1 parent c74e167 commit 337b2e3
Showing 1 changed file with 47 additions and 36 deletions.
83 changes: 47 additions & 36 deletions homeassistant/components/sensor/loopenergy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,67 @@
"""
import logging

import voluptuous as vol
import homeassistant.helpers.config_validation as cv

from homeassistant.helpers.entity import Entity
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.util import convert

_LOGGER = logging.getLogger(__name__)

DOMAIN = "loopenergy"

REQUIREMENTS = ['pyloopenergy==0.0.14']

CONFIG_ELEC = 'electricity'
CONFIG_GAS = 'gas'

CONFIG_ELEC_SERIAL = 'electricity_serial'
CONFIG_ELEC_SECRET = 'electricity_secret'

CONFIG_GAS_SERIAL = 'gas_serial'
CONFIG_GAS_SECRET = 'gas_secret'
CONFIG_GAS_CALORIFIC = 'gas_calorific'

CONFIG_GAS_TYPE = 'gas_type'

ELEC_SCHEMA = vol.Schema({
vol.Required(CONFIG_ELEC_SERIAL): cv.string,
vol.Required(CONFIG_ELEC_SECRET): cv.string,
})

GAS_TYPE_SCHEMA = vol.In(['imperial', 'metric'])

GAS_SCHEMA = vol.Schema({
vol.Required(CONFIG_GAS_SERIAL): cv.string,
vol.Required(CONFIG_GAS_SECRET): cv.string,
vol.Optional(CONFIG_GAS_TYPE, default='metric'):
GAS_TYPE_SCHEMA,
vol.Optional(CONFIG_GAS_CALORIFIC, default=39.11): vol.Coerce(float)
})

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONFIG_ELEC): vol.All(
dict, ELEC_SCHEMA),
vol.Optional(CONFIG_GAS, default={}): vol.All(
dict, GAS_SCHEMA)
})


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Loop Energy sensors."""
import pyloopenergy

elec_serial = config.get('electricity_serial')
elec_secret = config.get('electricity_secret')
gas_serial = config.get('gas_serial')
gas_secret = config.get('gas_secret')
gas_type = config.get('gas_type', 'metric')
gas_calorific = convert(config.get('gas_calorific'), float, 39.11)

if not (elec_serial and elec_secret):
_LOGGER.error(
"Configuration Error, "
"please make sure you have configured electricity "
"serial and secret tokens")
return None

if (gas_serial or gas_secret) and not (gas_serial and gas_secret):
_LOGGER.error(
"Configuration Error, "
"please make sure you have configured gas "
"serial and secret tokens")
return None

if gas_type not in ['imperial', 'metric']:
_LOGGER.error(
"Configuration Error, 'gas_type' "
"can only be 'imperial' or 'metric' ")
return None
elec_config = config.get(CONFIG_ELEC)
gas_config = config.get(CONFIG_GAS)

# pylint: disable=too-many-function-args
controller = pyloopenergy.LoopEnergy(
elec_serial,
elec_secret,
gas_serial,
gas_secret,
gas_type,
gas_calorific
elec_config.get(CONFIG_ELEC_SERIAL),
elec_config.get(CONFIG_ELEC_SECRET),
gas_config.get(CONFIG_GAS_SERIAL),
gas_config.get(CONFIG_GAS_SECRET),
gas_config.get(CONFIG_GAS_TYPE),
gas_config.get(CONFIG_GAS_CALORIFIC)
)

def stop_loopenergy(event):
Expand All @@ -67,7 +78,7 @@ def stop_loopenergy(event):

sensors = [LoopEnergyElec(controller)]

if gas_serial:
if gas_config.get(CONFIG_GAS_SERIAL):
sensors.append(LoopEnergyGas(controller))

add_devices(sensors)
Expand Down

0 comments on commit 337b2e3

Please sign in to comment.