Home Assistant Unofficial Reference 2024.12.1
const.py
Go to the documentation of this file.
1 """Provides the constants needed for the component."""
2 
3 from __future__ import annotations
4 
5 from enum import StrEnum
6 from functools import partial
7 from typing import Final
8 
9 import voluptuous as vol
10 
11 from homeassistant.const import (
12  CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
13  CONCENTRATION_PARTS_PER_BILLION,
14  CONCENTRATION_PARTS_PER_MILLION,
15  LIGHT_LUX,
16  PERCENTAGE,
17  SIGNAL_STRENGTH_DECIBELS,
18  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
19  UnitOfApparentPower,
20  UnitOfArea,
21  UnitOfBloodGlucoseConcentration,
22  UnitOfConductivity,
23  UnitOfDataRate,
24  UnitOfElectricCurrent,
25  UnitOfElectricPotential,
26  UnitOfEnergy,
27  UnitOfFrequency,
28  UnitOfInformation,
29  UnitOfIrradiance,
30  UnitOfLength,
31  UnitOfMass,
32  UnitOfPower,
33  UnitOfPrecipitationDepth,
34  UnitOfPressure,
35  UnitOfReactivePower,
36  UnitOfSoundPressure,
37  UnitOfSpeed,
38  UnitOfTemperature,
39  UnitOfTime,
40  UnitOfVolume,
41  UnitOfVolumeFlowRate,
42  UnitOfVolumetricFlux,
43 )
45  DeprecatedConstantEnum,
46  all_with_deprecated_constants,
47  check_if_deprecated_constant,
48  dir_with_deprecated_constants,
49 )
51  BaseUnitConverter,
52  TemperatureConverter,
53  VolumeFlowRateConverter,
54 )
55 
56 ATTR_VALUE = "value"
57 ATTR_MIN = "min"
58 ATTR_MAX = "max"
59 ATTR_STEP = "step"
60 ATTR_STEP_VALIDATION = "step_validation"
61 
62 DEFAULT_MIN_VALUE = 0.0
63 DEFAULT_MAX_VALUE = 100.0
64 DEFAULT_STEP = 1.0
65 
66 DOMAIN = "number"
67 
68 SERVICE_SET_VALUE = "set_value"
69 
70 
71 class NumberMode(StrEnum):
72  """Modes for number entities."""
73 
74  AUTO = "auto"
75  BOX = "box"
76  SLIDER = "slider"
77 
78 
79 # MODE_* are deprecated as of 2021.12, use the NumberMode enum instead.
80 _DEPRECATED_MODE_AUTO: Final = DeprecatedConstantEnum(NumberMode.AUTO, "2025.1")
81 _DEPRECATED_MODE_BOX: Final = DeprecatedConstantEnum(NumberMode.BOX, "2025.1")
82 _DEPRECATED_MODE_SLIDER: Final = DeprecatedConstantEnum(NumberMode.SLIDER, "2025.1")
83 
84 
85 class NumberDeviceClass(StrEnum):
86  """Device class for numbers."""
87 
88  # NumberDeviceClass should be aligned with SensorDeviceClass
89 
90  APPARENT_POWER = "apparent_power"
91  """Apparent power.
92 
93  Unit of measurement: `VA`
94  """
95 
96  AQI = "aqi"
97  """Air Quality Index.
98 
99  Unit of measurement: `None`
100  """
101 
102  AREA = "area"
103  """Area
104 
105  Unit of measurement: `UnitOfArea` units
106  """
107 
108  ATMOSPHERIC_PRESSURE = "atmospheric_pressure"
109  """Atmospheric pressure.
110 
111  Unit of measurement: `UnitOfPressure` units
112  """
113 
114  BATTERY = "battery"
115  """Percentage of battery that is left.
116 
117  Unit of measurement: `%`
118  """
119 
120  BLOOD_GLUCOSE_CONCENTRATION = "blood_glucose_concentration"
121  """Blood glucose concentration.
122 
123  Unit of measurement: `mg/dL`, `mmol/L`
124  """
125 
126  CO = "carbon_monoxide"
127  """Carbon Monoxide gas concentration.
128 
129  Unit of measurement: `ppm` (parts per million)
130  """
131 
132  CO2 = "carbon_dioxide"
133  """Carbon Dioxide gas concentration.
134 
135  Unit of measurement: `ppm` (parts per million)
136  """
137 
138  CONDUCTIVITY = "conductivity"
139  """Conductivity.
140 
141  Unit of measurement: `S/cm`, `mS/cm`, `µS/cm`
142  """
143 
144  CURRENT = "current"
145  """Current.
146 
147  Unit of measurement: `A`, `mA`
148  """
149 
150  DATA_RATE = "data_rate"
151  """Data rate.
152 
153  Unit of measurement: UnitOfDataRate
154  """
155 
156  DATA_SIZE = "data_size"
157  """Data size.
158 
159  Unit of measurement: UnitOfInformation
160  """
161 
162  DISTANCE = "distance"
163  """Generic distance.
164 
165  Unit of measurement: `LENGTH_*` units
166  - SI /metric: `mm`, `cm`, `m`, `km`
167  - USCS / imperial: `in`, `ft`, `yd`, `mi`
168  """
169 
170  DURATION = "duration"
171  """Fixed duration.
172 
173  Unit of measurement: `d`, `h`, `min`, `s`, `ms`
174  """
175 
176  ENERGY = "energy"
177  """Energy.
178 
179  Unit of measurement: `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `MJ`, `GJ`
180  """
181 
182  ENERGY_STORAGE = "energy_storage"
183  """Stored energy.
184 
185  Use this device class for sensors measuring stored energy, for example the amount
186  of electric energy currently stored in a battery or the capacity of a battery.
187 
188  Unit of measurement: `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `MJ`, `GJ`
189  """
190 
191  FREQUENCY = "frequency"
192  """Frequency.
193 
194  Unit of measurement: `Hz`, `kHz`, `MHz`, `GHz`
195  """
196 
197  GAS = "gas"
198  """Gas.
199 
200  Unit of measurement:
201  - SI / metric: `m³`
202  - USCS / imperial: `ft³`, `CCF`
203  """
204 
205  HUMIDITY = "humidity"
206  """Relative humidity.
207 
208  Unit of measurement: `%`
209  """
210 
211  ILLUMINANCE = "illuminance"
212  """Illuminance.
213 
214  Unit of measurement: `lx`
215  """
216 
217  IRRADIANCE = "irradiance"
218  """Irradiance.
219 
220  Unit of measurement:
221  - SI / metric: `W/m²`
222  - USCS / imperial: `BTU/(h⋅ft²)`
223  """
224 
225  MOISTURE = "moisture"
226  """Moisture.
227 
228  Unit of measurement: `%`
229  """
230 
231  MONETARY = "monetary"
232  """Amount of money.
233 
234  Unit of measurement: ISO4217 currency code
235 
236  See https://en.wikipedia.org/wiki/ISO_4217#Active_codes for active codes
237  """
238 
239  NITROGEN_DIOXIDE = "nitrogen_dioxide"
240  """Amount of NO2.
241 
242  Unit of measurement: `µg/m³`
243  """
244 
245  NITROGEN_MONOXIDE = "nitrogen_monoxide"
246  """Amount of NO.
247 
248  Unit of measurement: `µg/m³`
249  """
250 
251  NITROUS_OXIDE = "nitrous_oxide"
252  """Amount of N2O.
253 
254  Unit of measurement: `µg/m³`
255  """
256 
257  OZONE = "ozone"
258  """Amount of O3.
259 
260  Unit of measurement: `µg/m³`
261  """
262 
263  PH = "ph"
264  """Potential hydrogen (acidity/alkalinity).
265 
266  Unit of measurement: Unitless
267  """
268 
269  PM1 = "pm1"
270  """Particulate matter <= 1 μm.
271 
272  Unit of measurement: `µg/m³`
273  """
274 
275  PM10 = "pm10"
276  """Particulate matter <= 10 μm.
277 
278  Unit of measurement: `µg/m³`
279  """
280 
281  PM25 = "pm25"
282  """Particulate matter <= 2.5 μm.
283 
284  Unit of measurement: `µg/m³`
285  """
286 
287  POWER_FACTOR = "power_factor"
288  """Power factor.
289 
290  Unit of measurement: `%`, `None`
291  """
292 
293  POWER = "power"
294  """Power.
295 
296  Unit of measurement: `W`, `kW`, `MW`, `GW`, `TW`
297  """
298 
299  PRECIPITATION = "precipitation"
300  """Accumulated precipitation.
301 
302  Unit of measurement: UnitOfPrecipitationDepth
303  - SI / metric: `cm`, `mm`
304  - USCS / imperial: `in`
305  """
306 
307  PRECIPITATION_INTENSITY = "precipitation_intensity"
308  """Precipitation intensity.
309 
310  Unit of measurement: UnitOfVolumetricFlux
311  - SI /metric: `mm/d`, `mm/h`
312  - USCS / imperial: `in/d`, `in/h`
313  """
314 
315  PRESSURE = "pressure"
316  """Pressure.
317 
318  Unit of measurement:
319  - `mbar`, `cbar`, `bar`
320  - `Pa`, `hPa`, `kPa`
321  - `inHg`
322  - `psi`
323  """
324 
325  REACTIVE_POWER = "reactive_power"
326  """Reactive power.
327 
328  Unit of measurement: `var`
329  """
330 
331  SIGNAL_STRENGTH = "signal_strength"
332  """Signal strength.
333 
334  Unit of measurement: `dB`, `dBm`
335  """
336 
337  SOUND_PRESSURE = "sound_pressure"
338  """Sound pressure.
339 
340  Unit of measurement: `dB`, `dBA`
341  """
342 
343  SPEED = "speed"
344  """Generic speed.
345 
346  Unit of measurement: `SPEED_*` units or `UnitOfVolumetricFlux`
347  - SI /metric: `mm/d`, `mm/h`, `m/s`, `km/h`
348  - USCS / imperial: `in/d`, `in/h`, `ft/s`, `mph`
349  - Nautical: `kn`
350  """
351 
352  SULPHUR_DIOXIDE = "sulphur_dioxide"
353  """Amount of SO2.
354 
355  Unit of measurement: `µg/m³`
356  """
357 
358  TEMPERATURE = "temperature"
359  """Temperature.
360 
361  Unit of measurement: `°C`, `°F`, `K`
362  """
363 
364  VOLATILE_ORGANIC_COMPOUNDS = "volatile_organic_compounds"
365  """Amount of VOC.
366 
367  Unit of measurement: `µg/m³`
368  """
369 
370  VOLATILE_ORGANIC_COMPOUNDS_PARTS = "volatile_organic_compounds_parts"
371  """Ratio of VOC.
372 
373  Unit of measurement: `ppm`, `ppb`
374  """
375 
376  VOLTAGE = "voltage"
377  """Voltage.
378 
379  Unit of measurement: `V`, `mV`, `µV`
380  """
381 
382  VOLUME = "volume"
383  """Generic volume.
384 
385  Unit of measurement: `VOLUME_*` units
386  - SI / metric: `mL`, `L`, `m³`
387  - USCS / imperial: `ft³`, `CCF`, `fl. oz.`, `gal` (warning: volumes expressed in
388  USCS/imperial units are currently assumed to be US volumes)
389  """
390 
391  VOLUME_STORAGE = "volume_storage"
392  """Generic stored volume.
393 
394  Use this device class for sensors measuring stored volume, for example the amount
395  of fuel in a fuel tank.
396 
397  Unit of measurement: `VOLUME_*` units
398  - SI / metric: `mL`, `L`, `m³`
399  - USCS / imperial: `ft³`, `CCF`, `fl. oz.`, `gal` (warning: volumes expressed in
400  USCS/imperial units are currently assumed to be US volumes)
401  """
402 
403  VOLUME_FLOW_RATE = "volume_flow_rate"
404  """Generic flow rate
405 
406  Unit of measurement: UnitOfVolumeFlowRate
407  - SI / metric: `m³/h`, `L/min`, `mL/s`
408  - USCS / imperial: `ft³/min`, `gal/min`
409  """
410 
411  WATER = "water"
412  """Water.
413 
414  Unit of measurement:
415  - SI / metric: `m³`, `L`
416  - USCS / imperial: `ft³`, `CCF`, `gal` (warning: volumes expressed in
417  USCS/imperial units are currently assumed to be US volumes)
418  """
419 
420  WEIGHT = "weight"
421  """Generic weight, represents a measurement of an object's mass.
422 
423  Weight is used instead of mass to fit with every day language.
424 
425  Unit of measurement: `MASS_*` units
426  - SI / metric: `µg`, `mg`, `g`, `kg`
427  - USCS / imperial: `oz`, `lb`
428  """
429 
430  WIND_SPEED = "wind_speed"
431  """Wind speed.
432 
433  Unit of measurement: `SPEED_*` units
434  - SI /metric: `m/s`, `km/h`
435  - USCS / imperial: `ft/s`, `mph`
436  - Nautical: `kn`
437  """
438 
439 
440 DEVICE_CLASSES_SCHEMA: Final = vol.All(vol.Lower, vol.Coerce(NumberDeviceClass))
441 DEVICE_CLASS_UNITS: dict[NumberDeviceClass, set[type[StrEnum] | str | None]] = {
442  NumberDeviceClass.APPARENT_POWER: set(UnitOfApparentPower),
443  NumberDeviceClass.AQI: {None},
444  NumberDeviceClass.AREA: set(UnitOfArea),
445  NumberDeviceClass.ATMOSPHERIC_PRESSURE: set(UnitOfPressure),
446  NumberDeviceClass.BATTERY: {PERCENTAGE},
447  NumberDeviceClass.BLOOD_GLUCOSE_CONCENTRATION: set(UnitOfBloodGlucoseConcentration),
448  NumberDeviceClass.CO: {CONCENTRATION_PARTS_PER_MILLION},
449  NumberDeviceClass.CO2: {CONCENTRATION_PARTS_PER_MILLION},
450  NumberDeviceClass.CONDUCTIVITY: set(UnitOfConductivity),
451  NumberDeviceClass.CURRENT: set(UnitOfElectricCurrent),
452  NumberDeviceClass.DATA_RATE: set(UnitOfDataRate),
453  NumberDeviceClass.DATA_SIZE: set(UnitOfInformation),
454  NumberDeviceClass.DISTANCE: set(UnitOfLength),
455  NumberDeviceClass.DURATION: {
456  UnitOfTime.DAYS,
457  UnitOfTime.HOURS,
458  UnitOfTime.MINUTES,
459  UnitOfTime.SECONDS,
460  UnitOfTime.MILLISECONDS,
461  },
462  NumberDeviceClass.ENERGY: set(UnitOfEnergy),
463  NumberDeviceClass.ENERGY_STORAGE: set(UnitOfEnergy),
464  NumberDeviceClass.FREQUENCY: set(UnitOfFrequency),
465  NumberDeviceClass.GAS: {
466  UnitOfVolume.CENTUM_CUBIC_FEET,
467  UnitOfVolume.CUBIC_FEET,
468  UnitOfVolume.CUBIC_METERS,
469  },
470  NumberDeviceClass.HUMIDITY: {PERCENTAGE},
471  NumberDeviceClass.ILLUMINANCE: {LIGHT_LUX},
472  NumberDeviceClass.IRRADIANCE: set(UnitOfIrradiance),
473  NumberDeviceClass.MOISTURE: {PERCENTAGE},
474  NumberDeviceClass.NITROGEN_DIOXIDE: {CONCENTRATION_MICROGRAMS_PER_CUBIC_METER},
475  NumberDeviceClass.NITROGEN_MONOXIDE: {CONCENTRATION_MICROGRAMS_PER_CUBIC_METER},
476  NumberDeviceClass.NITROUS_OXIDE: {CONCENTRATION_MICROGRAMS_PER_CUBIC_METER},
477  NumberDeviceClass.OZONE: {CONCENTRATION_MICROGRAMS_PER_CUBIC_METER},
478  NumberDeviceClass.PH: {None},
479  NumberDeviceClass.PM1: {CONCENTRATION_MICROGRAMS_PER_CUBIC_METER},
480  NumberDeviceClass.PM10: {CONCENTRATION_MICROGRAMS_PER_CUBIC_METER},
481  NumberDeviceClass.PM25: {CONCENTRATION_MICROGRAMS_PER_CUBIC_METER},
482  NumberDeviceClass.POWER_FACTOR: {PERCENTAGE, None},
483  NumberDeviceClass.POWER: {
484  UnitOfPower.WATT,
485  UnitOfPower.KILO_WATT,
486  UnitOfPower.MEGA_WATT,
487  UnitOfPower.GIGA_WATT,
488  UnitOfPower.TERA_WATT,
489  },
490  NumberDeviceClass.PRECIPITATION: set(UnitOfPrecipitationDepth),
491  NumberDeviceClass.PRECIPITATION_INTENSITY: set(UnitOfVolumetricFlux),
492  NumberDeviceClass.PRESSURE: set(UnitOfPressure),
493  NumberDeviceClass.REACTIVE_POWER: {UnitOfReactivePower.VOLT_AMPERE_REACTIVE},
494  NumberDeviceClass.SIGNAL_STRENGTH: {
495  SIGNAL_STRENGTH_DECIBELS,
496  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
497  },
498  NumberDeviceClass.SOUND_PRESSURE: set(UnitOfSoundPressure),
499  NumberDeviceClass.SPEED: set(UnitOfSpeed).union(set(UnitOfVolumetricFlux)),
500  NumberDeviceClass.SULPHUR_DIOXIDE: {CONCENTRATION_MICROGRAMS_PER_CUBIC_METER},
501  NumberDeviceClass.TEMPERATURE: set(UnitOfTemperature),
502  NumberDeviceClass.VOLATILE_ORGANIC_COMPOUNDS: {
503  CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
504  },
505  NumberDeviceClass.VOLATILE_ORGANIC_COMPOUNDS_PARTS: {
506  CONCENTRATION_PARTS_PER_BILLION,
507  CONCENTRATION_PARTS_PER_MILLION,
508  },
509  NumberDeviceClass.VOLTAGE: set(UnitOfElectricPotential),
510  NumberDeviceClass.VOLUME: set(UnitOfVolume),
511  NumberDeviceClass.VOLUME_STORAGE: set(UnitOfVolume),
512  NumberDeviceClass.VOLUME_FLOW_RATE: set(UnitOfVolumeFlowRate),
513  NumberDeviceClass.WATER: {
514  UnitOfVolume.CENTUM_CUBIC_FEET,
515  UnitOfVolume.CUBIC_FEET,
516  UnitOfVolume.CUBIC_METERS,
517  UnitOfVolume.GALLONS,
518  UnitOfVolume.LITERS,
519  },
520  NumberDeviceClass.WEIGHT: set(UnitOfMass),
521  NumberDeviceClass.WIND_SPEED: set(UnitOfSpeed),
522 }
523 
524 UNIT_CONVERTERS: dict[NumberDeviceClass, type[BaseUnitConverter]] = {
525  NumberDeviceClass.TEMPERATURE: TemperatureConverter,
526  NumberDeviceClass.VOLUME_FLOW_RATE: VolumeFlowRateConverter,
527 }
528 
529 # These can be removed if no deprecated constant are in this module anymore
530 __getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
531 __dir__ = partial(
532  dir_with_deprecated_constants, module_globals_keys=[*globals().keys()]
533 )
534 __all__ = all_with_deprecated_constants(globals())
list[str] all_with_deprecated_constants(dict[str, Any] module_globals)
Definition: deprecation.py:356