Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Daikin AC sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from pydaikin.daikin_base import Appliance
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import (
18  PERCENTAGE,
19  UnitOfEnergy,
20  UnitOfFrequency,
21  UnitOfPower,
22  UnitOfTemperature,
23 )
24 from homeassistant.core import HomeAssistant
25 from homeassistant.helpers.entity_platform import AddEntitiesCallback
26 
27 from . import DOMAIN as DAIKIN_DOMAIN
28 from .const import (
29  ATTR_COMPRESSOR_FREQUENCY,
30  ATTR_COOL_ENERGY,
31  ATTR_ENERGY_TODAY,
32  ATTR_HEAT_ENERGY,
33  ATTR_HUMIDITY,
34  ATTR_INSIDE_TEMPERATURE,
35  ATTR_OUTSIDE_TEMPERATURE,
36  ATTR_TARGET_HUMIDITY,
37  ATTR_TOTAL_ENERGY_TODAY,
38  ATTR_TOTAL_POWER,
39 )
40 from .coordinator import DaikinCoordinator
41 from .entity import DaikinEntity
42 
43 
44 @dataclass(frozen=True, kw_only=True)
46  """Describes Daikin sensor entity."""
47 
48  value_func: Callable[[Appliance], float | None]
49 
50 
51 SENSOR_TYPES: tuple[DaikinSensorEntityDescription, ...] = (
53  key=ATTR_INSIDE_TEMPERATURE,
54  translation_key="inside_temperature",
55  device_class=SensorDeviceClass.TEMPERATURE,
56  state_class=SensorStateClass.MEASUREMENT,
57  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
58  value_func=lambda device: device.inside_temperature,
59  ),
61  key=ATTR_OUTSIDE_TEMPERATURE,
62  translation_key="outside_temperature",
63  device_class=SensorDeviceClass.TEMPERATURE,
64  state_class=SensorStateClass.MEASUREMENT,
65  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
66  value_func=lambda device: device.outside_temperature,
67  ),
69  key=ATTR_HUMIDITY,
70  device_class=SensorDeviceClass.HUMIDITY,
71  state_class=SensorStateClass.MEASUREMENT,
72  native_unit_of_measurement=PERCENTAGE,
73  value_func=lambda device: device.humidity,
74  ),
76  key=ATTR_TARGET_HUMIDITY,
77  translation_key="target_humidity",
78  device_class=SensorDeviceClass.HUMIDITY,
79  state_class=SensorStateClass.MEASUREMENT,
80  native_unit_of_measurement=PERCENTAGE,
81  value_func=lambda device: device.humidity,
82  ),
84  key=ATTR_TOTAL_POWER,
85  translation_key="compressor_estimated_power_consumption",
86  device_class=SensorDeviceClass.POWER,
87  state_class=SensorStateClass.MEASUREMENT,
88  native_unit_of_measurement=UnitOfPower.KILO_WATT,
89  value_func=lambda device: round(device.current_total_power_consumption, 2),
90  ),
92  key=ATTR_COOL_ENERGY,
93  translation_key="cool_energy_consumption",
94  device_class=SensorDeviceClass.ENERGY,
95  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
96  entity_registry_enabled_default=False,
97  value_func=lambda device: round(device.last_hour_cool_energy_consumption, 2),
98  ),
100  key=ATTR_HEAT_ENERGY,
101  translation_key="heat_energy_consumption",
102  device_class=SensorDeviceClass.ENERGY,
103  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
104  entity_registry_enabled_default=False,
105  value_func=lambda device: round(device.last_hour_heat_energy_consumption, 2),
106  ),
108  key=ATTR_ENERGY_TODAY,
109  translation_key="energy_consumption",
110  device_class=SensorDeviceClass.ENERGY,
111  state_class=SensorStateClass.TOTAL_INCREASING,
112  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
113  value_func=lambda device: round(device.today_energy_consumption, 2),
114  ),
116  key=ATTR_COMPRESSOR_FREQUENCY,
117  translation_key="compressor_frequency",
118  device_class=SensorDeviceClass.FREQUENCY,
119  state_class=SensorStateClass.MEASUREMENT,
120  native_unit_of_measurement=UnitOfFrequency.HERTZ,
121  entity_registry_enabled_default=False,
122  value_func=lambda device: device.compressor_frequency,
123  ),
125  key=ATTR_TOTAL_ENERGY_TODAY,
126  translation_key="compressor_energy_consumption",
127  device_class=SensorDeviceClass.ENERGY,
128  state_class=SensorStateClass.TOTAL_INCREASING,
129  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
130  entity_registry_enabled_default=False,
131  value_func=lambda device: round(device.today_total_energy_consumption, 2),
132  ),
133 )
134 
135 
137  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
138 ) -> None:
139  """Set up Daikin climate based on config_entry."""
140  daikin_api = hass.data[DAIKIN_DOMAIN].get(entry.entry_id)
141  sensors = [ATTR_INSIDE_TEMPERATURE]
142  if daikin_api.device.support_outside_temperature:
143  sensors.append(ATTR_OUTSIDE_TEMPERATURE)
144  if daikin_api.device.support_energy_consumption:
145  sensors.append(ATTR_ENERGY_TODAY)
146  sensors.append(ATTR_COOL_ENERGY)
147  sensors.append(ATTR_HEAT_ENERGY)
148  sensors.append(ATTR_TOTAL_POWER)
149  sensors.append(ATTR_TOTAL_ENERGY_TODAY)
150  if daikin_api.device.support_humidity:
151  sensors.append(ATTR_HUMIDITY)
152  sensors.append(ATTR_TARGET_HUMIDITY)
153  if daikin_api.device.support_compressor_frequency:
154  sensors.append(ATTR_COMPRESSOR_FREQUENCY)
155 
156  entities = [
157  DaikinSensor(daikin_api, description)
158  for description in SENSOR_TYPES
159  if description.key in sensors
160  ]
161  async_add_entities(entities)
162 
163 
165  """Representation of a Sensor."""
166 
167  entity_description: DaikinSensorEntityDescription
168 
169  def __init__(
170  self, coordinator: DaikinCoordinator, description: DaikinSensorEntityDescription
171  ) -> None:
172  """Initialize the sensor."""
173  super().__init__(coordinator)
174  self.entity_descriptionentity_description = description
175  self._attr_unique_id_attr_unique_id = f"{self.device.mac}-{description.key}"
176 
177  @property
178  def native_value(self) -> float | None:
179  """Return the state of the sensor."""
180  return self.entity_descriptionentity_description.value_func(self.devicedevicedevice)
None __init__(self, DaikinCoordinator coordinator, DaikinSensorEntityDescription description)
Definition: sensor.py:171
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:138