2020-06-01 12:01:17 +00:00
|
|
|
"""Plugwise Sensor component for Home Assistant."""
|
2022-02-05 13:20:07 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from plugwise.smile import Smile
|
|
|
|
|
2021-10-22 16:46:21 +00:00
|
|
|
from homeassistant.components.sensor import (
|
2021-12-16 14:40:32 +00:00
|
|
|
SensorDeviceClass,
|
2021-10-22 16:46:21 +00:00
|
|
|
SensorEntity,
|
2022-02-06 18:19:57 +00:00
|
|
|
SensorEntityDescription,
|
2021-12-16 14:40:32 +00:00
|
|
|
SensorStateClass,
|
2021-10-22 16:46:21 +00:00
|
|
|
)
|
2022-01-03 18:10:57 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-10-22 16:46:21 +00:00
|
|
|
from homeassistant.const import (
|
2020-06-01 12:01:17 +00:00
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
ENERGY_WATT_HOUR,
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2020-06-01 12:01:17 +00:00
|
|
|
POWER_WATT,
|
|
|
|
PRESSURE_BAR,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
VOLUME_CUBIC_METERS,
|
|
|
|
)
|
2022-01-03 18:10:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-06-01 12:01:17 +00:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
COOL_ICON,
|
2020-09-06 20:26:14 +00:00
|
|
|
COORDINATOR,
|
2020-06-01 12:01:17 +00:00
|
|
|
DOMAIN,
|
|
|
|
FLAME_ICON,
|
|
|
|
IDLE_ICON,
|
2022-02-05 23:43:05 +00:00
|
|
|
LOGGER,
|
2020-06-01 12:01:17 +00:00
|
|
|
UNIT_LUMEN,
|
|
|
|
)
|
2022-02-06 17:03:50 +00:00
|
|
|
from .coordinator import PlugwiseDataUpdateCoordinator
|
2022-02-05 18:09:37 +00:00
|
|
|
from .entity import PlugwiseEntity
|
2020-06-01 12:01:17 +00:00
|
|
|
|
2022-02-06 18:19:57 +00:00
|
|
|
SENSORS: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="setpoint",
|
|
|
|
name="Setpoint",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="temperature",
|
|
|
|
name="Temperature",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="intended_boiler_temperature",
|
|
|
|
name="Intended Boiler Temperature",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="temperature_difference",
|
|
|
|
name="Temperature Difference",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="outdoor_temperature",
|
|
|
|
name="Outdoor Temperature",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="water_temperature",
|
|
|
|
name="Water Temperature",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="return_temperature",
|
|
|
|
name="Return Temperature",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="return_temperature",
|
|
|
|
name="Return Temperature",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_consumed",
|
|
|
|
name="Electricity Consumed",
|
|
|
|
native_unit_of_measurement=POWER_WATT,
|
|
|
|
device_class=SensorDeviceClass.POWER,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_produced",
|
|
|
|
name="Electricity Produced",
|
|
|
|
native_unit_of_measurement=POWER_WATT,
|
|
|
|
device_class=SensorDeviceClass.POWER,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_consumed_interval",
|
|
|
|
name="Electricity Consumed Interval",
|
|
|
|
native_unit_of_measurement=ENERGY_WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_consumed_peak_interval",
|
|
|
|
name="Electricity Consumed Peak Interval",
|
|
|
|
native_unit_of_measurement=ENERGY_WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_consumed_off_peak_interval",
|
|
|
|
name="Electricity Consumed Off Peak Interval",
|
|
|
|
native_unit_of_measurement=ENERGY_WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_produced_interval",
|
|
|
|
name="Electricity Produced Interval",
|
|
|
|
native_unit_of_measurement=ENERGY_WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_produced_peak_interval",
|
|
|
|
name="Electricity Produced Peak Interval",
|
|
|
|
native_unit_of_measurement=ENERGY_WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_produced_off_peak_interval",
|
|
|
|
name="Electricity Produced Off Peak Interval",
|
|
|
|
native_unit_of_measurement=ENERGY_WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_consumed_off_peak_point",
|
|
|
|
name="Electricity Consumed Off Peak Point",
|
|
|
|
native_unit_of_measurement=POWER_WATT,
|
|
|
|
device_class=SensorDeviceClass.POWER,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_consumed_peak_point",
|
|
|
|
name="Electricity Consumed Peak Point",
|
|
|
|
native_unit_of_measurement=POWER_WATT,
|
|
|
|
device_class=SensorDeviceClass.POWER,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_consumed_off_peak_cumulative",
|
|
|
|
name="Electricity Consumed Off Peak Cumulative",
|
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_consumed_peak_cumulative",
|
|
|
|
name="Electricity Consumed Peak Cumulative",
|
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_produced_off_peak_point",
|
|
|
|
name="Electricity Produced Off Peak Point",
|
|
|
|
native_unit_of_measurement=POWER_WATT,
|
|
|
|
device_class=SensorDeviceClass.POWER,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_produced_peak_point",
|
|
|
|
name="Electricity Produced Peak Point",
|
|
|
|
native_unit_of_measurement=POWER_WATT,
|
|
|
|
device_class=SensorDeviceClass.POWER,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_produced_off_peak_cumulative",
|
|
|
|
name="Electricity Produced Off Peak Cumulative",
|
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="electricity_produced_peak_cumulative",
|
|
|
|
name="Electricity Produced Peak Cumulative",
|
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="gas_consumed_interval",
|
|
|
|
name="Gas Consumed Interval",
|
|
|
|
native_unit_of_measurement=VOLUME_CUBIC_METERS,
|
|
|
|
device_class=SensorDeviceClass.GAS,
|
|
|
|
state_class=SensorStateClass.TOTAL,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="gas_consumed_cumulative",
|
|
|
|
name="Gas Consumed Cumulative",
|
|
|
|
native_unit_of_measurement=VOLUME_CUBIC_METERS,
|
|
|
|
device_class=SensorDeviceClass.GAS,
|
|
|
|
state_class=SensorStateClass.TOTAL,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="net_electricity_point",
|
|
|
|
name="Net Electricity Point",
|
|
|
|
native_unit_of_measurement=POWER_WATT,
|
|
|
|
device_class=SensorDeviceClass.POWER,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="net_electricity_cumulative",
|
|
|
|
name="Net Electricity Cumulative",
|
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="battery",
|
|
|
|
name="Battery",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
device_class=SensorDeviceClass.BATTERY,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="illuminance",
|
|
|
|
name="Illuminance",
|
|
|
|
native_unit_of_measurement=UNIT_LUMEN,
|
|
|
|
device_class=SensorDeviceClass.ILLUMINANCE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="modulation_level",
|
|
|
|
name="Modulation Level",
|
|
|
|
icon="mdi:percent",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="valve_position",
|
|
|
|
name="Valve Position",
|
|
|
|
icon="mdi:valve",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="water_pressure",
|
|
|
|
name="Water Pressure",
|
|
|
|
native_unit_of_measurement=PRESSURE_BAR,
|
|
|
|
device_class=SensorDeviceClass.PRESSURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
INDICATE_ACTIVE_LOCAL_DEVICE_SENSORS: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="cooling_state",
|
|
|
|
name="Cooling State",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="flame_state",
|
|
|
|
name="Flame State",
|
|
|
|
),
|
|
|
|
)
|
2020-06-16 12:49:13 +00:00
|
|
|
|
2020-06-01 12:01:17 +00:00
|
|
|
|
2022-01-03 18:10:57 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-06-01 12:01:17 +00:00
|
|
|
"""Set up the Smile sensors from a config entry."""
|
|
|
|
api = hass.data[DOMAIN][config_entry.entry_id]["api"]
|
2020-09-06 20:26:14 +00:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]
|
2020-06-01 12:01:17 +00:00
|
|
|
|
2022-02-08 10:13:05 +00:00
|
|
|
entities: list[PlugwiseSensorEnity] = []
|
2022-02-08 18:08:01 +00:00
|
|
|
for device_id, device in coordinator.data.devices.items():
|
2022-02-06 18:19:57 +00:00
|
|
|
for description in SENSORS:
|
2022-02-08 10:13:05 +00:00
|
|
|
if (
|
2022-02-08 18:08:01 +00:00
|
|
|
"sensors" not in device
|
|
|
|
or device["sensors"].get(description.key) is None
|
2022-02-08 10:13:05 +00:00
|
|
|
):
|
2020-06-04 06:18:46 +00:00
|
|
|
continue
|
|
|
|
|
2022-02-08 10:13:05 +00:00
|
|
|
entities.append(
|
|
|
|
PlugwiseSensorEnity(
|
|
|
|
api,
|
|
|
|
coordinator,
|
|
|
|
device_id,
|
|
|
|
description,
|
2020-06-04 06:18:46 +00:00
|
|
|
)
|
2022-02-08 10:13:05 +00:00
|
|
|
)
|
2020-06-01 12:01:17 +00:00
|
|
|
|
2022-02-08 10:13:05 +00:00
|
|
|
if coordinator.data.gateway["single_master_thermostat"] is False:
|
|
|
|
# These sensors should actually be binary sensors.
|
2022-02-06 18:19:57 +00:00
|
|
|
for description in INDICATE_ACTIVE_LOCAL_DEVICE_SENSORS:
|
2022-02-08 18:08:01 +00:00
|
|
|
if description.key not in device:
|
2020-06-04 06:18:46 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
entities.append(
|
2022-02-08 10:13:05 +00:00
|
|
|
PlugwiseAuxSensorEntity(
|
2020-06-04 06:18:46 +00:00
|
|
|
api,
|
|
|
|
coordinator,
|
2022-02-08 10:13:05 +00:00
|
|
|
device_id,
|
2022-02-06 18:19:57 +00:00
|
|
|
description,
|
2020-06-01 12:01:17 +00:00
|
|
|
)
|
2020-06-04 06:18:46 +00:00
|
|
|
)
|
|
|
|
break
|
2020-06-01 12:01:17 +00:00
|
|
|
|
|
|
|
async_add_entities(entities, True)
|
|
|
|
|
|
|
|
|
2022-02-08 10:13:05 +00:00
|
|
|
class PlugwiseSensorEnity(PlugwiseEntity, SensorEntity):
|
|
|
|
"""Represent Plugwise Sensors."""
|
2020-06-01 12:01:17 +00:00
|
|
|
|
2022-02-05 13:20:07 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
api: Smile,
|
2022-02-06 17:03:50 +00:00
|
|
|
coordinator: PlugwiseDataUpdateCoordinator,
|
2022-02-08 10:13:05 +00:00
|
|
|
device_id: str,
|
2022-02-06 18:19:57 +00:00
|
|
|
description: SensorEntityDescription,
|
2022-02-05 13:20:07 +00:00
|
|
|
) -> None:
|
2020-06-01 12:01:17 +00:00
|
|
|
"""Initialise the sensor."""
|
2022-02-08 18:08:01 +00:00
|
|
|
super().__init__(coordinator, device_id)
|
2022-02-06 18:19:57 +00:00
|
|
|
self.entity_description = description
|
2022-02-08 18:08:01 +00:00
|
|
|
self._api = api
|
2022-02-08 10:13:05 +00:00
|
|
|
self._attr_unique_id = f"{device_id}-{description.key}"
|
2022-02-08 18:08:01 +00:00
|
|
|
self._attr_name = (
|
|
|
|
f"{coordinator.data.devices[device_id].get('name', '')} {description.name}"
|
|
|
|
).lstrip()
|
2020-06-01 12:01:17 +00:00
|
|
|
|
|
|
|
@callback
|
2022-02-05 13:20:07 +00:00
|
|
|
def _async_process_data(self) -> None:
|
2020-06-01 12:01:17 +00:00
|
|
|
"""Update the entity."""
|
2022-02-08 10:13:05 +00:00
|
|
|
if not (data := self.coordinator.data.devices.get(self._dev_id)):
|
2022-02-08 18:08:01 +00:00
|
|
|
LOGGER.error("Received no data for device %s", self._dev_id)
|
2020-06-01 12:01:17 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
return
|
|
|
|
|
2022-02-08 10:13:05 +00:00
|
|
|
self._attr_native_value = data["sensors"].get(self.entity_description.key)
|
2020-06-01 12:01:17 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
|
2022-02-08 10:13:05 +00:00
|
|
|
class PlugwiseAuxSensorEntity(PlugwiseSensorEnity):
|
2020-11-08 17:09:43 +00:00
|
|
|
"""Auxiliary Device Sensors."""
|
2020-06-01 12:01:17 +00:00
|
|
|
|
2022-02-06 18:19:57 +00:00
|
|
|
_cooling_state = False
|
|
|
|
_heating_state = False
|
2020-06-01 12:01:17 +00:00
|
|
|
|
|
|
|
@callback
|
2022-02-05 13:20:07 +00:00
|
|
|
def _async_process_data(self) -> None:
|
2020-06-01 12:01:17 +00:00
|
|
|
"""Update the entity."""
|
2022-02-08 10:13:05 +00:00
|
|
|
if not (data := self.coordinator.data.devices.get(self._dev_id)):
|
2022-02-08 18:08:01 +00:00
|
|
|
LOGGER.error("Received no data for device %s", self._dev_id)
|
2020-06-01 12:01:17 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
return
|
|
|
|
|
|
|
|
if data.get("heating_state") is not None:
|
|
|
|
self._heating_state = data["heating_state"]
|
|
|
|
if data.get("cooling_state") is not None:
|
|
|
|
self._cooling_state = data["cooling_state"]
|
|
|
|
|
2022-02-05 13:20:07 +00:00
|
|
|
self._attr_native_value = "idle"
|
|
|
|
self._attr_icon = IDLE_ICON
|
2020-06-01 12:01:17 +00:00
|
|
|
if self._heating_state:
|
2022-02-05 13:20:07 +00:00
|
|
|
self._attr_native_value = "heating"
|
|
|
|
self._attr_icon = FLAME_ICON
|
2020-06-01 12:01:17 +00:00
|
|
|
if self._cooling_state:
|
2022-02-05 13:20:07 +00:00
|
|
|
self._attr_native_value = "cooling"
|
|
|
|
self._attr_icon = COOL_ICON
|
2020-06-01 12:01:17 +00:00
|
|
|
|
|
|
|
self.async_write_ha_state()
|