Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """Support for Ecoforest number platform."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from pyecoforest.models.device import Device
9 
10 from homeassistant.components.number import NumberEntity, NumberEntityDescription
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .const import DOMAIN
16 from .coordinator import EcoforestCoordinator
17 from .entity import EcoforestEntity
18 
19 
20 @dataclass(frozen=True, kw_only=True)
22  """Describes an ecoforest number entity."""
23 
24  value_fn: Callable[[Device], float | None]
25 
26 
27 NUMBER_ENTITIES = (
29  key="power_level",
30  translation_key="power_level",
31  native_min_value=1,
32  native_max_value=9,
33  native_step=1,
34  value_fn=lambda data: data.power,
35  ),
36 )
37 
38 
40  hass: HomeAssistant,
41  config_entry: ConfigEntry,
42  async_add_entities: AddEntitiesCallback,
43 ) -> None:
44  """Set up Ecoforest number platform."""
45  coordinator: EcoforestCoordinator = hass.data[DOMAIN][config_entry.entry_id]
46 
47  entities = [
48  EcoforestNumberEntity(coordinator, description)
49  for description in NUMBER_ENTITIES
50  ]
51 
52  async_add_entities(entities)
53 
54 
56  """Representation of an Ecoforest number entity."""
57 
58  entity_description: EcoforestNumberEntityDescription
59 
60  @property
61  def native_value(self) -> float | None:
62  """Return the state of the entity."""
63  return self.entity_descriptionentity_description.value_fn(self.datadatadata)
64 
65  async def async_set_native_value(self, value: float) -> None:
66  """Update the native value."""
67  await self.coordinator.api.set_power(int(value))
68  await self.coordinator.async_request_refresh()
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: number.py:43