Add sensors to Mill local heaters (#61247)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Daniel Hjelseth Høyer 2021-12-09 09:04:19 +01:00 committed by GitHub
parent 54e312e1f7
commit 4c542336d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

View file

@ -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]

View file

@ -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",