diff --git a/.coveragerc b/.coveragerc index 448764d202c..89c9c8a498f 100644 --- a/.coveragerc +++ b/.coveragerc @@ -139,6 +139,7 @@ omit = homeassistant/components/sensor/glances.py homeassistant/components/sensor/gtfs.py homeassistant/components/sensor/netatmo.py + homeassistant/components/sensor/loopenergy.py homeassistant/components/sensor/neurio_energy.py homeassistant/components/sensor/onewire.py homeassistant/components/sensor/openweathermap.py diff --git a/homeassistant/components/sensor/loopenergy.py b/homeassistant/components/sensor/loopenergy.py new file mode 100644 index 00000000000..5a04275dbde --- /dev/null +++ b/homeassistant/components/sensor/loopenergy.py @@ -0,0 +1,121 @@ +""" +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 diff --git a/requirements_all.txt b/requirements_all.txt index e298be83998..31f3fa51df6 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -182,6 +182,9 @@ pyfttt==0.3 # homeassistant.components.device_tracker.icloud pyicloud==0.7.2 +# homeassistant.components.sensor.loopenergy +pyloopenergy==0.0.4 + # homeassistant.components.device_tracker.netgear pynetgear==0.3.2