Speed up lookup of AirVisual pollutant labels, levels, and units (#52327)

* Speed up lookup of AirVisual pollutant levels, labels, and units

* Mispellings
This commit is contained in:
Aaron Bach 2021-06-30 04:33:50 -05:00 committed by GitHub
parent da9bb99ba8
commit 3902f0bdd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -56,57 +56,32 @@ NODE_PRO_SENSORS = [
(SENSOR_KIND_TEMPERATURE, "Temperature", DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS), (SENSOR_KIND_TEMPERATURE, "Temperature", DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS),
] ]
POLLUTANT_LABELS = {
"co": "Carbon Monoxide",
"n2": "Nitrogen Dioxide",
"o3": "Ozone",
"p1": "PM10",
"p2": "PM2.5",
"s2": "Sulfur Dioxide",
}
@callback POLLUTANT_LEVELS = {
def async_get_pollutant_label(symbol): (0, 50): ("Good", "mdi:emoticon-excited"),
"""Get a pollutant's label based on its symbol.""" (51, 100): ("Moderate", "mdi:emoticon-happy"),
if symbol == "co": (101, 150): ("Unhealthy for sensitive groups", "mdi:emoticon-neutral"),
return "Carbon Monoxide" (151, 200): ("Unhealthy", "mdi:emoticon-sad"),
if symbol == "n2": (201, 300): ("Very unhealthy", "mdi:emoticon-dead"),
return "Nitrogen Dioxide" (301, 1000): ("Hazardous", "mdi:biohazard"),
if symbol == "o3": }
return "Ozone"
if symbol == "p1":
return "PM10"
if symbol == "p2":
return "PM2.5"
if symbol == "s2":
return "Sulfur Dioxide"
return symbol
POLLUTANT_UNITS = {
@callback "co": CONCENTRATION_PARTS_PER_MILLION,
def async_get_pollutant_level_info(value): "n2": CONCENTRATION_PARTS_PER_BILLION,
"""Return a verbal pollutant level (and associated icon) for a numeric value.""" "o3": CONCENTRATION_PARTS_PER_BILLION,
if 0 <= value <= 50: "p1": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
return ("Good", "mdi:emoticon-excited") "p2": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
if 51 <= value <= 100: "s2": CONCENTRATION_PARTS_PER_BILLION,
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")
@callback
def async_get_pollutant_unit(symbol):
"""Get a pollutant's unit based on its symbol."""
if symbol == "co":
return CONCENTRATION_PARTS_PER_MILLION
if symbol == "n2":
return CONCENTRATION_PARTS_PER_BILLION
if symbol == "o3":
return CONCENTRATION_PARTS_PER_BILLION
if symbol == "p1":
return CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
if symbol == "p2":
return CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
if symbol == "s2":
return CONCENTRATION_PARTS_PER_BILLION
return None
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
@ -197,16 +172,20 @@ class AirVisualGeographySensor(AirVisualEntity, SensorEntity):
if self._kind == SENSOR_KIND_LEVEL: if self._kind == SENSOR_KIND_LEVEL:
aqi = data[f"aqi{self._locale}"] aqi = data[f"aqi{self._locale}"]
self._state, self._attr_icon = async_get_pollutant_level_info(aqi) [(self._state, self._attr_icon)] = [
(name, icon)
for (floor, ceiling), (name, icon) in POLLUTANT_LEVELS.items()
if floor <= aqi <= ceiling
]
elif self._kind == SENSOR_KIND_AQI: elif self._kind == SENSOR_KIND_AQI:
self._state = data[f"aqi{self._locale}"] self._state = data[f"aqi{self._locale}"]
elif self._kind == SENSOR_KIND_POLLUTANT: elif self._kind == SENSOR_KIND_POLLUTANT:
symbol = data[f"main{self._locale}"] symbol = data[f"main{self._locale}"]
self._state = async_get_pollutant_label(symbol) self._state = POLLUTANT_LABELS[symbol]
self._attrs.update( self._attrs.update(
{ {
ATTR_POLLUTANT_SYMBOL: symbol, ATTR_POLLUTANT_SYMBOL: symbol,
ATTR_POLLUTANT_UNIT: async_get_pollutant_unit(symbol), ATTR_POLLUTANT_UNIT: POLLUTANT_UNITS[symbol],
} }
) )