Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """TOLO Sauna number controls."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from tololib import (
10  FAN_TIMER_MAX,
11  POWER_TIMER_MAX,
12  SALT_BATH_TIMER_MAX,
13  ToloClient,
14  ToloSettings,
15 )
16 
17 from homeassistant.components.number import NumberEntity, NumberEntityDescription
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.const import EntityCategory, UnitOfTime
20 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 
23 from .const import DOMAIN
24 from .coordinator import ToloSaunaUpdateCoordinator
25 from .entity import ToloSaunaCoordinatorEntity
26 
27 
28 @dataclass(frozen=True, kw_only=True)
30  """Class describing TOLO Number entities."""
31 
32  getter: Callable[[ToloSettings], int | None]
33  setter: Callable[[ToloClient, int | None], Any]
34 
35  entity_category = EntityCategory.CONFIG
36  native_min_value = 0
37  native_step = 1
38 
39 
40 NUMBERS = (
42  key="power_timer",
43  translation_key="power_timer",
44  native_unit_of_measurement=UnitOfTime.MINUTES,
45  native_max_value=POWER_TIMER_MAX,
46  getter=lambda settings: settings.power_timer,
47  setter=lambda client, value: client.set_power_timer(value),
48  ),
50  key="salt_bath_timer",
51  translation_key="salt_bath_timer",
52  native_unit_of_measurement=UnitOfTime.MINUTES,
53  native_max_value=SALT_BATH_TIMER_MAX,
54  getter=lambda settings: settings.salt_bath_timer,
55  setter=lambda client, value: client.set_salt_bath_timer(value),
56  ),
58  key="fan_timer",
59  translation_key="fan_timer",
60  native_unit_of_measurement=UnitOfTime.MINUTES,
61  native_max_value=FAN_TIMER_MAX,
62  getter=lambda settings: settings.fan_timer,
63  setter=lambda client, value: client.set_fan_timer(value),
64  ),
65 )
66 
67 
69  hass: HomeAssistant,
70  entry: ConfigEntry,
71  async_add_entities: AddEntitiesCallback,
72 ) -> None:
73  """Set up number controls for TOLO Sauna."""
74  coordinator = hass.data[DOMAIN][entry.entry_id]
76  ToloNumberEntity(coordinator, entry, description) for description in NUMBERS
77  )
78 
79 
81  """TOLO Number entity."""
82 
83  entity_description: ToloNumberEntityDescription
84 
85  def __init__(
86  self,
87  coordinator: ToloSaunaUpdateCoordinator,
88  entry: ConfigEntry,
89  entity_description: ToloNumberEntityDescription,
90  ) -> None:
91  """Initialize TOLO Number entity."""
92  super().__init__(coordinator, entry)
93  self.entity_descriptionentity_description = entity_description
94  self._attr_unique_id_attr_unique_id = f"{entry.entry_id}_{entity_description.key}"
95 
96  @property
97  def native_value(self) -> float:
98  """Return the value of this TOLO Number entity."""
99  return self.entity_descriptionentity_description.getter(self.coordinator.data.settings) or 0
100 
101  def set_native_value(self, value: float) -> None:
102  """Set the value of this TOLO Number entity."""
103  int_value = int(value)
104  if int_value == 0:
105  self.entity_descriptionentity_description.setter(self.coordinator.client, None)
106  return
107  self.entity_descriptionentity_description.setter(self.coordinator.client, int_value)
None __init__(self, ToloSaunaUpdateCoordinator coordinator, ConfigEntry entry, ToloNumberEntityDescription entity_description)
Definition: number.py:90
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: number.py:72