From 017ba509a6eae3c544182962ed44f14c616a00dc Mon Sep 17 00:00:00 2001 From: Andre Lengwenus Date: Tue, 8 Oct 2024 14:24:48 +0200 Subject: [PATCH] Add device_class for LCN sensors (#127921) * Add device_class for lcn sensor * Rename device_class mapping dictionary --- homeassistant/components/lcn/sensor.py | 19 ++++++++++++++++++- .../components/lcn/snapshots/test_sensor.ambr | 14 ++++++++------ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/lcn/sensor.py b/homeassistant/components/lcn/sensor.py index 341182c0639..5a360d44b8c 100644 --- a/homeassistant/components/lcn/sensor.py +++ b/homeassistant/components/lcn/sensor.py @@ -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.""" diff --git a/tests/components/lcn/snapshots/test_sensor.ambr b/tests/components/lcn/snapshots/test_sensor.ambr index d6ac73b5822..56776e3e0f6 100644 --- a/tests/components/lcn/snapshots/test_sensor.ambr +++ b/tests/components/lcn/snapshots/test_sensor.ambr @@ -113,7 +113,7 @@ 'name': None, 'options': dict({ }), - 'original_device_class': None, + 'original_device_class': , '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': , }) # --- # 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': , }), 'context': , 'entity_id': 'sensor.sensor_setpoint1', @@ -160,7 +161,7 @@ 'name': None, 'options': dict({ }), - 'original_device_class': None, + 'original_device_class': , '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': , }) # --- # 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': , }), 'context': , 'entity_id': 'sensor.sensor_var1',