mirror of
https://github.com/home-assistant/core.git
synced 2025-11-08 18:39:30 +00:00
Add config entry for AirVisual (#32072)
* Add config entry for AirVisual * Update coverage * Catch invalid API key from config schema * Rename geographies to stations * Revert "Rename geographies to stations" This reverts commit 5477f89c24cb3f58965351985b1021fc5fc794a5. * Update strings * Update CONNECTION_CLASS * Remove options (subsequent PR) * Handle import step separately * Code review comments and simplification * Move default geography logic to config flow * Register domain in config flow init * Add tests * Update strings * Bump requirements * Update homeassistant/components/airvisual/config_flow.py * Update homeassistant/components/airvisual/config_flow.py * Make schemas stricter * Linting * Linting * Code review comments * Put config flow unique ID logic into a method * Fix tests * Streamline * Linting * show_on_map in options with default value * Code review comments * Default options * Update tests * Test update * Move config entry into data object (in prep for options flow) * Empty commit to re-trigger build
This commit is contained in:
@@ -1,30 +1,23 @@
|
||||
"""Support for AirVisual air quality sensors."""
|
||||
from datetime import timedelta
|
||||
from logging import getLogger
|
||||
|
||||
from pyairvisual import Client
|
||||
from pyairvisual.errors import AirVisualError
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.const import (
|
||||
ATTR_ATTRIBUTION,
|
||||
ATTR_LATITUDE,
|
||||
ATTR_LONGITUDE,
|
||||
ATTR_STATE,
|
||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||
CONCENTRATION_PARTS_PER_BILLION,
|
||||
CONCENTRATION_PARTS_PER_MILLION,
|
||||
CONF_API_KEY,
|
||||
CONF_LATITUDE,
|
||||
CONF_LONGITUDE,
|
||||
CONF_MONITORED_CONDITIONS,
|
||||
CONF_SCAN_INTERVAL,
|
||||
CONF_SHOW_ON_MAP,
|
||||
CONF_STATE,
|
||||
)
|
||||
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
from .const import CONF_CITY, CONF_COUNTRY, DATA_CLIENT, DOMAIN, TOPIC_UPDATE
|
||||
|
||||
_LOGGER = getLogger(__name__)
|
||||
|
||||
@@ -34,19 +27,19 @@ ATTR_POLLUTANT_SYMBOL = "pollutant_symbol"
|
||||
ATTR_POLLUTANT_UNIT = "pollutant_unit"
|
||||
ATTR_REGION = "region"
|
||||
|
||||
CONF_CITY = "city"
|
||||
CONF_COUNTRY = "country"
|
||||
|
||||
DEFAULT_ATTRIBUTION = "Data provided by AirVisual"
|
||||
DEFAULT_SCAN_INTERVAL = timedelta(minutes=10)
|
||||
|
||||
SENSOR_TYPE_LEVEL = "air_pollution_level"
|
||||
SENSOR_TYPE_AQI = "air_quality_index"
|
||||
SENSOR_TYPE_POLLUTANT = "main_pollutant"
|
||||
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"
|
||||
SENSORS = [
|
||||
(SENSOR_TYPE_LEVEL, "Air Pollution Level", "mdi:gauge", None),
|
||||
(SENSOR_TYPE_AQI, "Air Quality Index", "mdi:chart-line", "AQI"),
|
||||
(SENSOR_TYPE_POLLUTANT, "Main Pollutant", "mdi:chemical-weapon", None),
|
||||
(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),
|
||||
]
|
||||
|
||||
POLLUTANT_LEVEL_MAPPING = [
|
||||
@@ -79,102 +72,67 @@ POLLUTANT_MAPPING = {
|
||||
|
||||
SENSOR_LOCALES = {"cn": "Chinese", "us": "U.S."}
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_API_KEY): cv.string,
|
||||
vol.Required(CONF_MONITORED_CONDITIONS, default=list(SENSOR_LOCALES)): vol.All(
|
||||
cv.ensure_list, [vol.In(SENSOR_LOCALES)]
|
||||
),
|
||||
vol.Inclusive(CONF_CITY, "city"): cv.string,
|
||||
vol.Inclusive(CONF_COUNTRY, "city"): cv.string,
|
||||
vol.Inclusive(CONF_LATITUDE, "coords"): cv.latitude,
|
||||
vol.Inclusive(CONF_LONGITUDE, "coords"): cv.longitude,
|
||||
vol.Optional(CONF_SHOW_ON_MAP, default=True): cv.boolean,
|
||||
vol.Inclusive(CONF_STATE, "city"): cv.string,
|
||||
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL): cv.time_period,
|
||||
}
|
||||
)
|
||||
|
||||
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]
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Configure the platform and add the sensors."""
|
||||
|
||||
city = config.get(CONF_CITY)
|
||||
state = config.get(CONF_STATE)
|
||||
country = config.get(CONF_COUNTRY)
|
||||
|
||||
latitude = config.get(CONF_LATITUDE, hass.config.latitude)
|
||||
longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
|
||||
|
||||
websession = aiohttp_client.async_get_clientsession(hass)
|
||||
|
||||
if city and state and country:
|
||||
_LOGGER.debug(
|
||||
"Using city, state, and country: %s, %s, %s", city, state, country
|
||||
)
|
||||
location_id = ",".join((city, state, country))
|
||||
data = AirVisualData(
|
||||
Client(websession, api_key=config[CONF_API_KEY]),
|
||||
city=city,
|
||||
state=state,
|
||||
country=country,
|
||||
show_on_map=config[CONF_SHOW_ON_MAP],
|
||||
scan_interval=config[CONF_SCAN_INTERVAL],
|
||||
)
|
||||
else:
|
||||
_LOGGER.debug("Using latitude and longitude: %s, %s", latitude, longitude)
|
||||
location_id = ",".join((str(latitude), str(longitude)))
|
||||
data = AirVisualData(
|
||||
Client(websession, api_key=config[CONF_API_KEY]),
|
||||
latitude=latitude,
|
||||
longitude=longitude,
|
||||
show_on_map=config[CONF_SHOW_ON_MAP],
|
||||
scan_interval=config[CONF_SCAN_INTERVAL],
|
||||
)
|
||||
|
||||
await data.async_update()
|
||||
|
||||
sensors = []
|
||||
for locale in config[CONF_MONITORED_CONDITIONS]:
|
||||
for kind, name, icon, unit in SENSORS:
|
||||
sensors.append(
|
||||
AirVisualSensor(data, kind, name, icon, unit, locale, location_id)
|
||||
)
|
||||
|
||||
async_add_entities(sensors, True)
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
class AirVisualSensor(Entity):
|
||||
"""Define an AirVisual sensor."""
|
||||
|
||||
def __init__(self, airvisual, kind, name, icon, unit, locale, location_id):
|
||||
def __init__(self, airvisual, kind, name, icon, unit, locale, geography_id):
|
||||
"""Initialize."""
|
||||
self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
||||
self._airvisual = airvisual
|
||||
self._async_unsub_dispatcher_connects = []
|
||||
self._geography_id = geography_id
|
||||
self._icon = icon
|
||||
self._kind = kind
|
||||
self._locale = locale
|
||||
self._location_id = location_id
|
||||
self._name = name
|
||||
self._state = None
|
||||
self._type = kind
|
||||
self._unit = unit
|
||||
self.airvisual = airvisual
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the device state attributes."""
|
||||
if self.airvisual.show_on_map:
|
||||
self._attrs[ATTR_LATITUDE] = self.airvisual.latitude
|
||||
self._attrs[ATTR_LONGITUDE] = self.airvisual.longitude
|
||||
else:
|
||||
self._attrs["lati"] = self.airvisual.latitude
|
||||
self._attrs["long"] = self.airvisual.longitude
|
||||
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),
|
||||
}
|
||||
|
||||
return self._attrs
|
||||
geography = airvisual.geographies[geography_id]
|
||||
if geography.get(CONF_LATITUDE):
|
||||
if airvisual.show_on_map:
|
||||
self._attrs[ATTR_LATITUDE] = geography[CONF_LATITUDE]
|
||||
self._attrs[ATTR_LONGITUDE] = geography[CONF_LONGITUDE]
|
||||
else:
|
||||
self._attrs["lati"] = geography[CONF_LATITUDE]
|
||||
self._attrs["long"] = geography[CONF_LONGITUDE]
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return True if entity is available."""
|
||||
return bool(self.airvisual.pollution_info)
|
||||
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
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
@@ -194,22 +152,33 @@ class AirVisualSensor(Entity):
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique, Home Assistant friendly identifier for this entity."""
|
||||
return f"{self._location_id}_{self._locale}_{self._type}"
|
||||
return f"{self._geography_id}_{self._locale}_{self._kind}"
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit the value is expressed in."""
|
||||
return self._unit
|
||||
|
||||
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)
|
||||
)
|
||||
|
||||
async def async_update(self):
|
||||
"""Update the sensor."""
|
||||
await self.airvisual.async_update()
|
||||
data = self.airvisual.pollution_info
|
||||
|
||||
if not data:
|
||||
try:
|
||||
data = self._airvisual.data[self._geography_id]["current"]["pollution"]
|
||||
except KeyError:
|
||||
return
|
||||
|
||||
if self._type == SENSOR_TYPE_LEVEL:
|
||||
if self._kind == SENSOR_KIND_LEVEL:
|
||||
aqi = data[f"aqi{self._locale}"]
|
||||
[level] = [
|
||||
i
|
||||
@@ -218,9 +187,9 @@ class AirVisualSensor(Entity):
|
||||
]
|
||||
self._state = level["label"]
|
||||
self._icon = level["icon"]
|
||||
elif self._type == SENSOR_TYPE_AQI:
|
||||
elif self._kind == SENSOR_KIND_AQI:
|
||||
self._state = data[f"aqi{self._locale}"]
|
||||
elif self._type == SENSOR_TYPE_POLLUTANT:
|
||||
elif self._kind == SENSOR_KIND_POLLUTANT:
|
||||
symbol = data[f"main{self._locale}"]
|
||||
self._state = POLLUTANT_MAPPING[symbol]["label"]
|
||||
self._attrs.update(
|
||||
@@ -230,43 +199,8 @@ class AirVisualSensor(Entity):
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class AirVisualData:
|
||||
"""Define an object to hold sensor data."""
|
||||
|
||||
def __init__(self, client, **kwargs):
|
||||
"""Initialize."""
|
||||
self._client = client
|
||||
self.city = kwargs.get(CONF_CITY)
|
||||
self.country = kwargs.get(CONF_COUNTRY)
|
||||
self.latitude = kwargs.get(CONF_LATITUDE)
|
||||
self.longitude = kwargs.get(CONF_LONGITUDE)
|
||||
self.pollution_info = {}
|
||||
self.show_on_map = kwargs.get(CONF_SHOW_ON_MAP)
|
||||
self.state = kwargs.get(CONF_STATE)
|
||||
|
||||
self.async_update = Throttle(kwargs[CONF_SCAN_INTERVAL])(self._async_update)
|
||||
|
||||
async def _async_update(self):
|
||||
"""Update AirVisual data."""
|
||||
|
||||
try:
|
||||
if self.city and self.state and self.country:
|
||||
resp = await self._client.api.city(self.city, self.state, self.country)
|
||||
self.longitude, self.latitude = resp["location"]["coordinates"]
|
||||
else:
|
||||
resp = await self._client.api.nearest_city(
|
||||
self.latitude, self.longitude
|
||||
)
|
||||
|
||||
_LOGGER.debug("New data retrieved: %s", resp)
|
||||
|
||||
self.pollution_info = resp["current"]["pollution"]
|
||||
except (KeyError, AirVisualError) as err:
|
||||
if self.city and self.state and self.country:
|
||||
location = (self.city, self.state, self.country)
|
||||
else:
|
||||
location = (self.latitude, self.longitude)
|
||||
|
||||
_LOGGER.error("Can't retrieve data for location: %s (%s)", location, err)
|
||||
self.pollution_info = {}
|
||||
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 = []
|
||||
|
||||
Reference in New Issue
Block a user