mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
Merge pull request #2890 from home-assistant/loop_energy_voluptuous
Add voluptuous to Loopenergy
This commit is contained in:
commit
2df85242f9
@ -6,56 +6,67 @@ https://home-assistant.io/components/sensor.loop_energy/
|
||||
"""
|
||||
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):
|
||||
@ -67,7 +78,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
|
||||
sensors = [LoopEnergyElec(controller)]
|
||||
|
||||
if gas_serial:
|
||||
if gas_config.get(CONF_GAS_SERIAL):
|
||||
sensors.append(LoopEnergyGas(controller))
|
||||
|
||||
add_devices(sensors)
|
||||
|
Loading…
x
Reference in New Issue
Block a user