2019-04-03 15:40:03 +00:00
|
|
|
"""Support for AirVisual air quality sensors."""
|
2019-10-17 13:04:41 +00:00
|
|
|
from logging import getLogger
|
2017-09-08 14:05:51 +00:00
|
|
|
|
2017-09-24 19:25:18 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
ATTR_LATITUDE,
|
|
|
|
ATTR_LONGITUDE,
|
2020-02-29 03:14:17 +00:00
|
|
|
ATTR_STATE,
|
2020-02-25 01:52:14 +00:00
|
|
|
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
CONCENTRATION_PARTS_PER_BILLION,
|
|
|
|
CONCENTRATION_PARTS_PER_MILLION,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
2020-03-10 20:16:25 +00:00
|
|
|
CONF_SHOW_ON_MAP,
|
2019-10-17 13:04:41 +00:00
|
|
|
CONF_STATE,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-02-29 03:14:17 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2017-09-08 14:05:51 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2020-02-29 03:14:17 +00:00
|
|
|
|
|
|
|
from .const import CONF_CITY, CONF_COUNTRY, DATA_CLIENT, DOMAIN, TOPIC_UPDATE
|
2017-09-08 14:05:51 +00:00
|
|
|
|
2017-10-07 13:11:41 +00:00
|
|
|
_LOGGER = getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_CITY = "city"
|
|
|
|
ATTR_COUNTRY = "country"
|
|
|
|
ATTR_POLLUTANT_SYMBOL = "pollutant_symbol"
|
|
|
|
ATTR_POLLUTANT_UNIT = "pollutant_unit"
|
|
|
|
ATTR_REGION = "region"
|
2017-09-08 14:05:51 +00:00
|
|
|
|
2018-06-14 13:30:47 +00:00
|
|
|
DEFAULT_ATTRIBUTION = "Data provided by AirVisual"
|
2017-09-08 14:05:51 +00:00
|
|
|
|
2020-02-29 03:14:17 +00:00
|
|
|
MASS_PARTS_PER_MILLION = "ppm"
|
|
|
|
MASS_PARTS_PER_BILLION = "ppb"
|
|
|
|
VOLUME_MICROGRAMS_PER_CUBIC_METER = "µg/m3"
|
|
|
|
|
|
|
|
SENSOR_KIND_LEVEL = "air_pollution_level"
|
|
|
|
SENSOR_KIND_AQI = "air_quality_index"
|
|
|
|
SENSOR_KIND_POLLUTANT = "main_pollutant"
|
2018-06-14 13:30:47 +00:00
|
|
|
SENSORS = [
|
2020-02-29 03:14:17 +00:00
|
|
|
(SENSOR_KIND_LEVEL, "Air Pollution Level", "mdi:gauge", None),
|
|
|
|
(SENSOR_KIND_AQI, "Air Quality Index", "mdi:chart-line", "AQI"),
|
|
|
|
(SENSOR_KIND_POLLUTANT, "Main Pollutant", "mdi:chemical-weapon", None),
|
2018-02-15 22:52:47 +00:00
|
|
|
]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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,
|
2018-06-14 13:30:47 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
{"label": "Unhealthy", "icon": "mdi:emoticon-sad", "minimum": 151, "maximum": 200},
|
|
|
|
{
|
|
|
|
"label": "Very Unhealthy",
|
|
|
|
"icon": "mdi:emoticon-dead",
|
|
|
|
"minimum": 201,
|
|
|
|
"maximum": 300,
|
2018-06-14 13:30:47 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
{"label": "Hazardous", "icon": "mdi:biohazard", "minimum": 301, "maximum": 10000},
|
|
|
|
]
|
|
|
|
|
|
|
|
POLLUTANT_MAPPING = {
|
2020-02-25 01:52:14 +00:00
|
|
|
"co": {"label": "Carbon Monoxide", "unit": CONCENTRATION_PARTS_PER_MILLION},
|
|
|
|
"n2": {"label": "Nitrogen Dioxide", "unit": CONCENTRATION_PARTS_PER_BILLION},
|
|
|
|
"o3": {"label": "Ozone", "unit": CONCENTRATION_PARTS_PER_BILLION},
|
|
|
|
"p1": {"label": "PM10", "unit": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER},
|
|
|
|
"p2": {"label": "PM2.5", "unit": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER},
|
|
|
|
"s2": {"label": "Sulfur Dioxide", "unit": CONCENTRATION_PARTS_PER_BILLION},
|
2017-09-08 14:05:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_LOCALES = {"cn": "Chinese", "us": "U.S."}
|
|
|
|
|
2017-09-08 14:05:51 +00:00
|
|
|
|
2020-02-29 03:14:17 +00:00
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
|
|
"""Set up AirVisual sensors based on a config entry."""
|
|
|
|
airvisual = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
|
2018-02-15 22:52:47 +00:00
|
|
|
|
2020-02-29 03:14:17 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
AirVisualSensor(airvisual, kind, name, icon, unit, locale, geography_id)
|
|
|
|
for geography_id in airvisual.data
|
|
|
|
for locale in SENSOR_LOCALES
|
|
|
|
for kind, name, icon, unit in SENSORS
|
|
|
|
],
|
|
|
|
True,
|
|
|
|
)
|
2018-06-14 13:30:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AirVisualSensor(Entity):
|
|
|
|
"""Define an AirVisual sensor."""
|
|
|
|
|
2020-02-29 03:14:17 +00:00
|
|
|
def __init__(self, airvisual, kind, name, icon, unit, locale, geography_id):
|
2018-06-14 13:30:47 +00:00
|
|
|
"""Initialize."""
|
2020-02-29 03:14:17 +00:00
|
|
|
self._airvisual = airvisual
|
|
|
|
self._async_unsub_dispatcher_connects = []
|
|
|
|
self._geography_id = geography_id
|
2017-09-08 14:05:51 +00:00
|
|
|
self._icon = icon
|
2020-02-29 03:14:17 +00:00
|
|
|
self._kind = kind
|
2017-09-08 14:05:51 +00:00
|
|
|
self._locale = locale
|
|
|
|
self._name = name
|
|
|
|
self._state = None
|
2018-06-14 13:30:47 +00:00
|
|
|
self._unit = unit
|
2017-10-07 11:38:52 +00:00
|
|
|
|
2020-02-29 03:14:17 +00:00
|
|
|
self._attrs = {
|
|
|
|
ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION,
|
|
|
|
ATTR_CITY: airvisual.data[geography_id].get(CONF_CITY),
|
|
|
|
ATTR_STATE: airvisual.data[geography_id].get(CONF_STATE),
|
|
|
|
ATTR_COUNTRY: airvisual.data[geography_id].get(CONF_COUNTRY),
|
|
|
|
}
|
|
|
|
|
2018-06-14 13:30:47 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
2020-02-29 03:14:17 +00:00
|
|
|
try:
|
|
|
|
return bool(
|
|
|
|
self._airvisual.data[self._geography_id]["current"]["pollution"]
|
|
|
|
)
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device state attributes."""
|
|
|
|
return self._attrs
|
2018-06-14 13:30:47 +00:00
|
|
|
|
2017-09-08 14:05:51 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name."""
|
2020-02-23 21:38:05 +00:00
|
|
|
return f"{SENSOR_LOCALES[self._locale]} {self._name}"
|
2017-09-08 14:05:51 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state."""
|
|
|
|
return self._state
|
|
|
|
|
2018-02-15 22:52:47 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Return a unique, Home Assistant friendly identifier for this entity."""
|
2020-02-29 03:14:17 +00:00
|
|
|
return f"{self._geography_id}_{self._locale}_{self._kind}"
|
2018-02-15 22:52:47 +00:00
|
|
|
|
2017-09-08 14:05:51 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit the value is expressed in."""
|
2018-06-14 13:30:47 +00:00
|
|
|
return self._unit
|
2017-09-08 14:05:51 +00:00
|
|
|
|
2020-02-29 03:14:17 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update():
|
|
|
|
"""Update the state."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
|
|
|
self._async_unsub_dispatcher_connects.append(
|
|
|
|
async_dispatcher_connect(self.hass, TOPIC_UPDATE, update)
|
|
|
|
)
|
|
|
|
|
2018-06-14 13:30:47 +00:00
|
|
|
async def async_update(self):
|
|
|
|
"""Update the sensor."""
|
2020-02-29 03:14:17 +00:00
|
|
|
try:
|
|
|
|
data = self._airvisual.data[self._geography_id]["current"]["pollution"]
|
|
|
|
except KeyError:
|
2018-06-14 13:30:47 +00:00
|
|
|
return
|
2017-09-08 14:05:51 +00:00
|
|
|
|
2020-02-29 03:14:17 +00:00
|
|
|
if self._kind == SENSOR_KIND_LEVEL:
|
2019-09-03 14:11:36 +00:00
|
|
|
aqi = data[f"aqi{self._locale}"]
|
2018-06-14 13:30:47 +00:00
|
|
|
[level] = [
|
2019-07-31 19:25:30 +00:00
|
|
|
i
|
|
|
|
for i in POLLUTANT_LEVEL_MAPPING
|
|
|
|
if i["minimum"] <= aqi <= i["maximum"]
|
2018-06-14 13:30:47 +00:00
|
|
|
]
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = level["label"]
|
|
|
|
self._icon = level["icon"]
|
2020-02-29 03:14:17 +00:00
|
|
|
elif self._kind == SENSOR_KIND_AQI:
|
2019-09-03 14:11:36 +00:00
|
|
|
self._state = data[f"aqi{self._locale}"]
|
2020-02-29 03:14:17 +00:00
|
|
|
elif self._kind == SENSOR_KIND_POLLUTANT:
|
2019-09-03 14:11:36 +00:00
|
|
|
symbol = data[f"main{self._locale}"]
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = POLLUTANT_MAPPING[symbol]["label"]
|
|
|
|
self._attrs.update(
|
|
|
|
{
|
|
|
|
ATTR_POLLUTANT_SYMBOL: symbol,
|
|
|
|
ATTR_POLLUTANT_UNIT: POLLUTANT_MAPPING[symbol]["unit"],
|
|
|
|
}
|
|
|
|
)
|
2018-02-15 22:52:47 +00:00
|
|
|
|
2020-03-10 20:16:25 +00:00
|
|
|
geography = self._airvisual.geographies[self._geography_id]
|
|
|
|
if CONF_LATITUDE in geography:
|
|
|
|
if self._airvisual.options[CONF_SHOW_ON_MAP]:
|
|
|
|
self._attrs[ATTR_LATITUDE] = geography[CONF_LATITUDE]
|
|
|
|
self._attrs[ATTR_LONGITUDE] = geography[CONF_LONGITUDE]
|
|
|
|
self._attrs.pop("lati", None)
|
|
|
|
self._attrs.pop("long", None)
|
|
|
|
else:
|
|
|
|
self._attrs["lati"] = geography[CONF_LATITUDE]
|
|
|
|
self._attrs["long"] = geography[CONF_LONGITUDE]
|
|
|
|
self._attrs.pop(ATTR_LATITUDE, None)
|
|
|
|
self._attrs.pop(ATTR_LONGITUDE, None)
|
|
|
|
|
2020-02-29 03:14:17 +00:00
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
|
|
|
"""Disconnect dispatcher listener when removed."""
|
|
|
|
for cancel in self._async_unsub_dispatcher_connects:
|
|
|
|
cancel()
|
|
|
|
self._async_unsub_dispatcher_connects = []
|