Fix units for LCN sensor (#138940)

This commit is contained in:
Andre Lengwenus 2025-02-25 12:35:47 +01:00 committed by GitHub
parent 661b55d6eb
commit 051cc41d4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,7 +3,6 @@
from collections.abc import Iterable
from functools import partial
from itertools import chain
from typing import cast
import pypck
@ -18,6 +17,11 @@ from homeassistant.const import (
CONF_ENTITIES,
CONF_SOURCE,
CONF_UNIT_OF_MEASUREMENT,
LIGHT_LUX,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfSpeed,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
@ -47,6 +51,17 @@ DEVICE_CLASS_MAPPING = {
pypck.lcn_defs.VarUnit.AMPERE: SensorDeviceClass.CURRENT,
}
UNIT_OF_MEASUREMENT_MAPPING = {
pypck.lcn_defs.VarUnit.CELSIUS: UnitOfTemperature.CELSIUS,
pypck.lcn_defs.VarUnit.KELVIN: UnitOfTemperature.KELVIN,
pypck.lcn_defs.VarUnit.FAHRENHEIT: UnitOfTemperature.FAHRENHEIT,
pypck.lcn_defs.VarUnit.LUX_T: LIGHT_LUX,
pypck.lcn_defs.VarUnit.LUX_I: LIGHT_LUX,
pypck.lcn_defs.VarUnit.METERPERSECOND: UnitOfSpeed.METERS_PER_SECOND,
pypck.lcn_defs.VarUnit.VOLT: UnitOfElectricPotential.VOLT,
pypck.lcn_defs.VarUnit.AMPERE: UnitOfElectricCurrent.AMPERE,
}
def add_lcn_entities(
config_entry: ConfigEntry,
@ -103,8 +118,10 @@ class LcnVariableSensor(LcnEntity, SensorEntity):
config[CONF_DOMAIN_DATA][CONF_UNIT_OF_MEASUREMENT]
)
self._attr_native_unit_of_measurement = cast(str, self.unit.value)
self._attr_device_class = DEVICE_CLASS_MAPPING.get(self.unit, None)
self._attr_native_unit_of_measurement = UNIT_OF_MEASUREMENT_MAPPING.get(
self.unit
)
self._attr_device_class = DEVICE_CLASS_MAPPING.get(self.unit)
async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""