From 5e981d00a4b8954b6ca21910a60016ac1af5a573 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Hjelseth=20H=C3=B8yer?= <github@dahoiv.net>
Date: Wed, 1 Jan 2025 23:25:42 +0100
Subject: [PATCH] Add mill number platform (#134044)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Mill number, max heating power

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>

* Mill number, max heating power

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>

* Mill number, max heating power

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>

* Mill number, max heating power

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>

* Mill number, max heating power

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>

* type

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>

---------

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>
---
 homeassistant/components/mill/__init__.py |  2 +-
 homeassistant/components/mill/number.py   | 83 +++++++++++++++++++++++
 tests/components/mill/test_init.py        |  2 +-
 3 files changed, 85 insertions(+), 2 deletions(-)
 create mode 100644 homeassistant/components/mill/number.py

diff --git a/homeassistant/components/mill/__init__.py b/homeassistant/components/mill/__init__.py
index 11199e126cf..116b3ef0341 100644
--- a/homeassistant/components/mill/__init__.py
+++ b/homeassistant/components/mill/__init__.py
@@ -16,7 +16,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession
 from .const import CLOUD, CONNECTION_TYPE, DOMAIN, LOCAL
 from .coordinator import MillDataUpdateCoordinator
 
-PLATFORMS = [Platform.CLIMATE, Platform.SENSOR]
+PLATFORMS = [Platform.CLIMATE, Platform.NUMBER, Platform.SENSOR]
 
 
 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
diff --git a/homeassistant/components/mill/number.py b/homeassistant/components/mill/number.py
new file mode 100644
index 00000000000..e2425cfba5b
--- /dev/null
+++ b/homeassistant/components/mill/number.py
@@ -0,0 +1,83 @@
+"""Support for mill wifi-enabled home heaters."""
+
+from __future__ import annotations
+
+from mill import MillDevice
+
+from homeassistant.components.number import NumberDeviceClass, NumberEntity
+from homeassistant.config_entries import ConfigEntry
+from homeassistant.const import CONF_USERNAME, UnitOfPower
+from homeassistant.core import HomeAssistant, callback
+from homeassistant.helpers.device_registry import DeviceInfo
+from homeassistant.helpers.entity_platform import AddEntitiesCallback
+from homeassistant.helpers.update_coordinator import CoordinatorEntity
+
+from .const import CLOUD, CONNECTION_TYPE, DOMAIN, MANUFACTURER
+from .coordinator import MillDataUpdateCoordinator
+
+
+async def async_setup_entry(
+    hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
+) -> None:
+    """Set up the Mill Number."""
+    if entry.data.get(CONNECTION_TYPE) == CLOUD:
+        mill_data_coordinator: MillDataUpdateCoordinator = hass.data[DOMAIN][CLOUD][
+            entry.data[CONF_USERNAME]
+        ]
+
+        async_add_entities(
+            MillNumber(mill_data_coordinator, mill_device)
+            for mill_device in mill_data_coordinator.data.values()
+        )
+
+
+class MillNumber(CoordinatorEntity[MillDataUpdateCoordinator], NumberEntity):
+    """Representation of a Mill number device."""
+
+    _attr_device_class = NumberDeviceClass.POWER
+    _attr_has_entity_name = True
+    _attr_native_max_value = 2000
+    _attr_native_min_value = 0
+    _attr_native_step = 1
+    _attr_native_unit_of_measurement = UnitOfPower.WATT
+
+    def __init__(
+        self,
+        coordinator: MillDataUpdateCoordinator,
+        mill_device: MillDevice,
+    ) -> None:
+        """Initialize the number."""
+        super().__init__(coordinator)
+
+        self._id = mill_device.device_id
+        self._available = False
+        self._attr_unique_id = f"{mill_device.device_id}_max_heating_power"
+        self._attr_device_info = DeviceInfo(
+            identifiers={(DOMAIN, mill_device.device_id)},
+            name=mill_device.name,
+            manufacturer=MANUFACTURER,
+            model=mill_device.model,
+        )
+        self._update_attr(mill_device)
+
+    @callback
+    def _handle_coordinator_update(self) -> None:
+        """Handle updated data from the coordinator."""
+        self._update_attr(self.coordinator.data[self._id])
+        self.async_write_ha_state()
+
+    @callback
+    def _update_attr(self, device: MillDevice) -> None:
+        self._attr_native_value = device.data["deviceSettings"]["reported"].get(
+            "max_heater_power"
+        )
+        self._available = device.available and self._attr_native_value is not None
+
+    @property
+    def available(self) -> bool:
+        """Return True if entity is available."""
+        return super().available and self._available
+
+    async def async_set_native_value(self, value: float) -> None:
+        """Set new value."""
+        await self.coordinator.mill_data_connection.max_heating_power(self._id, value)
diff --git a/tests/components/mill/test_init.py b/tests/components/mill/test_init.py
index bf0026b8c6c..a47e6422bf8 100644
--- a/tests/components/mill/test_init.py
+++ b/tests/components/mill/test_init.py
@@ -147,5 +147,5 @@ async def test_unload_entry(hass: HomeAssistant) -> None:
 
         assert await hass.config_entries.async_unload(entry.entry_id)
 
-        assert unload_entry.call_count == 2
+        assert unload_entry.call_count == 3
         assert entry.entry_id not in hass.data[mill.DOMAIN]
-- 
GitLab