Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """Number platform for IronOS integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from enum import StrEnum
8 
9 from pynecil import CharSetting, CommunicationError, LiveDataResponse
10 
12  NumberDeviceClass,
13  NumberEntity,
14  NumberEntityDescription,
15  NumberMode,
16 )
17 from homeassistant.const import UnitOfTemperature
18 from homeassistant.core import HomeAssistant
19 from homeassistant.exceptions import ServiceValidationError
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from . import IronOSConfigEntry
23 from .const import DOMAIN, MAX_TEMP, MIN_TEMP
24 from .entity import IronOSBaseEntity
25 
26 
27 @dataclass(frozen=True, kw_only=True)
29  """Describes IronOS number entity."""
30 
31  value_fn: Callable[[LiveDataResponse], float | int | None]
32  max_value_fn: Callable[[LiveDataResponse], float | int]
33  set_key: CharSetting
34 
35 
36 class PinecilNumber(StrEnum):
37  """Number controls for Pinecil device."""
38 
39  SETPOINT_TEMP = "setpoint_temperature"
40 
41 
42 PINECIL_NUMBER_DESCRIPTIONS: tuple[IronOSNumberEntityDescription, ...] = (
44  key=PinecilNumber.SETPOINT_TEMP,
45  translation_key=PinecilNumber.SETPOINT_TEMP,
46  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
47  device_class=NumberDeviceClass.TEMPERATURE,
48  value_fn=lambda data: data.setpoint_temp,
49  set_key=CharSetting.SETPOINT_TEMP,
50  mode=NumberMode.BOX,
51  native_min_value=MIN_TEMP,
52  native_step=5,
53  max_value_fn=lambda data: min(data.max_tip_temp_ability or MAX_TEMP, MAX_TEMP),
54  ),
55 )
56 
57 
59  hass: HomeAssistant,
60  entry: IronOSConfigEntry,
61  async_add_entities: AddEntitiesCallback,
62 ) -> None:
63  """Set up number entities from a config entry."""
64  coordinator = entry.runtime_data
65 
67  IronOSNumberEntity(coordinator, description)
68  for description in PINECIL_NUMBER_DESCRIPTIONS
69  )
70 
71 
73  """Implementation of a IronOS number entity."""
74 
75  entity_description: IronOSNumberEntityDescription
76 
77  async def async_set_native_value(self, value: float) -> None:
78  """Update the current value."""
79  try:
80  await self.coordinator.device.write(self.entity_descriptionentity_description.set_key, value)
81  except CommunicationError as e:
83  translation_domain=DOMAIN,
84  translation_key="submit_setting_failed",
85  ) from e
86  self.async_write_ha_stateasync_write_ha_state()
87 
88  @property
89  def native_value(self) -> float | int | None:
90  """Return sensor state."""
91  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
92 
93  @property
94  def native_max_value(self) -> float:
95  """Return sensor state."""
96  return self.entity_descriptionentity_description.max_value_fn(self.coordinator.data)
None async_setup_entry(HomeAssistant hass, IronOSConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: number.py:62