From 4c542336d569a7d0f618c78dad2e0261323b271c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Hjelseth=20H=C3=B8yer?= Date: Thu, 9 Dec 2021 09:04:19 +0100 Subject: [PATCH] Add sensors to Mill local heaters (#61247) Co-authored-by: Franck Nijhof --- homeassistant/components/mill/sensor.py | 59 +++++++++++++++++++++++++ tests/components/mill/test_init.py | 2 + 2 files changed, 61 insertions(+) diff --git a/homeassistant/components/mill/sensor.py b/homeassistant/components/mill/sensor.py index 44d08e828f3..2a1d14d159e 100644 --- a/homeassistant/components/mill/sensor.py +++ b/homeassistant/components/mill/sensor.py @@ -12,9 +12,11 @@ from homeassistant.components.sensor import ( from homeassistant.const import ( CONCENTRATION_PARTS_PER_BILLION, CONCENTRATION_PARTS_PER_MILLION, + CONF_IP_ADDRESS, CONF_USERNAME, ENERGY_KILO_WATT_HOUR, PERCENTAGE, + POWER_WATT, TEMP_CELSIUS, ) from homeassistant.core import callback @@ -90,10 +92,43 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( ), ) +LOCAL_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( + SensorEntityDescription( + key="control_signal", + native_unit_of_measurement=PERCENTAGE, + name="Control signal", + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key="current_power", + device_class=SensorDeviceClass.POWER, + native_unit_of_measurement=POWER_WATT, + name="Current power", + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key="raw_ambient_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + native_unit_of_measurement=TEMP_CELSIUS, + name="Uncalibrated temperature", + state_class=SensorStateClass.MEASUREMENT, + entity_registry_enabled_default=False, + ), +) + async def async_setup_entry(hass, entry, async_add_entities): """Set up the Mill sensor.""" if entry.data.get(CONNECTION_TYPE) == LOCAL: + mill_data_coordinator = hass.data[DOMAIN][LOCAL][entry.data[CONF_IP_ADDRESS]] + + async_add_entities( + LocalMillSensor( + mill_data_coordinator, + entity_description, + ) + for entity_description in LOCAL_SENSOR_TYPES + ) return mill_data_coordinator = hass.data[DOMAIN][CLOUD][entry.data[CONF_USERNAME]] @@ -154,3 +189,27 @@ class MillSensor(CoordinatorEntity, SensorEntity): def _update_attr(self, device): self._available = device.available self._attr_native_value = getattr(device, self.entity_description.key) + + +class LocalMillSensor(CoordinatorEntity, SensorEntity): + """Representation of a Mill Sensor device.""" + + def __init__(self, coordinator, entity_description): + """Initialize the sensor.""" + super().__init__(coordinator) + + self.entity_description = entity_description + self._attr_name = ( + f"{coordinator.mill_data_connection.name} {entity_description.name}" + ) + self._update_attr() + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self._update_attr() + self.async_write_ha_state() + + @callback + def _update_attr(self) -> None: + self._attr_native_value = self.coordinator.data[self.entity_description.key] diff --git a/tests/components/mill/test_init.py b/tests/components/mill/test_init.py index f92b4689ebf..fe2facef50d 100644 --- a/tests/components/mill/test_init.py +++ b/tests/components/mill/test_init.py @@ -78,6 +78,8 @@ async def test_setup_with_local_config(hass): "ambient_temperature": 20, "set_temperature": 22, "current_power": 0, + "control_signal": 0, + "raw_ambient_temperature": 19, }, ) as mock_fetch, patch( "mill_local.Mill.connect",