1
mirror of https://github.com/home-assistant/core synced 2024-07-24 16:42:06 +02:00

Replace strange "dict logic" in AirVisual pollutant level sensors (1 of 2) (#44868)

This commit is contained in:
Aaron Bach 2021-01-20 13:24:56 -07:00 committed by GitHub
parent 14f5eb7305
commit 7ff02fe8d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -58,25 +58,6 @@ NODE_PRO_SENSORS = [
(SENSOR_KIND_TEMPERATURE, "Temperature", DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS),
]
POLLUTANT_LEVEL_MAPPING = [
{"label": "Good", "icon": "mdi:emoticon-excited", "minimum": 0, "maximum": 50},
{"label": "Moderate", "icon": "mdi:emoticon-happy", "minimum": 51, "maximum": 100},
{
"label": "Unhealthy for sensitive groups",
"icon": "mdi:emoticon-neutral",
"minimum": 101,
"maximum": 150,
},
{"label": "Unhealthy", "icon": "mdi:emoticon-sad", "minimum": 151, "maximum": 200},
{
"label": "Very Unhealthy",
"icon": "mdi:emoticon-dead",
"minimum": 201,
"maximum": 300,
},
{"label": "Hazardous", "icon": "mdi:biohazard", "minimum": 301, "maximum": 10000},
]
POLLUTANT_MAPPING = {
"co": {"label": "Carbon Monoxide", "unit": CONCENTRATION_PARTS_PER_MILLION},
"n2": {"label": "Nitrogen Dioxide", "unit": CONCENTRATION_PARTS_PER_BILLION},
@ -87,6 +68,22 @@ POLLUTANT_MAPPING = {
}
@callback
def async_get_pollutant_level_info(value):
"""Return a verbal pollutant level (and associated icon) for a numeric value."""
if 0 <= value <= 50:
return ("Good", "mdi:emoticon-excited")
if 51 <= value <= 100:
return ("Moderate", "mdi:emoticon-happy")
if 101 <= value <= 150:
return ("Unhealthy for sensitive groups", "mdi:emoticon-neutral")
if 151 <= value <= 200:
return ("Unhealthy", "mdi:emoticon-sad")
if 201 <= value <= 300:
return ("Very Unhealthy", "mdi:emoticon-dead")
return ("Hazardous", "mdi:biohazard")
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up AirVisual sensors based on a config entry."""
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][config_entry.entry_id]
@ -171,13 +168,7 @@ class AirVisualGeographySensor(AirVisualEntity):
if self._kind == SENSOR_KIND_LEVEL:
aqi = data[f"aqi{self._locale}"]
[level] = [
i
for i in POLLUTANT_LEVEL_MAPPING
if i["minimum"] <= aqi <= i["maximum"]
]
self._state = level["label"]
self._icon = level["icon"]
self._state, self._icon = async_get_pollutant_level_info(aqi)
elif self._kind == SENSOR_KIND_AQI:
self._state = data[f"aqi{self._locale}"]
elif self._kind == SENSOR_KIND_POLLUTANT: