init home-assistant
This commit is contained in:
451
home-assistant/homeassistant/components/plugwise/sensor.py
Normal file
451
home-assistant/homeassistant/components/plugwise/sensor.py
Normal file
@@ -0,0 +1,451 @@
|
||||
"""Plugwise Sensor component for Home Assistant."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from plugwise.constants import SensorType
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
LIGHT_LUX,
|
||||
PERCENTAGE,
|
||||
EntityCategory,
|
||||
UnitOfElectricPotential,
|
||||
UnitOfEnergy,
|
||||
UnitOfPower,
|
||||
UnitOfPressure,
|
||||
UnitOfTemperature,
|
||||
UnitOfVolume,
|
||||
UnitOfVolumeFlowRate,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import PlugwiseConfigEntry
|
||||
from .coordinator import PlugwiseDataUpdateCoordinator
|
||||
from .entity import PlugwiseEntity
|
||||
|
||||
# Coordinator is used to centralize the data updates
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PlugwiseSensorEntityDescription(SensorEntityDescription):
|
||||
"""Describes Plugwise sensor entity."""
|
||||
|
||||
key: SensorType
|
||||
|
||||
|
||||
SENSORS: tuple[PlugwiseSensorEntityDescription, ...] = (
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="setpoint",
|
||||
translation_key="setpoint",
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="setpoint_high",
|
||||
translation_key="cooling_setpoint",
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="setpoint_low",
|
||||
translation_key="heating_setpoint",
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="temperature",
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="intended_boiler_temperature",
|
||||
translation_key="intended_boiler_temperature",
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="temperature_difference",
|
||||
translation_key="temperature_difference",
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="outdoor_temperature",
|
||||
translation_key="outdoor_temperature",
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="outdoor_air_temperature",
|
||||
translation_key="outdoor_air_temperature",
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="water_temperature",
|
||||
translation_key="water_temperature",
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="return_temperature",
|
||||
translation_key="return_temperature",
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_consumed",
|
||||
translation_key="electricity_consumed",
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_produced",
|
||||
translation_key="electricity_produced",
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_consumed_interval",
|
||||
translation_key="electricity_consumed_interval",
|
||||
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
||||
device_class=SensorDeviceClass.ENERGY,
|
||||
state_class=SensorStateClass.TOTAL,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_consumed_peak_interval",
|
||||
translation_key="electricity_consumed_peak_interval",
|
||||
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
||||
device_class=SensorDeviceClass.ENERGY,
|
||||
state_class=SensorStateClass.TOTAL,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_consumed_off_peak_interval",
|
||||
translation_key="electricity_consumed_off_peak_interval",
|
||||
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
||||
device_class=SensorDeviceClass.ENERGY,
|
||||
state_class=SensorStateClass.TOTAL,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_produced_interval",
|
||||
translation_key="electricity_produced_interval",
|
||||
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
||||
device_class=SensorDeviceClass.ENERGY,
|
||||
state_class=SensorStateClass.TOTAL,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_produced_peak_interval",
|
||||
translation_key="electricity_produced_peak_interval",
|
||||
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
||||
device_class=SensorDeviceClass.ENERGY,
|
||||
state_class=SensorStateClass.TOTAL,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_produced_off_peak_interval",
|
||||
translation_key="electricity_produced_off_peak_interval",
|
||||
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
||||
device_class=SensorDeviceClass.ENERGY,
|
||||
state_class=SensorStateClass.TOTAL,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_consumed_point",
|
||||
translation_key="electricity_consumed_point",
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_consumed_off_peak_point",
|
||||
translation_key="electricity_consumed_off_peak_point",
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_consumed_peak_point",
|
||||
translation_key="electricity_consumed_peak_point",
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_consumed_off_peak_cumulative",
|
||||
translation_key="electricity_consumed_off_peak_cumulative",
|
||||
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
||||
device_class=SensorDeviceClass.ENERGY,
|
||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_consumed_peak_cumulative",
|
||||
translation_key="electricity_consumed_peak_cumulative",
|
||||
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
||||
device_class=SensorDeviceClass.ENERGY,
|
||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_produced_point",
|
||||
translation_key="electricity_produced_point",
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_produced_off_peak_point",
|
||||
translation_key="electricity_produced_off_peak_point",
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_produced_peak_point",
|
||||
translation_key="electricity_produced_peak_point",
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_produced_off_peak_cumulative",
|
||||
translation_key="electricity_produced_off_peak_cumulative",
|
||||
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
||||
device_class=SensorDeviceClass.ENERGY,
|
||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_produced_peak_cumulative",
|
||||
translation_key="electricity_produced_peak_cumulative",
|
||||
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
||||
device_class=SensorDeviceClass.ENERGY,
|
||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_phase_one_consumed",
|
||||
translation_key="electricity_phase_one_consumed",
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_phase_two_consumed",
|
||||
translation_key="electricity_phase_two_consumed",
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_phase_three_consumed",
|
||||
translation_key="electricity_phase_three_consumed",
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_phase_one_produced",
|
||||
translation_key="electricity_phase_one_produced",
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_phase_two_produced",
|
||||
translation_key="electricity_phase_two_produced",
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="electricity_phase_three_produced",
|
||||
translation_key="electricity_phase_three_produced",
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="voltage_phase_one",
|
||||
translation_key="voltage_phase_one",
|
||||
device_class=SensorDeviceClass.VOLTAGE,
|
||||
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="voltage_phase_two",
|
||||
translation_key="voltage_phase_two",
|
||||
device_class=SensorDeviceClass.VOLTAGE,
|
||||
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="voltage_phase_three",
|
||||
translation_key="voltage_phase_three",
|
||||
device_class=SensorDeviceClass.VOLTAGE,
|
||||
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="gas_consumed_interval",
|
||||
translation_key="gas_consumed_interval",
|
||||
native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="gas_consumed_cumulative",
|
||||
translation_key="gas_consumed_cumulative",
|
||||
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
|
||||
device_class=SensorDeviceClass.GAS,
|
||||
state_class=SensorStateClass.TOTAL,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="net_electricity_point",
|
||||
translation_key="net_electricity_point",
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="net_electricity_cumulative",
|
||||
translation_key="net_electricity_cumulative",
|
||||
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
||||
device_class=SensorDeviceClass.ENERGY,
|
||||
state_class=SensorStateClass.TOTAL,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="battery",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
device_class=SensorDeviceClass.BATTERY,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="illuminance",
|
||||
native_unit_of_measurement=LIGHT_LUX,
|
||||
device_class=SensorDeviceClass.ILLUMINANCE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="modulation_level",
|
||||
translation_key="modulation_level",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="valve_position",
|
||||
translation_key="valve_position",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="water_pressure",
|
||||
translation_key="water_pressure",
|
||||
native_unit_of_measurement=UnitOfPressure.BAR,
|
||||
device_class=SensorDeviceClass.PRESSURE,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="humidity",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
device_class=SensorDeviceClass.HUMIDITY,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="dhw_temperature",
|
||||
translation_key="dhw_temperature",
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
PlugwiseSensorEntityDescription(
|
||||
key="domestic_hot_water_setpoint",
|
||||
translation_key="domestic_hot_water_setpoint",
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: PlugwiseConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Smile sensors from a config entry."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
@callback
|
||||
def _add_entities() -> None:
|
||||
"""Add Entities."""
|
||||
if not coordinator.new_devices:
|
||||
return
|
||||
|
||||
async_add_entities(
|
||||
PlugwiseSensorEntity(coordinator, device_id, description)
|
||||
for device_id in coordinator.new_devices
|
||||
if (sensors := coordinator.data.devices[device_id].get("sensors"))
|
||||
for description in SENSORS
|
||||
if description.key in sensors
|
||||
)
|
||||
|
||||
_add_entities()
|
||||
entry.async_on_unload(coordinator.async_add_listener(_add_entities))
|
||||
|
||||
|
||||
class PlugwiseSensorEntity(PlugwiseEntity, SensorEntity):
|
||||
"""Represent Plugwise Sensors."""
|
||||
|
||||
entity_description: PlugwiseSensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: PlugwiseDataUpdateCoordinator,
|
||||
device_id: str,
|
||||
description: PlugwiseSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialise the sensor."""
|
||||
super().__init__(coordinator, device_id)
|
||||
self._attr_unique_id = f"{device_id}-{description.key}"
|
||||
self.entity_description = description
|
||||
|
||||
@property
|
||||
def native_value(self) -> int | float:
|
||||
"""Return the value reported by the sensor."""
|
||||
return self.device["sensors"][self.entity_description.key]
|
||||
Reference in New Issue
Block a user