From bf4ca2d68dee739bda0304829ab4fa2e8c2c1660 Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Mon, 19 Jul 2021 10:01:36 -0600 Subject: [PATCH] Modify AirVisual states to be translatable (#53133) * Modify AirVisual states to be translatable * Make constant names consistent --- homeassistant/components/airvisual/sensor.py | 42 ++++++++++++------- .../components/airvisual/strings.sensor.json | 20 +++++++++ .../airvisual/translations/sensor.en.json | 20 +++++++++ 3 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 homeassistant/components/airvisual/strings.sensor.json create mode 100644 homeassistant/components/airvisual/translations/sensor.en.json diff --git a/homeassistant/components/airvisual/sensor.py b/homeassistant/components/airvisual/sensor.py index fb6cc0c5e26..796607f8215 100644 --- a/homeassistant/components/airvisual/sensor.py +++ b/homeassistant/components/airvisual/sensor.py @@ -37,6 +37,9 @@ ATTR_POLLUTANT_SYMBOL = "pollutant_symbol" ATTR_POLLUTANT_UNIT = "pollutant_unit" ATTR_REGION = "region" +DEVICE_CLASS_POLLUTANT_LABEL = "airvisual__pollutant_label" +DEVICE_CLASS_POLLUTANT_LEVEL = "airvisual__pollutant_level" + SENSOR_KIND_AQI = "air_quality_index" SENSOR_KIND_BATTERY_LEVEL = "battery_level" SENSOR_KIND_CO2 = "carbon_dioxide" @@ -105,22 +108,27 @@ NODE_PRO_SENSORS = [ ), ] -POLLUTANT_LABELS = { - "co": "Carbon Monoxide", - "n2": "Nitrogen Dioxide", - "o3": "Ozone", - "p1": "PM10", - "p2": "PM2.5", - "s2": "Sulfur Dioxide", -} +STATE_POLLUTANT_LABEL_CO = "co" +STATE_POLLUTANT_LABEL_N2 = "n2" +STATE_POLLUTANT_LABEL_O3 = "o3" +STATE_POLLUTANT_LABEL_P1 = "p1" +STATE_POLLUTANT_LABEL_P2 = "p2" +STATE_POLLUTANT_LABEL_S2 = "s2" + +STATE_POLLUTANT_LEVEL_GOOD = "good" +STATE_POLLUTANT_LEVEL_MODERATE = "moderate" +STATE_POLLUTANT_LEVEL_UNHEALTHY_SENSITIVE = "unhealthy_sensitive" +STATE_POLLUTANT_LEVEL_UNHEALTHY = "unhealthy" +STATE_POLLUTANT_LEVEL_VERY_UNHEALTHY = "very_unhealthy" +STATE_POLLUTANT_LEVEL_HAZARDOUS = "hazardous" POLLUTANT_LEVELS = { - (0, 50): ("Good", "mdi:emoticon-excited"), - (51, 100): ("Moderate", "mdi:emoticon-happy"), - (101, 150): ("Unhealthy for sensitive groups", "mdi:emoticon-neutral"), - (151, 200): ("Unhealthy", "mdi:emoticon-sad"), - (201, 300): ("Very unhealthy", "mdi:emoticon-dead"), - (301, 1000): ("Hazardous", "mdi:biohazard"), + (0, 50): (STATE_POLLUTANT_LEVEL_GOOD, "mdi:emoticon-excited"), + (51, 100): (STATE_POLLUTANT_LEVEL_MODERATE, "mdi:emoticon-happy"), + (101, 150): (STATE_POLLUTANT_LEVEL_UNHEALTHY_SENSITIVE, "mdi:emoticon-neutral"), + (151, 200): (STATE_POLLUTANT_LEVEL_UNHEALTHY, "mdi:emoticon-sad"), + (201, 300): (STATE_POLLUTANT_LEVEL_VERY_UNHEALTHY, "mdi:emoticon-dead"), + (301, 1000): (STATE_POLLUTANT_LEVEL_HAZARDOUS, "mdi:biohazard"), } POLLUTANT_UNITS = { @@ -170,6 +178,10 @@ class AirVisualGeographySensor(AirVisualEntity, SensorEntity): """Initialize.""" super().__init__(coordinator) + if kind == SENSOR_KIND_LEVEL: + self._attr_device_class = DEVICE_CLASS_POLLUTANT_LEVEL + elif kind == SENSOR_KIND_POLLUTANT: + self._attr_device_class = DEVICE_CLASS_POLLUTANT_LABEL self._attr_extra_state_attributes.update( { ATTR_CITY: config_entry.data.get(CONF_CITY), @@ -209,7 +221,7 @@ class AirVisualGeographySensor(AirVisualEntity, SensorEntity): self._attr_state = data[f"aqi{self._locale}"] elif self._kind == SENSOR_KIND_POLLUTANT: symbol = data[f"main{self._locale}"] - self._attr_state = POLLUTANT_LABELS[symbol] + self._attr_state = symbol self._attr_extra_state_attributes.update( { ATTR_POLLUTANT_SYMBOL: symbol, diff --git a/homeassistant/components/airvisual/strings.sensor.json b/homeassistant/components/airvisual/strings.sensor.json new file mode 100644 index 00000000000..583ddaf4f3b --- /dev/null +++ b/homeassistant/components/airvisual/strings.sensor.json @@ -0,0 +1,20 @@ +{ + "state": { + "airvisual__pollutant_label": { + "co": "Carbon Monoxide", + "n2": "Nitrogen Dioxide", + "o3": "Ozone", + "p1": "PM10", + "p2": "PM2.5", + "s2": "Sulfur Dioxide" + }, + "airvisual__pollutant_level": { + "good": "Good", + "moderate": "Moderate", + "unhealthy": "Unhealthy", + "unhealthy_sensitive": "Unhealthy for sensitive groups", + "very_unhealthy": "Very unhealthy", + "hazardous": "Hazardous" + } + } +} diff --git a/homeassistant/components/airvisual/translations/sensor.en.json b/homeassistant/components/airvisual/translations/sensor.en.json new file mode 100644 index 00000000000..314cf34562a --- /dev/null +++ b/homeassistant/components/airvisual/translations/sensor.en.json @@ -0,0 +1,20 @@ +{ + "state": { + "airvisual__pollutant_label": { + "co": "Carbon Monoxide", + "n2": "Nitrogen Dioxide", + "o3": "Ozone", + "p1": "PM10", + "p2": "PM2.5", + "s2": "Sulfur Dioxide" + }, + "airvisual__pollutant_level": { + "good": "Good", + "hazardous": "Hazardous", + "moderate": "Moderate", + "unhealthy": "Unhealthy", + "unhealthy_sensitive": "Unhealthy for sensitive groups", + "very_unhealthy": "Very unhealthy" + } + } +} \ No newline at end of file