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,
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
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(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
|
2019-07-09 12:18:51 +00:00
|
|
|
) -> 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(
|
2019-07-31 19:25:30 +00:00
|
|
|
toon, "power", "value", "Current Power Usage", "mdi:power-plug", POWER_WATT
|
2019-07-09 12:18:51 +00:00
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"power",
|
|
|
|
"average",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Average Power Usage",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:power-plug",
|
2019-07-09 12:18:51 +00:00
|
|
|
POWER_WATT,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"power",
|
|
|
|
"daily_value",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Power Usage Today",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:power-plug",
|
2019-07-09 12:18:51 +00:00
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"power",
|
|
|
|
"daily_cost",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Power Cost Today",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:power-plug",
|
2019-07-09 12:18:51 +00:00
|
|
|
CURRENCY_EUR,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"power",
|
|
|
|
"average_daily",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Average Daily Power Usage",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:power-plug",
|
2019-07-09 12:18:51 +00:00
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"power",
|
|
|
|
"meter_reading",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Power Meter Feed IN Tariff 1",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:power-plug",
|
2019-07-09 12:18:51 +00:00
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"power",
|
|
|
|
"meter_reading_low",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Power Meter Feed IN Tariff 2",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:power-plug",
|
2019-07-09 12:18:51 +00:00
|
|
|
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,
|
2019-07-31 19:25:30 +00:00
|
|
|
"gas",
|
|
|
|
"value",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Current Gas Usage",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:gas-cylinder",
|
2019-07-09 12:18:51 +00:00
|
|
|
VOLUME_CM3,
|
|
|
|
),
|
|
|
|
ToonGasMeterDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"gas",
|
|
|
|
"average",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Average Gas Usage",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:gas-cylinder",
|
2019-07-09 12:18:51 +00:00
|
|
|
VOLUME_CM3,
|
|
|
|
),
|
|
|
|
ToonGasMeterDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"gas",
|
|
|
|
"daily_usage",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Gas Usage Today",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:gas-cylinder",
|
2019-07-09 12:18:51 +00:00
|
|
|
VOLUME_M3,
|
|
|
|
),
|
|
|
|
ToonGasMeterDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"gas",
|
|
|
|
"average_daily",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Average Daily Gas Usage",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:gas-cylinder",
|
2019-07-09 12:18:51 +00:00
|
|
|
VOLUME_M3,
|
|
|
|
),
|
|
|
|
ToonGasMeterDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"gas",
|
|
|
|
"meter_reading",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Gas Meter",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:gas-cylinder",
|
2019-07-09 12:18:51 +00:00
|
|
|
VOLUME_M3,
|
|
|
|
),
|
|
|
|
ToonGasMeterDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"gas",
|
|
|
|
"daily_cost",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Gas Cost Today",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:gas-cylinder",
|
2019-07-09 12:18:51 +00:00
|
|
|
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,
|
2019-07-31 19:25:30 +00:00
|
|
|
"solar",
|
|
|
|
"value",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Current Solar Production",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:solar-power",
|
2019-07-09 12:18:51 +00:00
|
|
|
POWER_WATT,
|
|
|
|
),
|
|
|
|
ToonSolarDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"solar",
|
|
|
|
"maximum",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Max Solar Production",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:solar-power",
|
2019-07-09 12:18:51 +00:00
|
|
|
POWER_WATT,
|
|
|
|
),
|
|
|
|
ToonSolarDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"solar",
|
|
|
|
"produced",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Solar Production to Grid",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:solar-power",
|
2019-07-09 12:18:51 +00:00
|
|
|
POWER_WATT,
|
|
|
|
),
|
|
|
|
ToonSolarDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"solar",
|
|
|
|
"average_produced",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Average Solar Production to Grid",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:solar-power",
|
2019-07-09 12:18:51 +00:00
|
|
|
POWER_WATT,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"solar",
|
|
|
|
"meter_reading_produced",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Power Meter Feed OUT Tariff 1",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:solar-power",
|
2019-07-09 12:18:51 +00:00
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
),
|
|
|
|
ToonElectricityMeterDeviceSensor(
|
|
|
|
toon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"solar",
|
|
|
|
"meter_reading_low_produced",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Power Meter Feed OUT Tariff 2",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:solar-power",
|
2019-07-09 12:18:51 +00:00
|
|
|
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,
|
2019-07-31 19:25:30 +00:00
|
|
|
"thermostat_info",
|
|
|
|
"current_modulation_level",
|
2019-07-09 12:18:51 +00:00
|
|
|
"Boiler Modulation Level",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:percent",
|
2019-07-09 12:18:51 +00:00
|
|
|
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__(
|
2019-07-31 19:25:30 +00:00
|
|
|
self,
|
|
|
|
toon: ToonData,
|
|
|
|
section: str,
|
|
|
|
measurement: str,
|
|
|
|
name: str,
|
|
|
|
icon: str,
|
|
|
|
unit_of_measurement: str,
|
2019-07-09 12:18:51 +00:00
|
|
|
) -> 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-31 19:25:30 +00:00
|
|
|
return "_".join(
|
|
|
|
[DOMAIN, self.toon.agreement.id, "sensor", self.section, self.measurement]
|
2019-07-09 12:18:51 +00:00
|
|
|
)
|
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-07-31 19:25:30 +00:00
|
|
|
if self.section == "power" and self.measurement == "daily_value":
|
2019-07-09 12:18:51 +00:00
|
|
|
value = round(
|
2019-07-31 19:25:30 +00:00
|
|
|
(float(section.daily_usage) + float(section.daily_usage_low)) / 1000.0,
|
2019-07-09 12:18:51 +00:00
|
|
|
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-31 19:25:30 +00:00
|
|
|
if self.section == "power" and self.measurement in [
|
|
|
|
"meter_reading",
|
|
|
|
"meter_reading_low",
|
|
|
|
"average_daily",
|
2019-07-09 12:18:51 +00:00
|
|
|
]:
|
|
|
|
value = round(float(value) / 1000.0, 2)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.section == "solar" and self.measurement in [
|
|
|
|
"meter_reading_produced",
|
|
|
|
"meter_reading_low_produced",
|
2019-07-09 12:18:51 +00:00
|
|
|
]:
|
|
|
|
value = float(value) / 1000.0
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.section == "gas" and self.measurement in [
|
|
|
|
"average_daily",
|
|
|
|
"daily_usage",
|
|
|
|
"meter_reading",
|
2019-07-09 12:18:51 +00:00
|
|
|
]:
|
|
|
|
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-31 19:25:30 +00:00
|
|
|
class ToonElectricityMeterDeviceSensor(ToonSensor, ToonElectricityMeterDeviceEntity):
|
2019-07-09 12:18:51 +00:00
|
|
|
"""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
|