|
| 1 | +"""Support for mill wifi-enabled home heaters.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from mill import MillDevice |
| 6 | + |
| 7 | +from homeassistant.components.number import NumberDeviceClass, NumberEntity |
| 8 | +from homeassistant.config_entries import ConfigEntry |
| 9 | +from homeassistant.const import CONF_USERNAME, UnitOfPower |
| 10 | +from homeassistant.core import HomeAssistant, callback |
| 11 | +from homeassistant.helpers.device_registry import DeviceInfo |
| 12 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 13 | +from homeassistant.helpers.update_coordinator import CoordinatorEntity |
| 14 | + |
| 15 | +from .const import CLOUD, CONNECTION_TYPE, DOMAIN, MANUFACTURER |
| 16 | +from .coordinator import MillDataUpdateCoordinator |
| 17 | + |
| 18 | + |
| 19 | +async def async_setup_entry( |
| 20 | + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback |
| 21 | +) -> None: |
| 22 | + """Set up the Mill Number.""" |
| 23 | + if entry.data.get(CONNECTION_TYPE) == CLOUD: |
| 24 | + mill_data_coordinator: MillDataUpdateCoordinator = hass.data[DOMAIN][CLOUD][ |
| 25 | + entry.data[CONF_USERNAME] |
| 26 | + ] |
| 27 | + |
| 28 | + async_add_entities( |
| 29 | + MillNumber(mill_data_coordinator, mill_device) |
| 30 | + for mill_device in mill_data_coordinator.data.values() |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +class MillNumber(CoordinatorEntity[MillDataUpdateCoordinator], NumberEntity): |
| 35 | + """Representation of a Mill number device.""" |
| 36 | + |
| 37 | + _attr_device_class = NumberDeviceClass.POWER |
| 38 | + _attr_has_entity_name = True |
| 39 | + _attr_native_max_value = 2000 |
| 40 | + _attr_native_min_value = 0 |
| 41 | + _attr_native_step = 1 |
| 42 | + _attr_native_unit_of_measurement = UnitOfPower.WATT |
| 43 | + |
| 44 | + def __init__( |
| 45 | + self, |
| 46 | + coordinator: MillDataUpdateCoordinator, |
| 47 | + mill_device: MillDevice, |
| 48 | + ) -> None: |
| 49 | + """Initialize the number.""" |
| 50 | + super().__init__(coordinator) |
| 51 | + |
| 52 | + self._id = mill_device.device_id |
| 53 | + self._available = False |
| 54 | + self._attr_unique_id = f"{mill_device.device_id}_max_heating_power" |
| 55 | + self._attr_device_info = DeviceInfo( |
| 56 | + identifiers={(DOMAIN, mill_device.device_id)}, |
| 57 | + name=mill_device.name, |
| 58 | + manufacturer=MANUFACTURER, |
| 59 | + model=mill_device.model, |
| 60 | + ) |
| 61 | + self._update_attr(mill_device) |
| 62 | + |
| 63 | + @callback |
| 64 | + def _handle_coordinator_update(self) -> None: |
| 65 | + """Handle updated data from the coordinator.""" |
| 66 | + self._update_attr(self.coordinator.data[self._id]) |
| 67 | + self.async_write_ha_state() |
| 68 | + |
| 69 | + @callback |
| 70 | + def _update_attr(self, device: MillDevice) -> None: |
| 71 | + self._attr_native_value = device.data["deviceSettings"]["reported"].get( |
| 72 | + "max_heater_power" |
| 73 | + ) |
| 74 | + self._available = device.available and self._attr_native_value is not None |
| 75 | + |
| 76 | + @property |
| 77 | + def available(self) -> bool: |
| 78 | + """Return True if entity is available.""" |
| 79 | + return super().available and self._available |
| 80 | + |
| 81 | + async def async_set_native_value(self, value: float) -> None: |
| 82 | + """Set new value.""" |
| 83 | + await self.coordinator.mill_data_connection.max_heating_power(self._id, value) |
0 commit comments