2019-02-26 18:18:09 +00:00
|
|
|
"""Support for Toon sensors."""
|
2017-10-19 15:59:57 +00:00
|
|
|
import logging
|
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
2019-07-09 12:18:51 +00:00
|
|
|
from homeassistant.const import ENERGY_KILO_WATT_HOUR, POWER_WATT
|
|
|
|
|
|
|
|
from . import (
|
|
|
|
ToonData,
|
|
|
|
ToonEntity,
|
|
|
|
ToonElectricityMeterDeviceEntity,
|
|
|
|
ToonGasMeterDeviceEntity,
|
|
|
|
ToonSolarDeviceEntity,
|
|
|
|
ToonBoilerDeviceEntity,
|
|
|
|
)
|
|
|
|
from .const import (
|
|
|
|
CURRENCY_EUR,
|
|
|
|
DATA_TOON,
|
|
|
|
DOMAIN,
|
|
|
|
VOLUME_CM3,
|
|
|
|
VOLUME_M3,
|
|
|
|
RATIO_PERCENT,
|
|
|
|
)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
|
2019-07-09 12:18:51 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
|
|
|
|
) -> None:
|
2019-02-26 18:18:09 +00:00
|
|
|
"""Set up Toon sensors based on a config entry."""
|
2019-07-09 12:18:51 +00:00
|
|
|
toon = hass.data[DATA_TOON][entry.entry_id]
|
2019-02-26 18:18:09 +00:00
|
|
|
|
|
|
|
sensors = [
|
2019-07-09 12:18:51 +00:00
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'power',
|
|
|
|
'value',
|
|
|
|
"Current Power Usage",
|
|
|
|
'mdi:power-plug',
|
|
|
|
POWER_WATT,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'power',
|
|
|
|
'average',
|
|
|
|
"Average Power Usage",
|
|
|
|
'mdi:power-plug',
|
|
|
|
POWER_WATT,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'power',
|
|
|
|
'daily_value',
|
|
|
|
"Power Usage Today",
|
|
|
|
'mdi:power-plug',
|
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'power',
|
|
|
|
'daily_cost',
|
|
|
|
"Power Cost Today",
|
|
|
|
'mdi:power-plug',
|
|
|
|
CURRENCY_EUR,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'power',
|
|
|
|
'average_daily',
|
|
|
|
"Average Daily Power Usage",
|
|
|
|
'mdi:power-plug',
|
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'power',
|
|
|
|
'meter_reading',
|
|
|
|
"Power Meter Feed IN Tariff 1",
|
|
|
|
'mdi:power-plug',
|
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'power',
|
|
|
|
'meter_reading_low',
|
|
|
|
"Power Meter Feed IN Tariff 2",
|
|
|
|
'mdi:power-plug',
|
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
),
|
2019-02-26 18:18:09 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
if toon.gas:
|
2019-07-09 12:18:51 +00:00
|
|
|
sensors.extend(
|
|
|
|
[
|
|
|
|
ToonGasMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'gas',
|
|
|
|
'value',
|
|
|
|
"Current Gas Usage",
|
|
|
|
'mdi:gas-cylinder',
|
|
|
|
VOLUME_CM3,
|
|
|
|
),
|
|
|
|
ToonGasMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'gas',
|
|
|
|
'average',
|
|
|
|
"Average Gas Usage",
|
|
|
|
'mdi:gas-cylinder',
|
|
|
|
VOLUME_CM3,
|
|
|
|
),
|
|
|
|
ToonGasMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'gas',
|
|
|
|
'daily_usage',
|
|
|
|
"Gas Usage Today",
|
|
|
|
'mdi:gas-cylinder',
|
|
|
|
VOLUME_M3,
|
|
|
|
),
|
|
|
|
ToonGasMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'gas',
|
|
|
|
'average_daily',
|
|
|
|
"Average Daily Gas Usage",
|
|
|
|
'mdi:gas-cylinder',
|
|
|
|
VOLUME_M3,
|
|
|
|
),
|
|
|
|
ToonGasMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'gas',
|
|
|
|
'meter_reading',
|
|
|
|
"Gas Meter",
|
|
|
|
'mdi:gas-cylinder',
|
|
|
|
VOLUME_M3,
|
|
|
|
),
|
|
|
|
ToonGasMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'gas',
|
|
|
|
'daily_cost',
|
|
|
|
"Gas Cost Today",
|
|
|
|
'mdi:gas-cylinder',
|
|
|
|
CURRENCY_EUR,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
if toon.solar:
|
2019-07-09 12:18:51 +00:00
|
|
|
sensors.extend(
|
|
|
|
[
|
|
|
|
ToonSolarDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'solar',
|
|
|
|
'value',
|
|
|
|
"Current Solar Production",
|
|
|
|
'mdi:solar-power',
|
|
|
|
POWER_WATT,
|
|
|
|
),
|
|
|
|
ToonSolarDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'solar',
|
|
|
|
'maximum',
|
|
|
|
"Max Solar Production",
|
|
|
|
'mdi:solar-power',
|
|
|
|
POWER_WATT,
|
|
|
|
),
|
|
|
|
ToonSolarDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'solar',
|
|
|
|
'produced',
|
|
|
|
"Solar Production to Grid",
|
|
|
|
'mdi:solar-power',
|
|
|
|
POWER_WATT,
|
|
|
|
),
|
|
|
|
ToonSolarDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'solar',
|
|
|
|
'average_produced',
|
|
|
|
"Average Solar Production to Grid",
|
|
|
|
'mdi:solar-power',
|
|
|
|
POWER_WATT,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'solar',
|
|
|
|
'meter_reading_produced',
|
|
|
|
"Power Meter Feed OUT Tariff 1",
|
|
|
|
'mdi:solar-power',
|
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'solar',
|
|
|
|
'meter_reading_low_produced',
|
|
|
|
"Power Meter Feed OUT Tariff 2",
|
|
|
|
'mdi:solar-power',
|
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
if toon.thermostat_info.have_ot_boiler:
|
2019-07-09 12:18:51 +00:00
|
|
|
sensors.extend(
|
|
|
|
[
|
|
|
|
ToonBoilerDeviceSensor(
|
|
|
|
toon,
|
|
|
|
'thermostat_info',
|
|
|
|
'current_modulation_level',
|
|
|
|
"Boiler Modulation Level",
|
|
|
|
'mdi:percent',
|
|
|
|
RATIO_PERCENT,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
async_add_entities(sensors, True)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
class ToonSensor(ToonEntity):
|
|
|
|
"""Defines a Toon sensor."""
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-07-09 12:18:51 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
toon: ToonData,
|
|
|
|
section: str,
|
|
|
|
measurement: str,
|
|
|
|
name: str,
|
|
|
|
icon: str,
|
|
|
|
unit_of_measurement: str,
|
|
|
|
) -> None:
|
2017-10-24 16:36:08 +00:00
|
|
|
"""Initialize the Toon sensor."""
|
2017-10-19 15:59:57 +00:00
|
|
|
self._state = None
|
|
|
|
self._unit_of_measurement = unit_of_measurement
|
2019-02-26 18:18:09 +00:00
|
|
|
self.section = section
|
|
|
|
self.measurement = measurement
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
super().__init__(toon, name, icon)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
|
|
|
@property
|
2019-02-26 18:18:09 +00:00
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return the unique ID for this sensor."""
|
2019-07-09 12:18:51 +00:00
|
|
|
return '_'.join(
|
|
|
|
[
|
|
|
|
DOMAIN,
|
|
|
|
self.toon.agreement.id,
|
|
|
|
'sensor',
|
|
|
|
self.section,
|
|
|
|
self.measurement,
|
|
|
|
]
|
|
|
|
)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2019-02-26 18:18:09 +00:00
|
|
|
return self._state
|
2017-10-19 15:59:57 +00:00
|
|
|
|
|
|
|
@property
|
2019-02-26 18:18:09 +00:00
|
|
|
def unit_of_measurement(self) -> str:
|
2017-10-19 15:59:57 +00:00
|
|
|
"""Return the unit this state is expressed in."""
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
2019-03-04 23:52:00 +00:00
|
|
|
def update(self) -> None:
|
2017-10-19 15:59:57 +00:00
|
|
|
"""Get the latest data from the sensor."""
|
2019-02-26 18:18:09 +00:00
|
|
|
section = getattr(self.toon, self.section)
|
|
|
|
value = None
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-07-09 12:18:51 +00:00
|
|
|
if not section:
|
|
|
|
return
|
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
if self.section == 'power' and self.measurement == 'daily_value':
|
2019-07-09 12:18:51 +00:00
|
|
|
value = round(
|
|
|
|
(float(section.daily_usage) + float(section.daily_usage_low))
|
|
|
|
/ 1000.0,
|
|
|
|
2,
|
|
|
|
)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
if value is None:
|
|
|
|
value = getattr(section, self.measurement)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-07-09 12:18:51 +00:00
|
|
|
if self.section == 'power' and self.measurement in [
|
|
|
|
'meter_reading',
|
|
|
|
'meter_reading_low',
|
|
|
|
'average_daily',
|
|
|
|
]:
|
|
|
|
value = round(float(value) / 1000.0, 2)
|
|
|
|
|
|
|
|
if self.section == 'solar' and self.measurement in [
|
|
|
|
'meter_reading_produced',
|
|
|
|
'meter_reading_low_produced',
|
|
|
|
]:
|
|
|
|
value = float(value) / 1000.0
|
|
|
|
|
|
|
|
if self.section == 'gas' and self.measurement in [
|
|
|
|
'average_daily',
|
|
|
|
'daily_usage',
|
|
|
|
'meter_reading',
|
|
|
|
]:
|
|
|
|
value = round(float(value) / 1000.0, 2)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
self._state = max(0, value)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
|
|
|
|
2019-07-09 12:18:51 +00:00
|
|
|
class ToonElectricityMeterDeviceSensor(
|
|
|
|
ToonSensor, ToonElectricityMeterDeviceEntity
|
|
|
|
):
|
|
|
|
"""Defines a Electricity Meter sensor."""
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
pass
|
2017-10-19 15:59:57 +00:00
|
|
|
|
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
class ToonGasMeterDeviceSensor(ToonSensor, ToonGasMeterDeviceEntity):
|
|
|
|
"""Defines a Gas Meter sensor."""
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
pass
|
2017-10-19 15:59:57 +00:00
|
|
|
|
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
class ToonSolarDeviceSensor(ToonSensor, ToonSolarDeviceEntity):
|
|
|
|
"""Defines a Solar sensor."""
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
pass
|
2017-10-19 15:59:57 +00:00
|
|
|
|
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
class ToonBoilerDeviceSensor(ToonSensor, ToonBoilerDeviceEntity):
|
|
|
|
"""Defines a Boiler sensor."""
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2019-02-26 18:18:09 +00:00
|
|
|
pass
|