2019-02-26 18:18:09 +00:00
|
|
|
"""Support for Toon sensors."""
|
2021-03-18 13:43:52 +00:00
|
|
|
from __future__ import annotations
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2021-08-18 10:15:01 +00:00
|
|
|
from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorEntity
|
2019-02-26 18:18:09 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-06-23 01:22:41 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
ATTR_DEFAULT_ENABLED,
|
|
|
|
ATTR_DEVICE_CLASS,
|
|
|
|
ATTR_ICON,
|
|
|
|
ATTR_MEASUREMENT,
|
|
|
|
ATTR_NAME,
|
|
|
|
ATTR_SECTION,
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
|
|
|
DOMAIN,
|
|
|
|
SENSOR_ENTITIES,
|
|
|
|
)
|
|
|
|
from .coordinator import ToonDataUpdateCoordinator
|
|
|
|
from .models import (
|
2019-12-06 09:40:38 +00:00
|
|
|
ToonBoilerDeviceEntity,
|
2020-07-07 17:06:24 +00:00
|
|
|
ToonDisplayDeviceEntity,
|
2019-07-09 12:18:51 +00:00
|
|
|
ToonElectricityMeterDeviceEntity,
|
2019-12-06 09:40:38 +00:00
|
|
|
ToonEntity,
|
2019-07-09 12:18:51 +00:00
|
|
|
ToonGasMeterDeviceEntity,
|
|
|
|
ToonSolarDeviceEntity,
|
2020-08-04 23:02:19 +00:00
|
|
|
ToonWaterMeterDeviceEntity,
|
2019-07-09 12:18:51 +00:00
|
|
|
)
|
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(
|
2020-06-23 01:22:41 +00:00
|
|
|
hass: HomeAssistant, 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."""
|
2020-06-23 01:22:41 +00:00
|
|
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
2019-02-26 18:18:09 +00:00
|
|
|
|
|
|
|
sensors = [
|
2020-06-23 01:22:41 +00:00
|
|
|
ToonElectricityMeterDeviceSensor(coordinator, key=key)
|
|
|
|
for key in (
|
|
|
|
"power_average_daily",
|
|
|
|
"power_average",
|
|
|
|
"power_daily_cost",
|
|
|
|
"power_daily_value",
|
|
|
|
"power_meter_reading_low",
|
|
|
|
"power_meter_reading",
|
|
|
|
"power_value",
|
|
|
|
"solar_meter_reading_low_produced",
|
|
|
|
"solar_meter_reading_produced",
|
|
|
|
)
|
2019-02-26 18:18:09 +00:00
|
|
|
]
|
|
|
|
|
2020-07-07 17:06:24 +00:00
|
|
|
sensors.extend(
|
|
|
|
[ToonDisplayDeviceSensor(coordinator, key="current_display_temperature")]
|
|
|
|
)
|
|
|
|
|
2020-07-14 20:22:44 +00:00
|
|
|
sensors.extend(
|
|
|
|
[
|
|
|
|
ToonGasMeterDeviceSensor(coordinator, key=key)
|
|
|
|
for key in (
|
|
|
|
"gas_average_daily",
|
|
|
|
"gas_average",
|
|
|
|
"gas_daily_cost",
|
|
|
|
"gas_daily_usage",
|
|
|
|
"gas_meter_reading",
|
|
|
|
"gas_value",
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2020-08-04 23:02:19 +00:00
|
|
|
sensors.extend(
|
|
|
|
[
|
|
|
|
ToonWaterMeterDeviceSensor(coordinator, key=key)
|
|
|
|
for key in (
|
|
|
|
"water_average_daily",
|
|
|
|
"water_average",
|
|
|
|
"water_daily_cost",
|
|
|
|
"water_daily_usage",
|
|
|
|
"water_meter_reading",
|
|
|
|
"water_value",
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-06-23 01:22:41 +00:00
|
|
|
if coordinator.data.agreement.is_toon_solar:
|
2019-07-09 12:18:51 +00:00
|
|
|
sensors.extend(
|
|
|
|
[
|
2020-06-23 01:22:41 +00:00
|
|
|
ToonSolarDeviceSensor(coordinator, key=key)
|
2021-07-19 13:57:06 +00:00
|
|
|
for key in (
|
2020-06-23 01:22:41 +00:00
|
|
|
"solar_value",
|
|
|
|
"solar_maximum",
|
|
|
|
"solar_produced",
|
|
|
|
"solar_average_produced",
|
|
|
|
"power_usage_day_produced_solar",
|
|
|
|
"power_usage_day_from_grid_usage",
|
|
|
|
"power_usage_day_to_grid_usage",
|
|
|
|
"power_usage_current_covered_by_solar",
|
2021-07-19 13:57:06 +00:00
|
|
|
)
|
2019-07-09 12:18:51 +00:00
|
|
|
]
|
|
|
|
)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2020-06-23 01:22:41 +00:00
|
|
|
if coordinator.data.thermostat.have_opentherm_boiler:
|
2019-07-09 12:18:51 +00:00
|
|
|
sensors.extend(
|
|
|
|
[
|
2020-07-14 20:22:44 +00:00
|
|
|
ToonBoilerDeviceSensor(
|
|
|
|
coordinator, key="thermostat_info_current_modulation_level"
|
|
|
|
)
|
2019-07-09 12:18:51 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
async_add_entities(sensors, True)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:47:44 +00:00
|
|
|
class ToonSensor(ToonEntity, SensorEntity):
|
2019-02-26 18:18:09 +00:00
|
|
|
"""Defines a Toon sensor."""
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2020-06-23 01:22:41 +00:00
|
|
|
def __init__(self, coordinator: ToonDataUpdateCoordinator, *, key: str) -> None:
|
2017-10-24 16:36:08 +00:00
|
|
|
"""Initialize the Toon sensor."""
|
2020-06-23 01:22:41 +00:00
|
|
|
self.key = key
|
2021-06-07 11:24:07 +00:00
|
|
|
super().__init__(coordinator)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
2021-06-07 11:24:07 +00:00
|
|
|
sensor = SENSOR_ENTITIES[key]
|
|
|
|
self._attr_entity_registry_enabled_default = sensor.get(
|
|
|
|
ATTR_DEFAULT_ENABLED, True
|
|
|
|
)
|
|
|
|
self._attr_icon = sensor.get(ATTR_ICON)
|
|
|
|
self._attr_name = sensor[ATTR_NAME]
|
|
|
|
self._attr_state_class = sensor.get(ATTR_STATE_CLASS)
|
2021-08-11 16:57:50 +00:00
|
|
|
self._attr_native_unit_of_measurement = sensor[ATTR_UNIT_OF_MEASUREMENT]
|
2021-06-07 11:24:07 +00:00
|
|
|
self._attr_device_class = sensor.get(ATTR_DEVICE_CLASS)
|
|
|
|
self._attr_unique_id = (
|
|
|
|
# This unique ID is a bit ugly and contains unneeded information.
|
|
|
|
# It is here for legacy / backward compatible reasons.
|
|
|
|
f"{DOMAIN}_{coordinator.data.agreement.agreement_id}_sensor_{key}"
|
2020-06-23 01:22:41 +00:00
|
|
|
)
|
2017-10-19 15:59:57 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_value(self) -> str | None:
|
2017-10-19 15:59:57 +00:00
|
|
|
"""Return the state of the sensor."""
|
2020-06-23 01:22:41 +00:00
|
|
|
section = getattr(
|
|
|
|
self.coordinator.data, SENSOR_ENTITIES[self.key][ATTR_SECTION]
|
|
|
|
)
|
|
|
|
return getattr(section, SENSOR_ENTITIES[self.key][ATTR_MEASUREMENT])
|
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
|
|
|
class ToonGasMeterDeviceSensor(ToonSensor, ToonGasMeterDeviceEntity):
|
|
|
|
"""Defines a Gas Meter sensor."""
|
2017-10-19 15:59:57 +00:00
|
|
|
|
|
|
|
|
2020-08-04 23:02:19 +00:00
|
|
|
class ToonWaterMeterDeviceSensor(ToonSensor, ToonWaterMeterDeviceEntity):
|
|
|
|
"""Defines a Water Meter sensor."""
|
|
|
|
|
|
|
|
|
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
|
|
|
class ToonBoilerDeviceSensor(ToonSensor, ToonBoilerDeviceEntity):
|
|
|
|
"""Defines a Boiler sensor."""
|
2020-07-07 17:06:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ToonDisplayDeviceSensor(ToonSensor, ToonDisplayDeviceEntity):
|
|
|
|
"""Defines a Display sensor."""
|