Add device_class for LCN sensors (#127921)
* Add device_class for lcn sensor * Rename device_class mapping dictionary
This commit is contained in:
parent
9d9b5af97f
commit
017ba509a6
2 changed files with 26 additions and 7 deletions
|
@ -7,7 +7,11 @@ from typing import cast
|
|||
|
||||
import pypck
|
||||
|
||||
from homeassistant.components.sensor import DOMAIN as DOMAIN_SENSOR, SensorEntity
|
||||
from homeassistant.components.sensor import (
|
||||
DOMAIN as DOMAIN_SENSOR,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_DOMAIN,
|
||||
|
@ -32,6 +36,17 @@ from .const import (
|
|||
from .entity import LcnEntity
|
||||
from .helpers import InputType
|
||||
|
||||
DEVICE_CLASS_MAPPING = {
|
||||
pypck.lcn_defs.VarUnit.CELSIUS: SensorDeviceClass.TEMPERATURE,
|
||||
pypck.lcn_defs.VarUnit.KELVIN: SensorDeviceClass.TEMPERATURE,
|
||||
pypck.lcn_defs.VarUnit.FAHRENHEIT: SensorDeviceClass.TEMPERATURE,
|
||||
pypck.lcn_defs.VarUnit.LUX_T: SensorDeviceClass.ILLUMINANCE,
|
||||
pypck.lcn_defs.VarUnit.LUX_I: SensorDeviceClass.ILLUMINANCE,
|
||||
pypck.lcn_defs.VarUnit.METERPERSECOND: SensorDeviceClass.SPEED,
|
||||
pypck.lcn_defs.VarUnit.VOLT: SensorDeviceClass.VOLTAGE,
|
||||
pypck.lcn_defs.VarUnit.AMPERE: SensorDeviceClass.CURRENT,
|
||||
}
|
||||
|
||||
|
||||
def add_lcn_entities(
|
||||
config_entry: ConfigEntry,
|
||||
|
@ -87,7 +102,9 @@ class LcnVariableSensor(LcnEntity, SensorEntity):
|
|||
self.unit = pypck.lcn_defs.VarUnit.parse(
|
||||
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)
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Run when entity about to be added to hass."""
|
||||
|
|
|
@ -113,7 +113,7 @@
|
|||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_device_class': <SensorDeviceClass.TEMPERATURE: 'temperature'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Sensor_Setpoint1',
|
||||
'platform': 'lcn',
|
||||
|
@ -121,14 +121,15 @@
|
|||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'unique_id': 'lcn/config_entry_pchk.json-m000007-r1varsetpoint',
|
||||
'unit_of_measurement': '°C',
|
||||
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_setup_lcn_sensor[sensor.sensor_setpoint1-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'temperature',
|
||||
'friendly_name': 'Sensor_Setpoint1',
|
||||
'unit_of_measurement': '°C',
|
||||
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.sensor_setpoint1',
|
||||
|
@ -160,7 +161,7 @@
|
|||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_device_class': <SensorDeviceClass.TEMPERATURE: 'temperature'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Sensor_Var1',
|
||||
'platform': 'lcn',
|
||||
|
@ -168,14 +169,15 @@
|
|||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'unique_id': 'lcn/config_entry_pchk.json-m000007-var1',
|
||||
'unit_of_measurement': '°C',
|
||||
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_setup_lcn_sensor[sensor.sensor_var1-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'temperature',
|
||||
'friendly_name': 'Sensor_Var1',
|
||||
'unit_of_measurement': '°C',
|
||||
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.sensor_var1',
|
||||
|
|
Loading…
Add table
Reference in a new issue