2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Loop Energy sensors."""
|
2016-03-31 16:00:43 +00:00
|
|
|
import logging
|
|
|
|
|
2019-10-21 08:07:35 +00:00
|
|
|
import pyloopenergy
|
2016-08-19 19:07:09 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2018-06-19 07:56:29 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_UNIT_SYSTEM_IMPERIAL,
|
|
|
|
CONF_UNIT_SYSTEM_METRIC,
|
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
|
|
|
)
|
2018-06-19 07:56:29 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-03-31 16:00:43 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ELEC = "electricity"
|
|
|
|
CONF_GAS = "gas"
|
2016-08-19 19:07:09 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ELEC_SERIAL = "electricity_serial"
|
|
|
|
CONF_ELEC_SECRET = "electricity_secret"
|
2016-08-19 19:07:09 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_GAS_SERIAL = "gas_serial"
|
|
|
|
CONF_GAS_SECRET = "gas_secret"
|
|
|
|
CONF_GAS_CALORIFIC = "gas_calorific"
|
2016-08-19 19:07:09 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_GAS_TYPE = "gas_type"
|
2016-08-19 19:07:09 +00:00
|
|
|
|
2016-10-30 08:58:34 +00:00
|
|
|
DEFAULT_CALORIFIC = 39.11
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_UNIT = "kW"
|
2016-10-30 08:58:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ELEC_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ELEC_SERIAL): cv.string,
|
|
|
|
vol.Required(CONF_ELEC_SECRET): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-08-19 19:07:09 +00:00
|
|
|
|
2016-10-30 08:58:34 +00:00
|
|
|
GAS_TYPE_SCHEMA = vol.In([CONF_UNIT_SYSTEM_METRIC, CONF_UNIT_SYSTEM_IMPERIAL])
|
2016-08-19 19:07:09 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
GAS_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_GAS_SERIAL): cv.string,
|
|
|
|
vol.Required(CONF_GAS_SECRET): cv.string,
|
|
|
|
vol.Optional(CONF_GAS_TYPE, default=CONF_UNIT_SYSTEM_METRIC): GAS_TYPE_SCHEMA,
|
|
|
|
vol.Optional(CONF_GAS_CALORIFIC, default=DEFAULT_CALORIFIC): vol.Coerce(float),
|
|
|
|
}
|
|
|
|
)
|
2016-08-19 19:07:09 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_ELEC): ELEC_SCHEMA, vol.Optional(CONF_GAS): GAS_SCHEMA}
|
|
|
|
)
|
2016-08-19 19:07:09 +00:00
|
|
|
|
2016-03-31 16:00:43 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Loop Energy sensors."""
|
2016-08-19 20:47:07 +00:00
|
|
|
elec_config = config.get(CONF_ELEC)
|
2018-02-17 09:29:14 +00:00
|
|
|
gas_config = config.get(CONF_GAS, {})
|
2016-04-17 17:34:40 +00:00
|
|
|
|
2016-03-31 16:00:43 +00:00
|
|
|
controller = pyloopenergy.LoopEnergy(
|
2016-08-19 20:47:07 +00:00
|
|
|
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),
|
2019-07-31 19:25:30 +00:00
|
|
|
gas_config.get(CONF_GAS_CALORIFIC),
|
|
|
|
)
|
2016-03-31 16:00:43 +00:00
|
|
|
|
2016-04-01 14:27:50 +00:00
|
|
|
def stop_loopenergy(event):
|
|
|
|
"""Shutdown loopenergy thread on exit."""
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.info("Shutting down loopenergy")
|
2016-04-01 14:27:50 +00:00
|
|
|
controller.terminate()
|
|
|
|
|
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_loopenergy)
|
|
|
|
|
2016-03-31 16:00:43 +00:00
|
|
|
sensors = [LoopEnergyElec(controller)]
|
|
|
|
|
2016-08-19 20:47:07 +00:00
|
|
|
if gas_config.get(CONF_GAS_SERIAL):
|
2016-03-31 16:00:43 +00:00
|
|
|
sensors.append(LoopEnergyGas(controller))
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(sensors)
|
2016-03-31 16:00:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LoopEnergyDevice(Entity):
|
|
|
|
"""Implementation of an Loop Energy base sensor."""
|
|
|
|
|
|
|
|
def __init__(self, controller):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._state = None
|
2016-10-30 08:58:34 +00:00
|
|
|
self._unit_of_measurement = DEFAULT_UNIT
|
2016-03-31 16:00:43 +00:00
|
|
|
self._controller = controller
|
|
|
|
self._name = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
def _callback(self):
|
2017-03-04 23:10:36 +00:00
|
|
|
self.schedule_update_ha_state(True)
|
2016-03-31 16:00:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LoopEnergyElec(LoopEnergyDevice):
|
|
|
|
"""Implementation of an Loop Energy Electricity sensor."""
|
|
|
|
|
|
|
|
def __init__(self, controller):
|
|
|
|
"""Initialize the sensor."""
|
2019-09-24 22:38:20 +00:00
|
|
|
super().__init__(controller)
|
2019-07-31 19:25:30 +00:00
|
|
|
self._name = "Power Usage"
|
2020-07-10 15:42:27 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Subscribe to updates."""
|
2016-03-31 16:00:43 +00:00
|
|
|
self._controller.subscribe_elecricity(self._callback)
|
|
|
|
|
|
|
|
def update(self):
|
2020-07-10 15:42:27 +00:00
|
|
|
"""Get the cached Loop energy reading."""
|
2016-04-02 15:47:21 +00:00
|
|
|
self._state = round(self._controller.electricity_useage, 2)
|
2016-03-31 16:00:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LoopEnergyGas(LoopEnergyDevice):
|
|
|
|
"""Implementation of an Loop Energy Gas sensor."""
|
|
|
|
|
|
|
|
def __init__(self, controller):
|
|
|
|
"""Initialize the sensor."""
|
2019-09-24 22:38:20 +00:00
|
|
|
super().__init__(controller)
|
2019-07-31 19:25:30 +00:00
|
|
|
self._name = "Gas Usage"
|
2020-07-10 15:42:27 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Subscribe to updates."""
|
2016-03-31 16:00:43 +00:00
|
|
|
self._controller.subscribe_gas(self._callback)
|
|
|
|
|
|
|
|
def update(self):
|
2020-07-10 15:42:27 +00:00
|
|
|
"""Get the cached Loop gas reading."""
|
2016-04-02 15:47:21 +00:00
|
|
|
self._state = round(self._controller.gas_useage, 2)
|