Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """Support for Big Ass Fans number."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import cast
8 
9 from aiobafi6 import Device
10 
12  NumberEntity,
13  NumberEntityDescription,
14  NumberMode,
15 )
16 from homeassistant.const import EntityCategory, UnitOfTime
17 from homeassistant.core import HomeAssistant, callback
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import BAFConfigEntry
21 from .const import HALF_DAY_SECS, ONE_DAY_SECS, ONE_MIN_SECS, SPEED_RANGE
22 from .entity import BAFDescriptionEntity
23 
24 
25 @dataclass(frozen=True, kw_only=True)
27  """Class describing BAF sensor entities."""
28 
29  value_fn: Callable[[Device], int | None]
30 
31 
32 AUTO_COMFORT_NUMBER_DESCRIPTIONS = (
34  key="comfort_min_speed",
35  translation_key="comfort_min_speed",
36  native_step=1,
37  native_min_value=0,
38  native_max_value=SPEED_RANGE[1] - 1,
39  entity_category=EntityCategory.CONFIG,
40  value_fn=lambda device: cast(int | None, device.comfort_min_speed),
41  mode=NumberMode.BOX,
42  ),
44  key="comfort_max_speed",
45  translation_key="comfort_max_speed",
46  native_step=1,
47  native_min_value=1,
48  native_max_value=SPEED_RANGE[1],
49  entity_category=EntityCategory.CONFIG,
50  value_fn=lambda device: cast(int | None, device.comfort_max_speed),
51  mode=NumberMode.BOX,
52  ),
54  key="comfort_heat_assist_speed",
55  translation_key="comfort_heat_assist_speed",
56  native_step=1,
57  native_min_value=SPEED_RANGE[0],
58  native_max_value=SPEED_RANGE[1],
59  entity_category=EntityCategory.CONFIG,
60  value_fn=lambda device: cast(int | None, device.comfort_heat_assist_speed),
61  mode=NumberMode.BOX,
62  ),
63 )
64 
65 FAN_NUMBER_DESCRIPTIONS = (
67  key="return_to_auto_timeout",
68  translation_key="return_to_auto_timeout",
69  native_step=1,
70  native_min_value=ONE_MIN_SECS,
71  native_max_value=HALF_DAY_SECS,
72  entity_category=EntityCategory.CONFIG,
73  native_unit_of_measurement=UnitOfTime.SECONDS,
74  value_fn=lambda device: cast(int | None, device.return_to_auto_timeout),
75  mode=NumberMode.SLIDER,
76  ),
78  key="motion_sense_timeout",
79  translation_key="motion_sense_timeout",
80  native_step=1,
81  native_min_value=ONE_MIN_SECS,
82  native_max_value=ONE_DAY_SECS,
83  entity_category=EntityCategory.CONFIG,
84  native_unit_of_measurement=UnitOfTime.SECONDS,
85  value_fn=lambda device: cast(int | None, device.motion_sense_timeout),
86  mode=NumberMode.SLIDER,
87  ),
88 )
89 
90 LIGHT_NUMBER_DESCRIPTIONS = (
92  key="light_return_to_auto_timeout",
93  translation_key="light_return_to_auto_timeout",
94  native_step=1,
95  native_min_value=ONE_MIN_SECS,
96  native_max_value=HALF_DAY_SECS,
97  entity_category=EntityCategory.CONFIG,
98  native_unit_of_measurement=UnitOfTime.SECONDS,
99  value_fn=lambda device: cast(int | None, device.light_return_to_auto_timeout),
100  mode=NumberMode.SLIDER,
101  ),
103  key="light_auto_motion_timeout",
104  translation_key="light_auto_motion_timeout",
105  native_step=1,
106  native_min_value=ONE_MIN_SECS,
107  native_max_value=ONE_DAY_SECS,
108  entity_category=EntityCategory.CONFIG,
109  native_unit_of_measurement=UnitOfTime.SECONDS,
110  value_fn=lambda device: cast(int | None, device.light_auto_motion_timeout),
111  mode=NumberMode.SLIDER,
112  ),
113 )
114 
115 
117  hass: HomeAssistant,
118  entry: BAFConfigEntry,
119  async_add_entities: AddEntitiesCallback,
120 ) -> None:
121  """Set up BAF numbers."""
122  device = entry.runtime_data
123  descriptions: list[BAFNumberDescription] = []
124  if device.has_fan:
125  descriptions.extend(FAN_NUMBER_DESCRIPTIONS)
126  if device.has_light:
127  descriptions.extend(LIGHT_NUMBER_DESCRIPTIONS)
128  if device.has_auto_comfort:
129  descriptions.extend(AUTO_COMFORT_NUMBER_DESCRIPTIONS)
130  async_add_entities(BAFNumber(device, description) for description in descriptions)
131 
132 
134  """BAF number."""
135 
136  entity_description: BAFNumberDescription
137 
138  @callback
139  def _async_update_attrs(self) -> None:
140  """Update attrs from device."""
141  if (value := self.entity_descriptionentity_description.value_fn(self._device_device)) is not None:
142  self._attr_native_value_attr_native_value = float(value)
143 
144  async def async_set_native_value(self, value: float) -> None:
145  """Set the value."""
146  setattr(self._device_device, self.entity_descriptionentity_description.key, int(value))
None async_set_native_value(self, float value)
Definition: number.py:144
None async_setup_entry(HomeAssistant hass, BAFConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: number.py:120