From f4a71ea83f6fd7a9cea02ef0f4297a06452fd913 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 5 Jan 2023 23:17:29 +0100 Subject: [PATCH] Fix translation keys for NAM sensors (#85245) Co-authored-by: Maciej Bieniek --- homeassistant/components/nam/sensor.py | 102 ++++++++++++++-------- homeassistant/components/nam/strings.json | 4 +- tests/components/nam/test_sensor.py | 16 ++-- 3 files changed, 76 insertions(+), 46 deletions(-) diff --git a/homeassistant/components/nam/sensor.py b/homeassistant/components/nam/sensor.py index 13ed5675b3c..5dab563d409 100644 --- a/homeassistant/components/nam/sensor.py +++ b/homeassistant/components/nam/sensor.py @@ -1,6 +1,7 @@ """Support for the Nettigo Air Monitor service.""" from __future__ import annotations +from dataclasses import dataclass from datetime import datetime, timedelta import logging from typing import cast @@ -70,208 +71,224 @@ PARALLEL_UPDATES = 1 _LOGGER = logging.getLogger(__name__) -SENSORS: tuple[SensorEntityDescription, ...] = ( - SensorEntityDescription( +AQI_LEVEL_STATE_MAPPING = { + "very low": "very_low", + "low": "low", + "medium": "medium", + "high": "high", + "very high": "very_high", +} + + +@dataclass +class NAMSensorEntityDescription(SensorEntityDescription): + """Describes NAM sensor entity.""" + + mapping: dict[str, str] | None = None + + +SENSORS: tuple[NAMSensorEntityDescription, ...] = ( + NAMSensorEntityDescription( key=ATTR_BME280_HUMIDITY, name="BME280 humidity", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.HUMIDITY, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_BME280_PRESSURE, name="BME280 pressure", native_unit_of_measurement=UnitOfPressure.HPA, device_class=SensorDeviceClass.PRESSURE, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_BME280_TEMPERATURE, name="BME280 temperature", native_unit_of_measurement=UnitOfTemperature.CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_BMP180_PRESSURE, name="BMP180 pressure", native_unit_of_measurement=UnitOfPressure.HPA, device_class=SensorDeviceClass.PRESSURE, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_BMP180_TEMPERATURE, name="BMP180 temperature", native_unit_of_measurement=UnitOfTemperature.CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_BMP280_PRESSURE, name="BMP280 pressure", native_unit_of_measurement=UnitOfPressure.HPA, device_class=SensorDeviceClass.PRESSURE, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_BMP280_TEMPERATURE, name="BMP280 temperature", native_unit_of_measurement=UnitOfTemperature.CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_HECA_HUMIDITY, name="HECA humidity", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.HUMIDITY, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_HECA_TEMPERATURE, name="HECA temperature", native_unit_of_measurement=UnitOfTemperature.CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_MHZ14A_CARBON_DIOXIDE, name="MH-Z14A carbon dioxide", native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION, device_class=SensorDeviceClass.CO2, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_PMSX003_CAQI, name="PMSx003 CAQI", icon="mdi:air-filter", ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_PMSX003_CAQI_LEVEL, name="PMSx003 CAQI level", icon="mdi:air-filter", device_class=SensorDeviceClass.ENUM, - options=["very low", "low", "medium", "high", "very high"], + mapping=AQI_LEVEL_STATE_MAPPING, translation_key="caqi_level", ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_PMSX003_P0, name="PMSx003 particulate matter 1.0", native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.PM1, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_PMSX003_P1, name="PMSx003 particulate matter 10", native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.PM10, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_PMSX003_P2, name="PMSx003 particulate matter 2.5", native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.PM25, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_SDS011_CAQI, name="SDS011 CAQI", icon="mdi:air-filter", ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_SDS011_CAQI_LEVEL, name="SDS011 CAQI level", icon="mdi:air-filter", device_class=SensorDeviceClass.ENUM, - options=["very low", "low", "medium", "high", "very high"], + mapping=AQI_LEVEL_STATE_MAPPING, translation_key="caqi_level", ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_SDS011_P1, name="SDS011 particulate matter 10", native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.PM10, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_SDS011_P2, name="SDS011 particulate matter 2.5", native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.PM25, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_SHT3X_HUMIDITY, name="SHT3X humidity", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.HUMIDITY, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_SHT3X_TEMPERATURE, name="SHT3X temperature", native_unit_of_measurement=UnitOfTemperature.CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_SPS30_CAQI, name="SPS30 CAQI", icon="mdi:air-filter", ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_SPS30_CAQI_LEVEL, name="SPS30 CAQI level", icon="mdi:air-filter", device_class=SensorDeviceClass.ENUM, - options=["very low", "low", "medium", "high", "very high"], + mapping=AQI_LEVEL_STATE_MAPPING, translation_key="caqi_level", ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_SPS30_P0, name="SPS30 particulate matter 1.0", native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.PM1, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_SPS30_P1, name="SPS30 particulate matter 10", native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.PM10, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_SPS30_P2, name="SPS30 particulate matter 2.5", native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.PM25, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_SPS30_P4, name="SPS30 particulate matter 4.0", native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, icon="mdi:molecule", state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_DHT22_HUMIDITY, name="DHT22 humidity", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.HUMIDITY, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_DHT22_TEMPERATURE, name="DHT22 temperature", native_unit_of_measurement=UnitOfTemperature.CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_SIGNAL_STRENGTH, name="Signal strength", native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, @@ -280,7 +297,7 @@ SENSORS: tuple[SensorEntityDescription, ...] = ( state_class=SensorStateClass.MEASUREMENT, entity_category=EntityCategory.DIAGNOSTIC, ), - SensorEntityDescription( + NAMSensorEntityDescription( key=ATTR_UPTIME, name="Uptime", device_class=SensorDeviceClass.TIMESTAMP, @@ -326,11 +343,12 @@ class NAMSensor(CoordinatorEntity[NAMDataUpdateCoordinator], SensorEntity): """Define an Nettigo Air Monitor sensor.""" _attr_has_entity_name = True + entity_description: NAMSensorEntityDescription def __init__( self, coordinator: NAMDataUpdateCoordinator, - description: SensorEntityDescription, + description: NAMSensorEntityDescription, ) -> None: """Initialize.""" super().__init__(coordinator) @@ -341,6 +359,11 @@ class NAMSensor(CoordinatorEntity[NAMDataUpdateCoordinator], SensorEntity): @property def native_value(self) -> StateType | datetime: """Return the state.""" + if self.entity_description.mapping is not None: + return self.entity_description.mapping[ + cast(str, getattr(self.coordinator.data, self.entity_description.key)) + ] + return cast( StateType, getattr(self.coordinator.data, self.entity_description.key) ) @@ -358,6 +381,13 @@ class NAMSensor(CoordinatorEntity[NAMDataUpdateCoordinator], SensorEntity): and getattr(self.coordinator.data, self.entity_description.key) is not None ) + @property + def options(self) -> list[str] | None: + """If the entity description provides a mapping, use that.""" + if self.entity_description.mapping: + return list(self.entity_description.mapping.values()) + return super().options + class NAMSensorUptime(NAMSensor): """Define an Nettigo Air Monitor uptime sensor.""" diff --git a/homeassistant/components/nam/strings.json b/homeassistant/components/nam/strings.json index a37bcc2983e..17983505e91 100644 --- a/homeassistant/components/nam/strings.json +++ b/homeassistant/components/nam/strings.json @@ -42,11 +42,11 @@ "sensor": { "caqi_level": { "state": { - "very low": "Very low", + "very_low": "Very low", "low": "Low", "medium": "Medium", "high": "High", - "very high": "Very high" + "very_high": "Very high" } } } diff --git a/tests/components/nam/test_sensor.py b/tests/components/nam/test_sensor.py index 271f8a7cb85..953e982b3b4 100644 --- a/tests/components/nam/test_sensor.py +++ b/tests/components/nam/test_sensor.py @@ -231,14 +231,14 @@ async def test_sensor(hass): state = hass.states.get("sensor.nettigo_air_monitor_pmsx003_caqi_level") assert state - assert state.state == "very low" + assert state.state == "very_low" assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENUM assert state.attributes.get(ATTR_OPTIONS) == [ - "very low", + "very_low", "low", "medium", "high", - "very high", + "very_high", ] assert state.attributes.get(ATTR_ICON) == "mdi:air-filter" @@ -331,14 +331,14 @@ async def test_sensor(hass): state = hass.states.get("sensor.nettigo_air_monitor_sds011_caqi_level") assert state - assert state.state == "very low" + assert state.state == "very_low" assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENUM assert state.attributes.get(ATTR_OPTIONS) == [ - "very low", + "very_low", "low", "medium", "high", - "very high", + "very_high", ] assert state.attributes.get(ATTR_ICON) == "mdi:air-filter" @@ -377,11 +377,11 @@ async def test_sensor(hass): assert state.state == "medium" assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENUM assert state.attributes.get(ATTR_OPTIONS) == [ - "very low", + "very_low", "low", "medium", "high", - "very high", + "very_high", ] assert state.attributes.get(ATTR_ICON) == "mdi:air-filter"