Files
ha_rdz_pdc_config/sensor.py
T
2025-02-13 17:08:01 +02:00

86 lines
3.6 KiB
Python

"""
Place this code in custom_components/heat_pump_monitor/sensor.py
"""
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.const import UnitOfTemperature, UnitOfTime
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from ha_rdz_pdc_config import DOMAIN, HeatPumpDataCoordinator
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up sensors."""
coordinator = hass.data[DOMAIN][entry.entry_id]
entities = [
HeatPumpTemperatureSensor(coordinator, "inlet_setpoint_temp", "Inlet Setpoint Temperature"),
HeatPumpTemperatureSensor(coordinator, "system_setpoint_temp", "System Setpoint Temperature"),
HeatPumpTemperatureSensor(coordinator, "water_temp_t1", "Inflow Temperature T1"),
HeatPumpTemperatureSensor(coordinator, "water_temp_t2", "Outflow Temperature T2"),
HeatPumpTemperatureSensor(coordinator, "dhw_temp_t3", "DHW Temperature T3"),
HeatPumpTemperatureSensor(coordinator, "outdoor_temp_t4", "Outdoor Temperature T4"),
HeatPumpTemperatureSensor(coordinator, "water_setpoint", "Water Setpoint"),
HeatPumpTemperatureSensor(coordinator, "dhw_setpoint", "DHW Setpoint"),
HeatPumpTemperatureSensor(coordinator, "aux_heater", "Auxiliary Heater"),
HeatPumpTimeSensor(coordinator, "worktime_pdc", "Runtime"),
HeatPumpTimeSensor(coordinator, "worktime_res", "Auxiliary Heater Runtime"),
HeatPumpVersionSensor(coordinator, "software_version", "Software Version"),
]
async_add_entities(entities)
class HeatPumpTemperatureSensor(CoordinatorEntity, SensorEntity):
"""Temperature sensor for heat pump."""
def __init__(self, coordinator, sensor_type, name):
"""Initialize the sensor."""
super().__init__(coordinator)
self.sensor_type = sensor_type
self._attr_name = f"Heat Pump {name}"
self._attr_unique_id = f"{DOMAIN}_{sensor_type}"
self._attr_device_class = SensorDeviceClass.TEMPERATURE
self._attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
self._attr_state_class = SensorStateClass.MEASUREMENT
@property
def native_value(self):
"""Return the state of the sensor."""
return self.coordinator.data.get(self.sensor_type)
class HeatPumpTimeSensor(CoordinatorEntity, SensorEntity):
"""Time sensor for heat pump operation hours."""
def __init__(self, coordinator, sensor_type, name):
"""Initialize the sensor."""
super().__init__(coordinator)
self.sensor_type = sensor_type
self._attr_name = f"Heat Pump {name}"
self._attr_unique_id = f"{DOMAIN}_{sensor_type}"
self._attr_device_class = SensorDeviceClass.DURATION
self._attr_native_unit_of_measurement = UnitOfTime.HOURS
self._attr_state_class = SensorStateClass.TOTAL_INCREASING
@property
def native_value(self):
"""Return the state of the sensor."""
# Convert the value from hours to seconds for Home Assistant
hours = self.coordinator.data.get(self.sensor_type, 0)
return hours
class HeatPumpVersionSensor(CoordinatorEntity, SensorEntity):
"""Software version sensor for heat pump."""
def __init__(self, coordinator, sensor_type, name):
"""Initialize the sensor."""
super().__init__(coordinator)
self.sensor_type = sensor_type
self._attr_name = f"Heat Pump {name}"
self._attr_unique_id = f"{DOMAIN}_{sensor_type}"
@property
def native_value(self):
"""Return the state of the sensor."""
return self.coordinator.data.get(self.sensor_type)