Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Nexia / Trane XL Thermostats."""
2 
3 from __future__ import annotations
4 
5 from nexia.const import UNIT_CELSIUS
6 from nexia.thermostat import NexiaThermostat
7 
9  SensorDeviceClass,
10  SensorEntity,
11  SensorStateClass,
12 )
13 from homeassistant.const import PERCENTAGE, UnitOfTemperature
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from .entity import NexiaThermostatEntity, NexiaThermostatZoneEntity
18 from .types import NexiaConfigEntry
19 from .util import percent_conv
20 
21 
23  hass: HomeAssistant,
24  config_entry: NexiaConfigEntry,
25  async_add_entities: AddEntitiesCallback,
26 ) -> None:
27  """Set up sensors for a Nexia device."""
28 
29  coordinator = config_entry.runtime_data
30  nexia_home = coordinator.nexia_home
31  entities: list[NexiaThermostatEntity] = []
32 
33  # Thermostat / System Sensors
34  for thermostat_id in nexia_home.get_thermostat_ids():
35  thermostat: NexiaThermostat = nexia_home.get_thermostat_by_id(thermostat_id)
36 
37  entities.append(
39  coordinator,
40  thermostat,
41  "get_system_status",
42  "system_status",
43  None,
44  None,
45  None,
46  )
47  )
48  # Air cleaner
49  entities.append(
51  coordinator,
52  thermostat,
53  "get_air_cleaner_mode",
54  "air_cleaner_mode",
55  None,
56  None,
57  None,
58  )
59  )
60  # Compressor Speed
61  if thermostat.has_variable_speed_compressor():
62  entities.append(
64  coordinator,
65  thermostat,
66  "get_current_compressor_speed",
67  "current_compressor_speed",
68  None,
69  PERCENTAGE,
70  SensorStateClass.MEASUREMENT,
71  percent_conv,
72  )
73  )
74  entities.append(
76  coordinator,
77  thermostat,
78  "get_requested_compressor_speed",
79  "requested_compressor_speed",
80  None,
81  PERCENTAGE,
82  SensorStateClass.MEASUREMENT,
83  percent_conv,
84  )
85  )
86  # Outdoor Temperature
87  if thermostat.has_outdoor_temperature():
88  if thermostat.get_unit() == UNIT_CELSIUS:
89  unit = UnitOfTemperature.CELSIUS
90  else:
91  unit = UnitOfTemperature.FAHRENHEIT
92  entities.append(
94  coordinator,
95  thermostat,
96  "get_outdoor_temperature",
97  "outdoor_temperature",
98  SensorDeviceClass.TEMPERATURE,
99  unit,
100  SensorStateClass.MEASUREMENT,
101  )
102  )
103  # Relative Humidity
104  if thermostat.has_relative_humidity():
105  entities.append(
107  coordinator,
108  thermostat,
109  "get_relative_humidity",
110  None,
111  SensorDeviceClass.HUMIDITY,
112  PERCENTAGE,
113  SensorStateClass.MEASUREMENT,
114  percent_conv,
115  )
116  )
117 
118  # Zone Sensors
119  for zone_id in thermostat.get_zone_ids():
120  zone = thermostat.get_zone_by_id(zone_id)
121  if thermostat.get_unit() == UNIT_CELSIUS:
122  unit = UnitOfTemperature.CELSIUS
123  else:
124  unit = UnitOfTemperature.FAHRENHEIT
125  # Temperature
126  entities.append(
128  coordinator,
129  zone,
130  "get_temperature",
131  None,
132  SensorDeviceClass.TEMPERATURE,
133  unit,
134  SensorStateClass.MEASUREMENT,
135  None,
136  )
137  )
138  # Zone Status
139  entities.append(
141  coordinator, zone, "get_status", "zone_status", None, None, None
142  )
143  )
144  # Setpoint Status
145  entities.append(
147  coordinator,
148  zone,
149  "get_setpoint_status",
150  "zone_setpoint_status",
151  None,
152  None,
153  None,
154  )
155  )
156 
157  async_add_entities(entities)
158 
159 
161  """Provides Nexia thermostat sensor support."""
162 
163  def __init__(
164  self,
165  coordinator,
166  thermostat,
167  sensor_call,
168  translation_key,
169  sensor_class,
170  sensor_unit,
171  state_class,
172  modifier=None,
173  ):
174  """Initialize the sensor."""
175  super().__init__(
176  coordinator,
177  thermostat,
178  unique_id=f"{thermostat.thermostat_id}_{sensor_call}",
179  )
180  self._call_call = sensor_call
181  self._modifier_modifier = modifier
182  self._attr_device_class_attr_device_class = sensor_class
183  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = sensor_unit
184  self._attr_state_class_attr_state_class = state_class
185  if translation_key is not None:
186  self._attr_translation_key_attr_translation_key = translation_key
187 
188  @property
189  def native_value(self):
190  """Return the state of the sensor."""
191  val = getattr(self._thermostat_thermostat, self._call_call)()
192  if self._modifier_modifier:
193  val = self._modifier_modifier(val)
194  if isinstance(val, float):
195  val = round(val, 1)
196  return val
197 
198 
200  """Nexia Zone Sensor Support."""
201 
202  def __init__(
203  self,
204  coordinator,
205  zone,
206  sensor_call,
207  translation_key,
208  sensor_class,
209  sensor_unit,
210  state_class,
211  modifier=None,
212  ):
213  """Create a zone sensor."""
214 
215  super().__init__(
216  coordinator,
217  zone,
218  unique_id=f"{zone.zone_id}_{sensor_call}",
219  )
220  self._call_call = sensor_call
221  self._modifier_modifier = modifier
222  self._attr_device_class_attr_device_class = sensor_class
223  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = sensor_unit
224  self._attr_state_class_attr_state_class = state_class
225  if translation_key is not None:
226  self._attr_translation_key_attr_translation_key = translation_key
227 
228  @property
229  def native_value(self):
230  """Return the state of the sensor."""
231  val = getattr(self._zone_zone, self._call_call)()
232  if self._modifier_modifier:
233  val = self._modifier_modifier(val)
234  if isinstance(val, float):
235  val = round(val, 1)
236  return val
def __init__(self, coordinator, thermostat, sensor_call, translation_key, sensor_class, sensor_unit, state_class, modifier=None)
Definition: sensor.py:173
def __init__(self, coordinator, zone, sensor_call, translation_key, sensor_class, sensor_unit, state_class, modifier=None)
Definition: sensor.py:212
None async_setup_entry(HomeAssistant hass, NexiaConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:26