Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """Home Assistant component for accessing the Wallbox Portal API.
2 
3 The number component allows control of charging current.
4 """
5 
6 from __future__ import annotations
7 
8 from collections.abc import Awaitable, Callable
9 from dataclasses import dataclass
10 from typing import cast
11 
12 from homeassistant.components.number import NumberEntity, NumberEntityDescription
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.core import HomeAssistant
15 from homeassistant.exceptions import PlatformNotReady
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .const import (
19  BIDIRECTIONAL_MODEL_PREFIXES,
20  CHARGER_DATA_KEY,
21  CHARGER_ENERGY_PRICE_KEY,
22  CHARGER_MAX_AVAILABLE_POWER_KEY,
23  CHARGER_MAX_CHARGING_CURRENT_KEY,
24  CHARGER_MAX_ICP_CURRENT_KEY,
25  CHARGER_PART_NUMBER_KEY,
26  CHARGER_SERIAL_NUMBER_KEY,
27  DOMAIN,
28 )
29 from .coordinator import InvalidAuth, WallboxCoordinator
30 from .entity import WallboxEntity
31 
32 
33 def min_charging_current_value(coordinator: WallboxCoordinator) -> float:
34  """Return the minimum available value for charging current."""
35  if (
36  coordinator.data[CHARGER_DATA_KEY][CHARGER_PART_NUMBER_KEY][0:2]
37  in BIDIRECTIONAL_MODEL_PREFIXES
38  ):
39  return cast(float, (coordinator.data[CHARGER_MAX_AVAILABLE_POWER_KEY] * -1))
40  return 6
41 
42 
43 @dataclass(frozen=True, kw_only=True)
45  """Describes Wallbox number entity."""
46 
47  max_value_fn: Callable[[WallboxCoordinator], float]
48  min_value_fn: Callable[[WallboxCoordinator], float]
49  set_value_fn: Callable[[WallboxCoordinator], Callable[[float], Awaitable[None]]]
50 
51 
52 NUMBER_TYPES: dict[str, WallboxNumberEntityDescription] = {
53  CHARGER_MAX_CHARGING_CURRENT_KEY: WallboxNumberEntityDescription(
54  key=CHARGER_MAX_CHARGING_CURRENT_KEY,
55  translation_key="maximum_charging_current",
56  max_value_fn=lambda coordinator: cast(
57  float, coordinator.data[CHARGER_MAX_AVAILABLE_POWER_KEY]
58  ),
59  min_value_fn=min_charging_current_value,
60  set_value_fn=lambda coordinator: coordinator.async_set_charging_current,
61  native_step=1,
62  ),
63  CHARGER_ENERGY_PRICE_KEY: WallboxNumberEntityDescription(
64  key=CHARGER_ENERGY_PRICE_KEY,
65  translation_key="energy_price",
66  max_value_fn=lambda _: 5,
67  min_value_fn=lambda _: -5,
68  set_value_fn=lambda coordinator: coordinator.async_set_energy_cost,
69  native_step=0.01,
70  ),
71  CHARGER_MAX_ICP_CURRENT_KEY: WallboxNumberEntityDescription(
72  key=CHARGER_MAX_ICP_CURRENT_KEY,
73  translation_key="maximum_icp_current",
74  max_value_fn=lambda coordinator: cast(
75  float, coordinator.data[CHARGER_MAX_AVAILABLE_POWER_KEY]
76  ),
77  min_value_fn=lambda _: 6,
78  set_value_fn=lambda coordinator: coordinator.async_set_icp_current,
79  native_step=1,
80  ),
81 }
82 
83 
85  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
86 ) -> None:
87  """Create wallbox number entities in HASS."""
88  coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id]
89  # Check if the user has sufficient rights to change values, if so, add number component:
90  try:
91  await coordinator.async_set_charging_current(
92  coordinator.data[CHARGER_MAX_CHARGING_CURRENT_KEY]
93  )
94  except InvalidAuth:
95  return
96  except ConnectionError as exc:
97  raise PlatformNotReady from exc
98 
100  WallboxNumber(coordinator, entry, description)
101  for ent in coordinator.data
102  if (description := NUMBER_TYPES.get(ent))
103  )
104 
105 
107  """Representation of the Wallbox portal."""
108 
109  entity_description: WallboxNumberEntityDescription
110 
111  def __init__(
112  self,
113  coordinator: WallboxCoordinator,
114  entry: ConfigEntry,
115  description: WallboxNumberEntityDescription,
116  ) -> None:
117  """Initialize a Wallbox number entity."""
118  super().__init__(coordinator)
119  self.entity_descriptionentity_description = description
120  self._coordinator_coordinator = coordinator
121  self._attr_unique_id_attr_unique_id = f"{description.key}-{coordinator.data[CHARGER_DATA_KEY][CHARGER_SERIAL_NUMBER_KEY]}"
122 
123  @property
124  def native_max_value(self) -> float:
125  """Return the maximum available value."""
126  return self.entity_descriptionentity_description.max_value_fn(self.coordinator)
127 
128  @property
129  def native_min_value(self) -> float:
130  """Return the minimum available value."""
131  return self.entity_descriptionentity_description.min_value_fn(self.coordinator)
132 
133  @property
134  def native_value(self) -> float | None:
135  """Return the value of the entity."""
136  return cast(float | None, self._coordinator_coordinator.data[self.entity_descriptionentity_description.key])
137 
138  async def async_set_native_value(self, value: float) -> None:
139  """Set the value of the entity."""
140  await self.entity_descriptionentity_description.set_value_fn(self.coordinator)(value)
None __init__(self, WallboxCoordinator coordinator, ConfigEntry entry, WallboxNumberEntityDescription description)
Definition: number.py:116
float min_charging_current_value(WallboxCoordinator coordinator)
Definition: number.py:33
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: number.py:86