1 """Support for MelCloud device sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
9 from pymelcloud
import DEVICE_TYPE_ATA, DEVICE_TYPE_ATW
10 from pymelcloud.atw_device
import Zone
15 SensorEntityDescription,
23 from .
import MelCloudDevice
24 from .const
import DOMAIN
27 @dataclasses.dataclass(frozen=True, kw_only=True)
29 """Describes Melcloud sensor entity."""
31 value_fn: Callable[[Any], float]
32 enabled: Callable[[Any], bool]
35 ATA_SENSORS: tuple[MelcloudSensorEntityDescription, ...] = (
37 key=
"room_temperature",
38 translation_key=
"room_temperature",
39 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
40 device_class=SensorDeviceClass.TEMPERATURE,
41 state_class=SensorStateClass.MEASUREMENT,
42 value_fn=
lambda x: x.device.room_temperature,
43 enabled=
lambda x:
True,
47 translation_key=
"energy_consumed",
48 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
49 device_class=SensorDeviceClass.ENERGY,
50 state_class=SensorStateClass.TOTAL_INCREASING,
51 value_fn=
lambda x: x.device.total_energy_consumed,
52 enabled=
lambda x: x.device.has_energy_consumed_meter,
55 ATW_SENSORS: tuple[MelcloudSensorEntityDescription, ...] = (
57 key=
"outside_temperature",
58 translation_key=
"outside_temperature",
59 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
60 device_class=SensorDeviceClass.TEMPERATURE,
61 state_class=SensorStateClass.MEASUREMENT,
62 value_fn=
lambda x: x.device.outside_temperature,
63 enabled=
lambda x:
True,
66 key=
"tank_temperature",
67 translation_key=
"tank_temperature",
68 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
69 device_class=SensorDeviceClass.TEMPERATURE,
70 state_class=SensorStateClass.MEASUREMENT,
71 value_fn=
lambda x: x.device.tank_temperature,
72 enabled=
lambda x:
True,
75 ATW_ZONE_SENSORS: tuple[MelcloudSensorEntityDescription, ...] = (
77 key=
"room_temperature",
78 translation_key=
"room_temperature",
79 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
80 device_class=SensorDeviceClass.TEMPERATURE,
81 state_class=SensorStateClass.MEASUREMENT,
82 value_fn=
lambda zone: zone.room_temperature,
83 enabled=
lambda x:
True,
86 key=
"flow_temperature",
87 translation_key=
"flow_temperature",
88 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
89 device_class=SensorDeviceClass.TEMPERATURE,
90 state_class=SensorStateClass.MEASUREMENT,
91 value_fn=
lambda zone: zone.flow_temperature,
92 enabled=
lambda x:
True,
95 key=
"return_temperature",
96 translation_key=
"return_temperature",
97 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
98 device_class=SensorDeviceClass.TEMPERATURE,
99 state_class=SensorStateClass.MEASUREMENT,
100 value_fn=
lambda zone: zone.return_temperature,
101 enabled=
lambda x:
True,
107 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
109 """Set up MELCloud device sensors based on config_entry."""
110 mel_devices = hass.data[DOMAIN].
get(entry.entry_id)
112 entities: list[MelDeviceSensor] = [
114 for description
in ATA_SENSORS
115 for mel_device
in mel_devices[DEVICE_TYPE_ATA]
116 if description.enabled(mel_device)
119 for description
in ATW_SENSORS
120 for mel_device
in mel_devices[DEVICE_TYPE_ATW]
121 if description.enabled(mel_device)
126 for mel_device
in mel_devices[DEVICE_TYPE_ATW]
127 for zone
in mel_device.device.zones
128 for description
in ATW_ZONE_SENSORS
129 if description.enabled(zone)
136 """Representation of a Sensor."""
138 entity_description: MelcloudSensorEntityDescription
139 _attr_has_entity_name =
True
144 description: MelcloudSensorEntityDescription,
146 """Initialize the sensor."""
150 self.
_attr_unique_id_attr_unique_id = f
"{api.device.serial}-{api.device.mac}-{description.key}"
155 """Return the state of the sensor."""
159 """Retrieve latest state."""
164 """Air-to-Air device sensor."""
170 description: MelcloudSensorEntityDescription,
172 """Initialize the sensor."""
173 if zone.zone_index != 1:
174 description = dataclasses.replace(
176 key=f
"{description.key}-zone-{zone.zone_index}",
185 """Return zone based state."""
None __init__(self, MelCloudDevice api, Zone zone, MelcloudSensorEntityDescription description)
float|None native_value(self)
None __init__(self, MelCloudDevice api, MelcloudSensorEntityDescription description)
float|None native_value(self)
web.Response get(self, web.Request request, str config_key)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)