2019-03-30 17:49:59 +00:00
|
|
|
"""Support for Ampio Air Quality data."""
|
2021-05-17 09:14:47 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-03-30 17:49:59 +00:00
|
|
|
import logging
|
2021-05-17 09:14:47 +00:00
|
|
|
from typing import Final
|
2019-03-30 17:49:59 +00:00
|
|
|
|
2019-10-17 13:00:00 +00:00
|
|
|
from asmog import AmpioSmog
|
2019-03-30 17:49:59 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-05-17 09:14:47 +00:00
|
|
|
from homeassistant.components.air_quality import (
|
|
|
|
PLATFORM_SCHEMA as BASE_PLATFORM_SCHEMA,
|
|
|
|
AirQualityEntity,
|
|
|
|
)
|
2019-03-30 17:49:59 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2021-05-17 09:14:47 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-03-30 17:49:59 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-05-17 09:14:47 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2019-03-30 17:49:59 +00:00
|
|
|
from homeassistant.util import Throttle
|
|
|
|
|
2021-05-17 09:14:47 +00:00
|
|
|
from .const import ATTRIBUTION, CONF_STATION_ID, SCAN_INTERVAL
|
2019-03-30 17:49:59 +00:00
|
|
|
|
2021-05-17 09:14:47 +00:00
|
|
|
_LOGGER: Final = logging.getLogger(__name__)
|
2019-03-30 17:49:59 +00:00
|
|
|
|
2021-05-17 09:14:47 +00:00
|
|
|
PLATFORM_SCHEMA: Final = BASE_PLATFORM_SCHEMA.extend(
|
2019-07-31 19:25:30 +00:00
|
|
|
{vol.Required(CONF_STATION_ID): cv.string, vol.Optional(CONF_NAME): cv.string}
|
|
|
|
)
|
2019-03-30 17:49:59 +00:00
|
|
|
|
|
|
|
|
2021-05-17 09:14:47 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2019-03-30 17:49:59 +00:00
|
|
|
"""Set up the Ampio Smog air quality platform."""
|
|
|
|
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
station_id = config[CONF_STATION_ID]
|
|
|
|
|
|
|
|
session = async_get_clientsession(hass)
|
|
|
|
api = AmpioSmogMapData(AmpioSmog(station_id, hass.loop, session))
|
|
|
|
|
|
|
|
await api.async_update()
|
|
|
|
|
|
|
|
if not api.api.data:
|
|
|
|
_LOGGER.error("Station %s is not available", station_id)
|
|
|
|
return
|
|
|
|
|
|
|
|
async_add_entities([AmpioSmogQuality(api, station_id, name)], True)
|
|
|
|
|
|
|
|
|
|
|
|
class AmpioSmogQuality(AirQualityEntity):
|
|
|
|
"""Implementation of an Ampio Smog air quality entity."""
|
|
|
|
|
2021-05-17 09:14:47 +00:00
|
|
|
def __init__(
|
|
|
|
self, api: AmpioSmogMapData, station_id: str, name: str | None
|
|
|
|
) -> None:
|
2019-03-30 17:49:59 +00:00
|
|
|
"""Initialize the air quality entity."""
|
|
|
|
self._ampio = api
|
|
|
|
self._station_id = station_id
|
|
|
|
self._name = name or api.api.name
|
|
|
|
|
|
|
|
@property
|
2021-05-17 09:14:47 +00:00
|
|
|
def name(self) -> str:
|
2019-03-30 17:49:59 +00:00
|
|
|
"""Return the name of the air quality entity."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
2021-05-17 09:14:47 +00:00
|
|
|
def unique_id(self) -> str:
|
2019-03-30 17:49:59 +00:00
|
|
|
"""Return unique_name."""
|
2019-09-03 14:11:36 +00:00
|
|
|
return f"ampio_smog_{self._station_id}"
|
2019-03-30 17:49:59 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-17 09:14:47 +00:00
|
|
|
def particulate_matter_2_5(self) -> str | None:
|
2019-03-30 17:49:59 +00:00
|
|
|
"""Return the particulate matter 2.5 level."""
|
2021-05-17 09:14:47 +00:00
|
|
|
return self._ampio.api.pm2_5 # type: ignore[no-any-return]
|
2019-03-30 17:49:59 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-17 09:14:47 +00:00
|
|
|
def particulate_matter_10(self) -> str | None:
|
2019-03-30 17:49:59 +00:00
|
|
|
"""Return the particulate matter 10 level."""
|
2021-05-17 09:14:47 +00:00
|
|
|
return self._ampio.api.pm10 # type: ignore[no-any-return]
|
2019-03-30 17:49:59 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-17 09:14:47 +00:00
|
|
|
def attribution(self) -> str:
|
2019-03-30 17:49:59 +00:00
|
|
|
"""Return the attribution."""
|
|
|
|
return ATTRIBUTION
|
|
|
|
|
2021-05-17 09:14:47 +00:00
|
|
|
async def async_update(self) -> None:
|
2019-03-30 17:49:59 +00:00
|
|
|
"""Get the latest data from the AmpioMap API."""
|
|
|
|
await self._ampio.async_update()
|
|
|
|
|
|
|
|
|
|
|
|
class AmpioSmogMapData:
|
|
|
|
"""Get the latest data and update the states."""
|
|
|
|
|
2021-05-17 09:14:47 +00:00
|
|
|
def __init__(self, api: AmpioSmog) -> None:
|
2019-03-30 17:49:59 +00:00
|
|
|
"""Initialize the data object."""
|
|
|
|
self.api = api
|
|
|
|
|
|
|
|
@Throttle(SCAN_INTERVAL)
|
2021-05-17 09:14:47 +00:00
|
|
|
async def async_update(self) -> None:
|
2019-03-30 17:49:59 +00:00
|
|
|
"""Get the latest data from AmpioMap."""
|
|
|
|
await self.api.get_data()
|