From 4c854a06d9d68716c7649f737ee19796fcbad97a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 30 Sep 2021 12:27:52 +0300 Subject: [PATCH] Add some huawei_lte sensor state classifications (#55601) --- homeassistant/components/huawei_lte/sensor.py | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/huawei_lte/sensor.py b/homeassistant/components/huawei_lte/sensor.py index f62450088ae..568f7c31a53 100644 --- a/homeassistant/components/huawei_lte/sensor.py +++ b/homeassistant/components/huawei_lte/sensor.py @@ -13,6 +13,8 @@ from homeassistant.components.sensor import ( DEVICE_CLASS_BATTERY, DEVICE_CLASS_SIGNAL_STRENGTH, DOMAIN as SENSOR_DOMAIN, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_TOTAL_INCREASING, SensorEntity, ) from homeassistant.config_entries import ConfigEntry @@ -54,6 +56,7 @@ class SensorMeta(NamedTuple): device_class: str | None = None icon: str | Callable[[StateType], str] | None = None unit: str | None = None + state_class: str | None = None enabled_default: bool = False include: re.Pattern[str] | None = None exclude: re.Pattern[str] | None = None @@ -123,6 +126,7 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = { "mdi:signal-cellular-2", "mdi:signal-cellular-3", )[bisect((-11, -8, -5), x if x is not None else -1000)], + state_class=STATE_CLASS_MEASUREMENT, enabled_default=True, ), (KEY_DEVICE_SIGNAL, "rsrp"): SensorMeta( @@ -135,6 +139,7 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = { "mdi:signal-cellular-2", "mdi:signal-cellular-3", )[bisect((-110, -95, -80), x if x is not None else -1000)], + state_class=STATE_CLASS_MEASUREMENT, enabled_default=True, ), (KEY_DEVICE_SIGNAL, "rssi"): SensorMeta( @@ -147,6 +152,7 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = { "mdi:signal-cellular-2", "mdi:signal-cellular-3", )[bisect((-80, -70, -60), x if x is not None else -1000)], + state_class=STATE_CLASS_MEASUREMENT, enabled_default=True, ), (KEY_DEVICE_SIGNAL, "sinr"): SensorMeta( @@ -159,6 +165,7 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = { "mdi:signal-cellular-2", "mdi:signal-cellular-3", )[bisect((0, 5, 10), x if x is not None else -1000)], + state_class=STATE_CLASS_MEASUREMENT, enabled_default=True, ), (KEY_DEVICE_SIGNAL, "rscp"): SensorMeta( @@ -171,6 +178,7 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = { "mdi:signal-cellular-2", "mdi:signal-cellular-3", )[bisect((-95, -85, -75), x if x is not None else -1000)], + state_class=STATE_CLASS_MEASUREMENT, ), (KEY_DEVICE_SIGNAL, "ecio"): SensorMeta( name="EC/IO", @@ -182,6 +190,7 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = { "mdi:signal-cellular-2", "mdi:signal-cellular-3", )[bisect((-20, -10, -6), x if x is not None else -1000)], + state_class=STATE_CLASS_MEASUREMENT, ), (KEY_DEVICE_SIGNAL, "transmode"): SensorMeta(name="Transmission mode"), (KEY_DEVICE_SIGNAL, "cqi0"): SensorMeta( @@ -219,10 +228,16 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = { exclude=re.compile(r"^month(duration|lastcleartime)$", re.IGNORECASE) ), (KEY_MONITORING_MONTH_STATISTICS, "CurrentMonthDownload"): SensorMeta( - name="Current month download", unit=DATA_BYTES, icon="mdi:download" + name="Current month download", + unit=DATA_BYTES, + icon="mdi:download", + state_class=STATE_CLASS_TOTAL_INCREASING, ), (KEY_MONITORING_MONTH_STATISTICS, "CurrentMonthUpload"): SensorMeta( - name="Current month upload", unit=DATA_BYTES, icon="mdi:upload" + name="Current month upload", + unit=DATA_BYTES, + icon="mdi:upload", + state_class=STATE_CLASS_TOTAL_INCREASING, ), KEY_MONITORING_STATUS: SensorMeta( include=re.compile( @@ -257,29 +272,43 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = { name="Current connection duration", unit=TIME_SECONDS, icon="mdi:timer-outline" ), (KEY_MONITORING_TRAFFIC_STATISTICS, "CurrentDownload"): SensorMeta( - name="Current connection download", unit=DATA_BYTES, icon="mdi:download" + name="Current connection download", + unit=DATA_BYTES, + icon="mdi:download", + state_class=STATE_CLASS_TOTAL_INCREASING, ), (KEY_MONITORING_TRAFFIC_STATISTICS, "CurrentDownloadRate"): SensorMeta( name="Current download rate", unit=DATA_RATE_BYTES_PER_SECOND, icon="mdi:download", + state_class=STATE_CLASS_MEASUREMENT, ), (KEY_MONITORING_TRAFFIC_STATISTICS, "CurrentUpload"): SensorMeta( - name="Current connection upload", unit=DATA_BYTES, icon="mdi:upload" + name="Current connection upload", + unit=DATA_BYTES, + icon="mdi:upload", + state_class=STATE_CLASS_TOTAL_INCREASING, ), (KEY_MONITORING_TRAFFIC_STATISTICS, "CurrentUploadRate"): SensorMeta( name="Current upload rate", unit=DATA_RATE_BYTES_PER_SECOND, icon="mdi:upload", + state_class=STATE_CLASS_MEASUREMENT, ), (KEY_MONITORING_TRAFFIC_STATISTICS, "TotalConnectTime"): SensorMeta( name="Total connected duration", unit=TIME_SECONDS, icon="mdi:timer-outline" ), (KEY_MONITORING_TRAFFIC_STATISTICS, "TotalDownload"): SensorMeta( - name="Total download", unit=DATA_BYTES, icon="mdi:download" + name="Total download", + unit=DATA_BYTES, + icon="mdi:download", + state_class=STATE_CLASS_TOTAL_INCREASING, ), (KEY_MONITORING_TRAFFIC_STATISTICS, "TotalUpload"): SensorMeta( - name="Total upload", unit=DATA_BYTES, icon="mdi:upload" + name="Total upload", + unit=DATA_BYTES, + icon="mdi:upload", + state_class=STATE_CLASS_TOTAL_INCREASING, ), KEY_NET_CURRENT_PLMN: SensorMeta( exclude=re.compile(r"^(Rat|ShortName|Spn)$", re.IGNORECASE) @@ -455,6 +484,11 @@ class HuaweiLteSensor(HuaweiLteBaseEntity, SensorEntity): return icon(self.state) return icon + @property + def state_class(self) -> str | None: + """Return sensor state class.""" + return self.meta.state_class + @property def entity_registry_enabled_default(self) -> bool: """Return if the entity should be enabled when first added to the entity registry."""