|
| 1 | +"""Provides number enties for Home Connect.""" |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from homeconnect.api import HomeConnectError |
| 6 | + |
| 7 | +from homeassistant.components.number import ( |
| 8 | + ATTR_MAX, |
| 9 | + ATTR_MIN, |
| 10 | + NumberDeviceClass, |
| 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 | + |
| 18 | +from .api import ConfigEntryAuth |
| 19 | +from .const import ATTR_CONSTRAINTS, ATTR_STEPSIZE, ATTR_UNIT, ATTR_VALUE, DOMAIN |
| 20 | +from .entity import HomeConnectEntity |
| 21 | + |
| 22 | +_LOGGER = logging.getLogger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +NUMBERS = ( |
| 26 | + NumberEntityDescription( |
| 27 | + key="Refrigeration.FridgeFreezer.Setting.SetpointTemperatureRefrigerator", |
| 28 | + device_class=NumberDeviceClass.TEMPERATURE, |
| 29 | + translation_key="refrigerator_setpoint_temperature", |
| 30 | + ), |
| 31 | + NumberEntityDescription( |
| 32 | + key="Refrigeration.FridgeFreezer.Setting.SetpointTemperatureFreezer", |
| 33 | + device_class=NumberDeviceClass.TEMPERATURE, |
| 34 | + translation_key="freezer_setpoint_temperature", |
| 35 | + ), |
| 36 | + NumberEntityDescription( |
| 37 | + key="Refrigeration.Common.Setting.BottleCooler.SetpointTemperature", |
| 38 | + device_class=NumberDeviceClass.TEMPERATURE, |
| 39 | + translation_key="bottle_cooler_setpoint_temperature", |
| 40 | + ), |
| 41 | + NumberEntityDescription( |
| 42 | + key="Refrigeration.Common.Setting.ChillerLeft.SetpointTemperature", |
| 43 | + device_class=NumberDeviceClass.TEMPERATURE, |
| 44 | + translation_key="chiller_left_setpoint_temperature", |
| 45 | + ), |
| 46 | + NumberEntityDescription( |
| 47 | + key="Refrigeration.Common.Setting.ChillerCommon.SetpointTemperature", |
| 48 | + device_class=NumberDeviceClass.TEMPERATURE, |
| 49 | + translation_key="chiller_setpoint_temperature", |
| 50 | + ), |
| 51 | + NumberEntityDescription( |
| 52 | + key="Refrigeration.Common.Setting.ChillerRight.SetpointTemperature", |
| 53 | + device_class=NumberDeviceClass.TEMPERATURE, |
| 54 | + translation_key="chiller_right_setpoint_temperature", |
| 55 | + ), |
| 56 | + NumberEntityDescription( |
| 57 | + key="Refrigeration.Common.Setting.WineCompartment.SetpointTemperature", |
| 58 | + device_class=NumberDeviceClass.TEMPERATURE, |
| 59 | + translation_key="wine_compartment_setpoint_temperature", |
| 60 | + ), |
| 61 | + NumberEntityDescription( |
| 62 | + key="Refrigeration.Common.Setting.WineCompartment2.SetpointTemperature", |
| 63 | + device_class=NumberDeviceClass.TEMPERATURE, |
| 64 | + translation_key="wine_compartment_2_setpoint_temperature", |
| 65 | + ), |
| 66 | + NumberEntityDescription( |
| 67 | + key="Refrigeration.Common.Setting.WineCompartment3.SetpointTemperature", |
| 68 | + device_class=NumberDeviceClass.TEMPERATURE, |
| 69 | + translation_key="wine_compartment_3_setpoint_temperature", |
| 70 | + ), |
| 71 | +) |
| 72 | + |
| 73 | + |
| 74 | +async def async_setup_entry( |
| 75 | + hass: HomeAssistant, |
| 76 | + config_entry: ConfigEntry, |
| 77 | + async_add_entities: AddEntitiesCallback, |
| 78 | +) -> None: |
| 79 | + """Set up the Home Connect number.""" |
| 80 | + |
| 81 | + def get_entities() -> list[HomeConnectNumberEntity]: |
| 82 | + """Get a list of entities.""" |
| 83 | + hc_api: ConfigEntryAuth = hass.data[DOMAIN][config_entry.entry_id] |
| 84 | + return [ |
| 85 | + HomeConnectNumberEntity(device, description) |
| 86 | + for description in NUMBERS |
| 87 | + for device in hc_api.devices |
| 88 | + if description.key in device.appliance.status |
| 89 | + ] |
| 90 | + |
| 91 | + async_add_entities(await hass.async_add_executor_job(get_entities), True) |
| 92 | + |
| 93 | + |
| 94 | +class HomeConnectNumberEntity(HomeConnectEntity, NumberEntity): |
| 95 | + """Number setting class for Home Connect.""" |
| 96 | + |
| 97 | + async def async_set_native_value(self, value: float) -> None: |
| 98 | + """Set the native value of the entity.""" |
| 99 | + _LOGGER.debug( |
| 100 | + "Tried to set value %s to %s for %s", |
| 101 | + value, |
| 102 | + self.bsh_key, |
| 103 | + self.entity_id, |
| 104 | + ) |
| 105 | + try: |
| 106 | + await self.hass.async_add_executor_job( |
| 107 | + self.device.appliance.set_setting, |
| 108 | + self.bsh_key, |
| 109 | + value, |
| 110 | + ) |
| 111 | + except HomeConnectError as err: |
| 112 | + _LOGGER.error( |
| 113 | + "Error setting value %s to %s for %s: %s", |
| 114 | + value, |
| 115 | + self.bsh_key, |
| 116 | + self.entity_id, |
| 117 | + err, |
| 118 | + ) |
| 119 | + |
| 120 | + async def async_fetch_constraints(self) -> None: |
| 121 | + """Fetch the max and min values and step for the number entity.""" |
| 122 | + try: |
| 123 | + data = await self.hass.async_add_executor_job( |
| 124 | + self.device.appliance.get, f"/settings/{self.bsh_key}" |
| 125 | + ) |
| 126 | + except HomeConnectError as err: |
| 127 | + _LOGGER.error("An error occurred: %s", err) |
| 128 | + return |
| 129 | + if not data or not (constraints := data.get(ATTR_CONSTRAINTS)): |
| 130 | + return |
| 131 | + self._attr_native_max_value = constraints.get(ATTR_MAX) |
| 132 | + self._attr_native_min_value = constraints.get(ATTR_MIN) |
| 133 | + self._attr_native_step = constraints.get(ATTR_STEPSIZE) |
| 134 | + self._attr_native_unit_of_measurement = data.get(ATTR_UNIT) |
| 135 | + |
| 136 | + async def async_update(self) -> None: |
| 137 | + """Update the number setting status.""" |
| 138 | + if not (data := self.device.appliance.status.get(self.bsh_key)): |
| 139 | + _LOGGER.error("No value for %s", self.bsh_key) |
| 140 | + self._attr_native_value = None |
| 141 | + return |
| 142 | + self._attr_native_value = data.get(ATTR_VALUE, None) |
| 143 | + _LOGGER.debug("Updated, new value: %s", self._attr_native_value) |
| 144 | + |
| 145 | + if ( |
| 146 | + not hasattr(self, "_attr_native_min_value") |
| 147 | + or self._attr_native_min_value is None |
| 148 | + or not hasattr(self, "_attr_native_max_value") |
| 149 | + or self._attr_native_max_value is None |
| 150 | + or not hasattr(self, "_attr_native_step") |
| 151 | + or self._attr_native_step is None |
| 152 | + ): |
| 153 | + await self.async_fetch_constraints() |
0 commit comments