122 lines
3.3 KiB
Python
122 lines
3.3 KiB
Python
|
"""
|
||
|
Support for Loop Energy sensors.
|
||
|
|
||
|
For more details about this platform, please refer to the documentation at
|
||
|
https://home-assistant.io/components/sensor.loop_energy/
|
||
|
"""
|
||
|
import logging
|
||
|
|
||
|
from homeassistant.helpers.entity import Entity
|
||
|
|
||
|
_LOGGER = logging.getLogger(__name__)
|
||
|
|
||
|
DOMAIN = "loopenergy"
|
||
|
|
||
|
REQUIREMENTS = ['pyloopenergy==0.0.4']
|
||
|
|
||
|
|
||
|
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')
|
||
|
|
||
|
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
|
||
|
|
||
|
controller = pyloopenergy.LoopEnergy(
|
||
|
elec_serial,
|
||
|
elec_secret,
|
||
|
gas_serial,
|
||
|
gas_secret
|
||
|
)
|
||
|
|
||
|
sensors = [LoopEnergyElec(controller)]
|
||
|
|
||
|
if gas_serial:
|
||
|
sensors.append(LoopEnergyGas(controller))
|
||
|
|
||
|
add_devices(sensors)
|
||
|
|
||
|
|
||
|
# pylint: disable=too-many-instance-attributes
|
||
|
class LoopEnergyDevice(Entity):
|
||
|
"""Implementation of an Loop Energy base sensor."""
|
||
|
|
||
|
# pylint: disable=too-many-arguments
|
||
|
def __init__(self, controller):
|
||
|
"""Initialize the sensor."""
|
||
|
self._state = None
|
||
|
self._unit_of_measurement = 'kW'
|
||
|
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):
|
||
|
self.update_ha_state(True)
|
||
|
|
||
|
|
||
|
# pylint: disable=too-many-instance-attributes
|
||
|
class LoopEnergyElec(LoopEnergyDevice):
|
||
|
"""Implementation of an Loop Energy Electricity sensor."""
|
||
|
|
||
|
# pylint: disable=too-many-arguments
|
||
|
def __init__(self, controller):
|
||
|
"""Initialize the sensor."""
|
||
|
super(LoopEnergyElec, self).__init__(controller)
|
||
|
self._name = 'Power Usage'
|
||
|
self._controller.subscribe_elecricity(self._callback)
|
||
|
|
||
|
def update(self):
|
||
|
"""Get the cached Loop energy."""
|
||
|
self._state = self._controller.electricity_useage
|
||
|
|
||
|
|
||
|
# pylint: disable=too-many-instance-attributes
|
||
|
class LoopEnergyGas(LoopEnergyDevice):
|
||
|
"""Implementation of an Loop Energy Gas sensor."""
|
||
|
|
||
|
# pylint: disable=too-many-arguments
|
||
|
def __init__(self, controller):
|
||
|
"""Initialize the sensor."""
|
||
|
super(LoopEnergyGas, self).__init__(controller)
|
||
|
self._name = 'Gas Usage'
|
||
|
self._controller.subscribe_gas(self._callback)
|
||
|
|
||
|
def update(self):
|
||
|
"""Get the cached Loop energy."""
|
||
|
self._state = self._controller.gas_useage
|