Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """Support for controlling juicenet/juicepoint/juicebox based EVSE numbers."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from pyjuicenet import Api, Charger
8 
10  DEFAULT_MAX_VALUE,
11  NumberEntity,
12  NumberEntityDescription,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
18 
19 from .const import DOMAIN, JUICENET_API, JUICENET_COORDINATOR
20 from .entity import JuiceNetDevice
21 
22 
23 @dataclass(frozen=True, kw_only=True)
25  """An entity description for a JuiceNetNumber."""
26 
27  setter_key: str
28  native_max_value_key: str | None = None
29 
30 
31 NUMBER_TYPES: tuple[JuiceNetNumberEntityDescription, ...] = (
33  translation_key="amperage_limit",
34  key="current_charging_amperage_limit",
35  native_min_value=6,
36  native_max_value_key="max_charging_amperage",
37  native_step=1,
38  setter_key="set_charging_amperage_limit",
39  ),
40 )
41 
42 
44  hass: HomeAssistant,
45  config_entry: ConfigEntry,
46  async_add_entities: AddEntitiesCallback,
47 ) -> None:
48  """Set up the JuiceNet Numbers."""
49  juicenet_data = hass.data[DOMAIN][config_entry.entry_id]
50  api: Api = juicenet_data[JUICENET_API]
51  coordinator = juicenet_data[JUICENET_COORDINATOR]
52 
53  entities = [
54  JuiceNetNumber(device, description, coordinator)
55  for device in api.devices
56  for description in NUMBER_TYPES
57  ]
58  async_add_entities(entities)
59 
60 
62  """Implementation of a JuiceNet number."""
63 
64  entity_description: JuiceNetNumberEntityDescription
65 
66  def __init__(
67  self,
68  device: Charger,
69  description: JuiceNetNumberEntityDescription,
70  coordinator: DataUpdateCoordinator,
71  ) -> None:
72  """Initialise the number."""
73  super().__init__(device, description.key, coordinator)
74  self.entity_descriptionentity_description = description
75 
76  @property
77  def native_value(self) -> float | None:
78  """Return the value of the entity."""
79  return getattr(self.devicedevice, self.entity_descriptionentity_description.key, None)
80 
81  @property
82  def native_max_value(self) -> float:
83  """Return the maximum value."""
84  if self.entity_descriptionentity_description.native_max_value_key is not None:
85  return getattr(self.devicedevice, self.entity_descriptionentity_description.native_max_value_key)
86  if self.entity_descriptionentity_description.native_max_value is not None:
87  return self.entity_descriptionentity_description.native_max_value
88  return DEFAULT_MAX_VALUE
89 
90  async def async_set_native_value(self, value: float) -> None:
91  """Update the current value."""
92  await getattr(self.devicedevice, self.entity_descriptionentity_description.setter_key)(value)
None __init__(self, Charger device, JuiceNetNumberEntityDescription description, DataUpdateCoordinator coordinator)
Definition: number.py:71
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: number.py:47