Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Honeywell (US) Total Connect Comfort sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from aiosomecomfort.device import Device
10 
12  SensorDeviceClass,
13  SensorEntity,
14  SensorEntityDescription,
15  SensorStateClass,
16 )
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.const import PERCENTAGE, UnitOfTemperature
19 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.device_registry import DeviceInfo
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 from homeassistant.helpers.typing import StateType
23 
24 from . import HoneywellData
25 from .const import DOMAIN
26 
27 OUTDOOR_TEMPERATURE_STATUS_KEY = "outdoor_temperature"
28 OUTDOOR_HUMIDITY_STATUS_KEY = "outdoor_humidity"
29 CURRENT_TEMPERATURE_STATUS_KEY = "current_temperature"
30 CURRENT_HUMIDITY_STATUS_KEY = "current_humidity"
31 
32 
33 def _get_temperature_sensor_unit(device: Device) -> str:
34  """Get the correct temperature unit for the device."""
35  if device.temperature_unit == "C":
36  return UnitOfTemperature.CELSIUS
37  return UnitOfTemperature.FAHRENHEIT
38 
39 
40 @dataclass(frozen=True, kw_only=True)
42  """Describes a Honeywell sensor entity."""
43 
44  value_fn: Callable[[Device], Any]
45  unit_fn: Callable[[Device], Any]
46 
47 
48 SENSOR_TYPES: tuple[HoneywellSensorEntityDescription, ...] = (
50  key=OUTDOOR_TEMPERATURE_STATUS_KEY,
51  translation_key=OUTDOOR_TEMPERATURE_STATUS_KEY,
52  device_class=SensorDeviceClass.TEMPERATURE,
53  state_class=SensorStateClass.MEASUREMENT,
54  value_fn=lambda device: device.outdoor_temperature,
55  unit_fn=_get_temperature_sensor_unit,
56  ),
58  key=OUTDOOR_HUMIDITY_STATUS_KEY,
59  translation_key=OUTDOOR_HUMIDITY_STATUS_KEY,
60  device_class=SensorDeviceClass.HUMIDITY,
61  state_class=SensorStateClass.MEASUREMENT,
62  value_fn=lambda device: device.outdoor_humidity,
63  unit_fn=lambda device: PERCENTAGE,
64  ),
66  key=CURRENT_TEMPERATURE_STATUS_KEY,
67  device_class=SensorDeviceClass.TEMPERATURE,
68  state_class=SensorStateClass.MEASUREMENT,
69  value_fn=lambda device: device.current_temperature,
70  unit_fn=_get_temperature_sensor_unit,
71  ),
73  key=CURRENT_HUMIDITY_STATUS_KEY,
74  device_class=SensorDeviceClass.HUMIDITY,
75  state_class=SensorStateClass.MEASUREMENT,
76  value_fn=lambda device: device.current_humidity,
77  unit_fn=lambda device: PERCENTAGE,
78  ),
79 )
80 
81 
83  hass: HomeAssistant,
84  config_entry: ConfigEntry,
85  async_add_entities: AddEntitiesCallback,
86 ) -> None:
87  """Set up the Honeywell thermostat."""
88  data: HoneywellData = hass.data[DOMAIN][config_entry.entry_id]
89 
91  HoneywellSensor(device, description)
92  for device in data.devices.values()
93  for description in SENSOR_TYPES
94  if getattr(device, description.key) is not None
95  )
96 
97 
99  """Representation of a Honeywell US Outdoor Temperature Sensor."""
100 
101  entity_description: HoneywellSensorEntityDescription
102  _attr_has_entity_name = True
103 
104  def __init__(self, device, description) -> None:
105  """Initialize the outdoor temperature sensor."""
106  self._device_device = device
107  self.entity_descriptionentity_description = description
108  self._attr_unique_id_attr_unique_id = f"{device.deviceid}_{description.key}"
109  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = description.unit_fn(device)
110 
111  self._attr_device_info_attr_device_info = DeviceInfo(
112  identifiers={(DOMAIN, device.deviceid)},
113  name=device.name,
114  manufacturer="Honeywell",
115  )
116 
117  @property
118  def native_value(self) -> StateType:
119  """Return the state."""
120  return self.entity_descriptionentity_description.value_fn(self._device_device)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:86
str _get_temperature_sensor_unit(Device device)
Definition: sensor.py:33