Skip to content

Commit

Permalink
Merge pull request #2890 from home-assistant/loop_energy_voluptuous
Browse files Browse the repository at this point in the history
Add voluptuous to Loopenergy
  • Loading branch information
pavoni authored Aug 20, 2016
2 parents 8eb66ac + d1a31b3 commit 2df8524
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']

CONF_ELEC = 'electricity'
CONF_GAS = 'gas'

CONF_ELEC_SERIAL = 'electricity_serial'
CONF_ELEC_SECRET = 'electricity_secret'

CONF_GAS_SERIAL = 'gas_serial'
CONF_GAS_SECRET = 'gas_secret'
CONF_GAS_CALORIFIC = 'gas_calorific'

CONF_GAS_TYPE = 'gas_type'

ELEC_SCHEMA = vol.Schema({
vol.Required(CONF_ELEC_SERIAL): cv.string,
vol.Required(CONF_ELEC_SECRET): cv.string,
})

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

GAS_SCHEMA = vol.Schema({
vol.Required(CONF_GAS_SERIAL): cv.string,
vol.Required(CONF_GAS_SECRET): cv.string,
vol.Optional(CONF_GAS_TYPE, default='metric'):
GAS_TYPE_SCHEMA,
vol.Optional(CONF_GAS_CALORIFIC, default=39.11): vol.Coerce(float)
})

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_ELEC): vol.All(
dict, ELEC_SCHEMA),
vol.Optional(CONF_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(CONF_ELEC)
gas_config = config.get(CONF_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(CONF_ELEC_SERIAL),
elec_config.get(CONF_ELEC_SECRET),
gas_config.get(CONF_GAS_SERIAL),
gas_config.get(CONF_GAS_SECRET),
gas_config.get(CONF_GAS_TYPE),
gas_config.get(CONF_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(CONF_GAS_SERIAL):
sensors.append(LoopEnergyGas(controller))

add_devices(sensors)
Expand Down

0 comments on commit 2df8524

Please sign in to comment.