From f0e6f45e430ad793d5423250990d30e4eef69dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 17 Jan 2023 00:24:50 +0200 Subject: [PATCH] Remove signal strength state class from Huawei LTE transmit power sensor (#85973) Co-authored-by: Franck Nijhof --- homeassistant/components/huawei_lte/sensor.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/huawei_lte/sensor.py b/homeassistant/components/huawei_lte/sensor.py index 0b9983bab51..adb6cb1f06e 100644 --- a/homeassistant/components/huawei_lte/sensor.py +++ b/homeassistant/components/huawei_lte/sensor.py @@ -118,6 +118,7 @@ class HuaweiSensorEntityDescription(SensorEntityDescription): format_fn: Callable[[str], tuple[StateType, str | None]] = format_default icon_fn: Callable[[StateType], str] | None = None + device_class_fn: Callable[[StateType], SensorDeviceClass | None] | None = None last_reset_item: str | None = None last_reset_format_fn: Callable[[str | None], datetime | None] | None = None @@ -351,7 +352,15 @@ SENSOR_META: dict[str, HuaweiSensorGroup] = { "txpower": HuaweiSensorEntityDescription( key="txpower", name="Transmit power", - device_class=SensorDeviceClass.SIGNAL_STRENGTH, + # The value we get from the API tends to consist of several, e.g. + # PPusch:15dBm PPucch:2dBm PSrs:42dBm PPrach:1dBm + # Present as SIGNAL_STRENGTH only if it was parsed to a number. + # We could try to parse this to separate component sensors sometime. + device_class_fn=lambda x: ( + SensorDeviceClass.SIGNAL_STRENGTH + if isinstance(x, (float, int)) + else None + ), entity_category=EntityCategory.DIAGNOSTIC, ), "ul_mcs": HuaweiSensorEntityDescription( @@ -744,6 +753,14 @@ class HuaweiLteSensor(HuaweiLteBaseEntityWithDevice, SensorEntity): return self.entity_description.icon_fn(self.state) return self.entity_description.icon + @property + def device_class(self) -> SensorDeviceClass | None: + """Return device class for sensor.""" + if self.entity_description.device_class_fn: + # Note: using self.state could infloop here. + return self.entity_description.device_class_fn(self.native_value) + return super().device_class + @property def last_reset(self) -> datetime | None: """Return the time when the sensor was last reset, if any."""