Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """Creates HomeWizard Number entities."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.number import NumberEntity
6 from homeassistant.const import PERCENTAGE, EntityCategory
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers.entity_platform import AddEntitiesCallback
9 from homeassistant.util.color import brightness_to_value, value_to_brightness
10 
11 from . import HomeWizardConfigEntry
12 from .coordinator import HWEnergyDeviceUpdateCoordinator
13 from .entity import HomeWizardEntity
14 from .helpers import homewizard_exception_handler
15 
16 PARALLEL_UPDATES = 1
17 
18 
20  hass: HomeAssistant,
21  entry: HomeWizardConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Set up numbers for device."""
25  if entry.runtime_data.supports_state():
26  async_add_entities([HWEnergyNumberEntity(entry.runtime_data)])
27 
28 
30  """Representation of status light number."""
31 
32  _attr_entity_category = EntityCategory.CONFIG
33  _attr_translation_key = "status_light_brightness"
34  _attr_native_unit_of_measurement = PERCENTAGE
35 
36  def __init__(
37  self,
38  coordinator: HWEnergyDeviceUpdateCoordinator,
39  ) -> None:
40  """Initialize the control number."""
41  super().__init__(coordinator)
42  self._attr_unique_id_attr_unique_id = (
43  f"{coordinator.config_entry.unique_id}_status_light_brightness"
44  )
45 
46  @homewizard_exception_handler
47  async def async_set_native_value(self, value: float) -> None:
48  """Set a new value."""
49  await self.coordinator.api.state_set(
50  brightness=value_to_brightness((0, 100), value)
51  )
52  await self.coordinator.async_refresh()
53 
54  @property
55  def available(self) -> bool:
56  """Return if entity is available."""
57  return super().available and self.coordinator.data.state is not None
58 
59  @property
60  def native_value(self) -> float | None:
61  """Return the current value."""
62  if (
63  not self.coordinator.data.state
64  or (brightness := self.coordinator.data.state.brightness) is None
65  ):
66  return None
67  return round(brightness_to_value((0, 100), brightness))
None __init__(self, HWEnergyDeviceUpdateCoordinator coordinator)
Definition: number.py:39
None async_setup_entry(HomeAssistant hass, HomeWizardConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: number.py:23
float brightness_to_value(tuple[float, float] low_high_range, int brightness)
Definition: color.py:752
int value_to_brightness(tuple[float, float] low_high_range, float value)
Definition: color.py:767