Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """Provides number enties for Home Connect."""
2 
3 import logging
4 
5 from homeconnect.api import HomeConnectError
6 
8  ATTR_MAX,
9  ATTR_MIN,
10  NumberDeviceClass,
11  NumberEntity,
12  NumberEntityDescription,
13 )
14 from homeassistant.core import HomeAssistant
15 from homeassistant.exceptions import ServiceValidationError
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from . import HomeConnectConfigEntry, get_dict_from_home_connect_error
19 from .const import (
20  ATTR_CONSTRAINTS,
21  ATTR_STEPSIZE,
22  ATTR_UNIT,
23  ATTR_VALUE,
24  DOMAIN,
25  SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID,
26  SVE_TRANSLATION_PLACEHOLDER_SETTING_KEY,
27  SVE_TRANSLATION_PLACEHOLDER_VALUE,
28 )
29 from .entity import HomeConnectEntity
30 
31 _LOGGER = logging.getLogger(__name__)
32 
33 
34 NUMBERS = (
36  key="Refrigeration.FridgeFreezer.Setting.SetpointTemperatureRefrigerator",
37  device_class=NumberDeviceClass.TEMPERATURE,
38  translation_key="refrigerator_setpoint_temperature",
39  ),
41  key="Refrigeration.FridgeFreezer.Setting.SetpointTemperatureFreezer",
42  device_class=NumberDeviceClass.TEMPERATURE,
43  translation_key="freezer_setpoint_temperature",
44  ),
46  key="Refrigeration.Common.Setting.BottleCooler.SetpointTemperature",
47  device_class=NumberDeviceClass.TEMPERATURE,
48  translation_key="bottle_cooler_setpoint_temperature",
49  ),
51  key="Refrigeration.Common.Setting.ChillerLeft.SetpointTemperature",
52  device_class=NumberDeviceClass.TEMPERATURE,
53  translation_key="chiller_left_setpoint_temperature",
54  ),
56  key="Refrigeration.Common.Setting.ChillerCommon.SetpointTemperature",
57  device_class=NumberDeviceClass.TEMPERATURE,
58  translation_key="chiller_setpoint_temperature",
59  ),
61  key="Refrigeration.Common.Setting.ChillerRight.SetpointTemperature",
62  device_class=NumberDeviceClass.TEMPERATURE,
63  translation_key="chiller_right_setpoint_temperature",
64  ),
66  key="Refrigeration.Common.Setting.WineCompartment.SetpointTemperature",
67  device_class=NumberDeviceClass.TEMPERATURE,
68  translation_key="wine_compartment_setpoint_temperature",
69  ),
71  key="Refrigeration.Common.Setting.WineCompartment2.SetpointTemperature",
72  device_class=NumberDeviceClass.TEMPERATURE,
73  translation_key="wine_compartment_2_setpoint_temperature",
74  ),
76  key="Refrigeration.Common.Setting.WineCompartment3.SetpointTemperature",
77  device_class=NumberDeviceClass.TEMPERATURE,
78  translation_key="wine_compartment_3_setpoint_temperature",
79  ),
80 )
81 
82 
84  hass: HomeAssistant,
85  entry: HomeConnectConfigEntry,
86  async_add_entities: AddEntitiesCallback,
87 ) -> None:
88  """Set up the Home Connect number."""
89 
90  def get_entities() -> list[HomeConnectNumberEntity]:
91  """Get a list of entities."""
92  return [
93  HomeConnectNumberEntity(device, description)
94  for description in NUMBERS
95  for device in entry.runtime_data.devices
96  if description.key in device.appliance.status
97  ]
98 
99  async_add_entities(await hass.async_add_executor_job(get_entities), True)
100 
101 
103  """Number setting class for Home Connect."""
104 
105  async def async_set_native_value(self, value: float) -> None:
106  """Set the native value of the entity."""
107  _LOGGER.debug(
108  "Tried to set value %s to %s for %s",
109  value,
110  self.bsh_keybsh_key,
111  self.entity_identity_id,
112  )
113  try:
114  await self.hasshass.async_add_executor_job(
115  self.devicedevice.appliance.set_setting,
116  self.bsh_keybsh_key,
117  value,
118  )
119  except HomeConnectError as err:
121  translation_domain=DOMAIN,
122  translation_key="set_setting",
123  translation_placeholders={
125  SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID: self.entity_identity_id,
126  SVE_TRANSLATION_PLACEHOLDER_SETTING_KEY: self.bsh_keybsh_key,
127  SVE_TRANSLATION_PLACEHOLDER_VALUE: str(value),
128  },
129  ) from err
130 
131  async def async_fetch_constraints(self) -> None:
132  """Fetch the max and min values and step for the number entity."""
133  try:
134  data = await self.hasshass.async_add_executor_job(
135  self.devicedevice.appliance.get, f"/settings/{self.bsh_key}"
136  )
137  except HomeConnectError as err:
138  _LOGGER.error("An error occurred: %s", err)
139  return
140  if not data or not (constraints := data.get(ATTR_CONSTRAINTS)):
141  return
142  self._attr_native_max_value_attr_native_max_value = constraints.get(ATTR_MAX)
143  self._attr_native_min_value_attr_native_min_value = constraints.get(ATTR_MIN)
144  self._attr_native_step_attr_native_step = constraints.get(ATTR_STEPSIZE)
145  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = data.get(ATTR_UNIT)
146 
147  async def async_update(self) -> None:
148  """Update the number setting status."""
149  if not (data := self.devicedevice.appliance.status.get(self.bsh_keybsh_key)):
150  _LOGGER.error("No value for %s", self.bsh_keybsh_key)
151  self._attr_native_value_attr_native_value = None
152  return
153  self._attr_native_value_attr_native_value = data.get(ATTR_VALUE, None)
154  _LOGGER.debug("Updated, new value: %s", self._attr_native_value_attr_native_value)
155 
156  if (
157  not hasattr(self, "_attr_native_min_value")
158  or self._attr_native_min_value_attr_native_min_value is None
159  or not hasattr(self, "_attr_native_max_value")
160  or self._attr_native_max_value_attr_native_max_value is None
161  or not hasattr(self, "_attr_native_step")
162  or self._attr_native_step_attr_native_step is None
163  ):
164  await self.async_fetch_constraints()
list[BaseAprilaireSensor] get_entities(type[BaseAprilaireSensor] entity_class, AprilaireCoordinator coordinator, str unique_id, tuple[AprilaireSensorDescription,...] descriptions)
Definition: sensor.py:64
None async_setup_entry(HomeAssistant hass, HomeConnectConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: number.py:87
dict[str, Any] get_dict_from_home_connect_error(api.HomeConnectError err)
Definition: __init__.py:334