Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """The Flexit Nordic (BACnet) integration."""
2 
3 import asyncio.exceptions
4 from collections.abc import Awaitable, Callable
5 from dataclasses import dataclass
6 
7 from flexit_bacnet import FlexitBACnet
8 from flexit_bacnet.bacnet import DecodingError
9 
11  NumberDeviceClass,
12  NumberEntity,
13  NumberEntityDescription,
14  NumberMode,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import PERCENTAGE
18 from homeassistant.core import HomeAssistant
19 from homeassistant.exceptions import HomeAssistantError
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from . import FlexitCoordinator
23 from .const import DOMAIN
24 from .entity import FlexitEntity
25 
26 
27 @dataclass(kw_only=True, frozen=True)
29  """Describes a Flexit number entity."""
30 
31  native_value_fn: Callable[[FlexitBACnet], float]
32  set_native_value_fn: Callable[[FlexitBACnet], Callable[[int], Awaitable[None]]]
33 
34 
35 NUMBERS: tuple[FlexitNumberEntityDescription, ...] = (
37  key="away_extract_fan_setpoint",
38  translation_key="away_extract_fan_setpoint",
39  device_class=NumberDeviceClass.POWER_FACTOR,
40  native_min_value=0,
41  native_max_value=100,
42  native_step=1,
43  mode=NumberMode.SLIDER,
44  native_value_fn=lambda device: device.fan_setpoint_extract_air_away,
45  set_native_value_fn=lambda device: device.set_fan_setpoint_extract_air_away,
46  native_unit_of_measurement=PERCENTAGE,
47  ),
49  key="away_supply_fan_setpoint",
50  translation_key="away_supply_fan_setpoint",
51  device_class=NumberDeviceClass.POWER_FACTOR,
52  native_min_value=0,
53  native_max_value=100,
54  native_step=1,
55  mode=NumberMode.SLIDER,
56  native_value_fn=lambda device: device.fan_setpoint_supply_air_away,
57  set_native_value_fn=lambda device: device.set_fan_setpoint_supply_air_away,
58  native_unit_of_measurement=PERCENTAGE,
59  ),
61  key="cooker_hood_extract_fan_setpoint",
62  translation_key="cooker_hood_extract_fan_setpoint",
63  device_class=NumberDeviceClass.POWER_FACTOR,
64  native_min_value=0,
65  native_max_value=100,
66  native_step=1,
67  mode=NumberMode.SLIDER,
68  native_value_fn=lambda device: device.fan_setpoint_extract_air_cooker,
69  set_native_value_fn=lambda device: device.set_fan_setpoint_extract_air_cooker,
70  native_unit_of_measurement=PERCENTAGE,
71  ),
73  key="cooker_hood_supply_fan_setpoint",
74  translation_key="cooker_hood_supply_fan_setpoint",
75  device_class=NumberDeviceClass.POWER_FACTOR,
76  native_min_value=0,
77  native_max_value=100,
78  native_step=1,
79  mode=NumberMode.SLIDER,
80  native_value_fn=lambda device: device.fan_setpoint_supply_air_cooker,
81  set_native_value_fn=lambda device: device.set_fan_setpoint_supply_air_cooker,
82  native_unit_of_measurement=PERCENTAGE,
83  ),
85  key="fireplace_extract_fan_setpoint",
86  translation_key="fireplace_extract_fan_setpoint",
87  device_class=NumberDeviceClass.POWER_FACTOR,
88  native_min_value=0,
89  native_max_value=100,
90  native_step=1,
91  mode=NumberMode.SLIDER,
92  native_value_fn=lambda device: device.fan_setpoint_extract_air_fire,
93  set_native_value_fn=lambda device: device.set_fan_setpoint_extract_air_fire,
94  native_unit_of_measurement=PERCENTAGE,
95  ),
97  key="fireplace_supply_fan_setpoint",
98  translation_key="fireplace_supply_fan_setpoint",
99  device_class=NumberDeviceClass.POWER_FACTOR,
100  native_min_value=0,
101  native_max_value=100,
102  native_step=1,
103  mode=NumberMode.SLIDER,
104  native_value_fn=lambda device: device.fan_setpoint_supply_air_fire,
105  set_native_value_fn=lambda device: device.set_fan_setpoint_supply_air_fire,
106  native_unit_of_measurement=PERCENTAGE,
107  ),
109  key="high_extract_fan_setpoint",
110  translation_key="high_extract_fan_setpoint",
111  device_class=NumberDeviceClass.POWER_FACTOR,
112  native_min_value=0,
113  native_max_value=100,
114  native_step=1,
115  mode=NumberMode.SLIDER,
116  native_value_fn=lambda device: device.fan_setpoint_extract_air_high,
117  set_native_value_fn=lambda device: device.set_fan_setpoint_extract_air_high,
118  native_unit_of_measurement=PERCENTAGE,
119  ),
121  key="high_supply_fan_setpoint",
122  translation_key="high_supply_fan_setpoint",
123  device_class=NumberDeviceClass.POWER_FACTOR,
124  native_min_value=0,
125  native_max_value=100,
126  native_step=1,
127  mode=NumberMode.SLIDER,
128  native_value_fn=lambda device: device.fan_setpoint_supply_air_high,
129  set_native_value_fn=lambda device: device.set_fan_setpoint_supply_air_high,
130  native_unit_of_measurement=PERCENTAGE,
131  ),
133  key="home_extract_fan_setpoint",
134  translation_key="home_extract_fan_setpoint",
135  device_class=NumberDeviceClass.POWER_FACTOR,
136  native_min_value=0,
137  native_max_value=100,
138  native_step=1,
139  mode=NumberMode.SLIDER,
140  native_value_fn=lambda device: device.fan_setpoint_extract_air_home,
141  set_native_value_fn=lambda device: device.set_fan_setpoint_extract_air_home,
142  native_unit_of_measurement=PERCENTAGE,
143  ),
145  key="home_supply_fan_setpoint",
146  translation_key="home_supply_fan_setpoint",
147  device_class=NumberDeviceClass.POWER_FACTOR,
148  native_min_value=0,
149  native_max_value=100,
150  native_step=1,
151  mode=NumberMode.SLIDER,
152  native_value_fn=lambda device: device.fan_setpoint_supply_air_home,
153  set_native_value_fn=lambda device: device.set_fan_setpoint_supply_air_home,
154  native_unit_of_measurement=PERCENTAGE,
155  ),
156 )
157 
158 
160  hass: HomeAssistant,
161  config_entry: ConfigEntry,
162  async_add_entities: AddEntitiesCallback,
163 ) -> None:
164  """Set up Flexit (bacnet) number from a config entry."""
165  coordinator: FlexitCoordinator = hass.data[DOMAIN][config_entry.entry_id]
166 
168  FlexitNumber(coordinator, description) for description in NUMBERS
169  )
170 
171 
173  """Representation of a Flexit Number."""
174 
175  entity_description: FlexitNumberEntityDescription
176 
177  def __init__(
178  self,
179  coordinator: FlexitCoordinator,
180  entity_description: FlexitNumberEntityDescription,
181  ) -> None:
182  """Initialize Flexit (bacnet) number."""
183  super().__init__(coordinator)
184 
185  self.entity_descriptionentity_description = entity_description
186  self._attr_unique_id_attr_unique_id = (
187  f"{coordinator.device.serial_number}-{entity_description.key}"
188  )
189 
190  @property
191  def native_value(self) -> float:
192  """Return the state of the number."""
193  return self.entity_descriptionentity_description.native_value_fn(self.coordinator.device)
194 
195  async def async_set_native_value(self, value: float) -> None:
196  """Update the current value."""
197  set_native_value_fn = self.entity_descriptionentity_description.set_native_value_fn(
198  self.coordinator.device
199  )
200  try:
201  await set_native_value_fn(int(value))
202  except (asyncio.exceptions.TimeoutError, ConnectionError, DecodingError) as exc:
203  raise HomeAssistantError from exc
204  finally:
205  await self.coordinator.async_refresh()
None __init__(self, FlexitCoordinator coordinator, FlexitNumberEntityDescription entity_description)
Definition: number.py:181
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: number.py:163