2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Luftdaten stations."""
|
2021-09-03 20:34:01 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-11-06 13:27:52 +00:00
|
|
|
import logging
|
|
|
|
|
2019-10-19 16:22:32 +00:00
|
|
|
from luftdaten import Luftdaten
|
|
|
|
from luftdaten.exceptions import LuftdatenError
|
2018-11-06 13:27:52 +00:00
|
|
|
|
2021-12-14 04:39:19 +00:00
|
|
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntityDescription
|
2018-11-06 13:27:52 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT
|
|
|
|
from homeassistant.const import (
|
2020-02-25 01:52:14 +00:00
|
|
|
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_MONITORED_CONDITIONS,
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
CONF_SENSORS,
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2021-11-15 08:51:57 +00:00
|
|
|
PRESSURE_PA,
|
2019-07-31 19:25:30 +00:00
|
|
|
TEMP_CELSIUS,
|
2021-12-06 03:10:07 +00:00
|
|
|
Platform,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-01-31 01:12:59 +00:00
|
|
|
from homeassistant.core import callback
|
2018-11-06 13:27:52 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
|
|
from homeassistant.helpers.event import async_track_time_interval
|
|
|
|
|
2021-12-14 08:21:12 +00:00
|
|
|
from .config_flow import duplicate_stations
|
2018-11-06 13:27:52 +00:00
|
|
|
from .const import CONF_SENSOR_ID, DEFAULT_SCAN_INTERVAL, DOMAIN
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_LUFTDATEN = "luftdaten"
|
|
|
|
DATA_LUFTDATEN_CLIENT = "data_luftdaten_client"
|
|
|
|
DATA_LUFTDATEN_LISTENER = "data_luftdaten_listener"
|
2018-11-06 13:27:52 +00:00
|
|
|
|
2021-12-06 03:10:07 +00:00
|
|
|
PLATFORMS = [Platform.SENSOR]
|
2021-04-27 16:49:13 +00:00
|
|
|
|
2019-09-03 19:14:00 +00:00
|
|
|
TOPIC_UPDATE = f"{DOMAIN}_data_update"
|
2018-11-06 13:27:52 +00:00
|
|
|
|
2021-09-03 20:34:01 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
2021-12-16 20:25:24 +00:00
|
|
|
key="temperature",
|
2021-09-03 20:34:01 +00:00
|
|
|
name="Temperature",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
2021-12-14 04:39:19 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2021-09-03 20:34:01 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
2021-12-16 20:25:24 +00:00
|
|
|
key="humidity",
|
2021-09-03 20:34:01 +00:00
|
|
|
name="Humidity",
|
|
|
|
icon="mdi:water-percent",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
2021-12-14 04:39:19 +00:00
|
|
|
device_class=SensorDeviceClass.HUMIDITY,
|
2021-09-03 20:34:01 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
2021-12-16 20:25:24 +00:00
|
|
|
key="pressure",
|
2021-09-03 20:34:01 +00:00
|
|
|
name="Pressure",
|
|
|
|
icon="mdi:arrow-down-bold",
|
2021-11-15 08:51:57 +00:00
|
|
|
native_unit_of_measurement=PRESSURE_PA,
|
2021-12-14 04:39:19 +00:00
|
|
|
device_class=SensorDeviceClass.PRESSURE,
|
2021-09-03 20:34:01 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
2021-12-16 20:25:24 +00:00
|
|
|
key="pressure_at_sealevel",
|
2021-09-03 20:34:01 +00:00
|
|
|
name="Pressure at sealevel",
|
|
|
|
icon="mdi:download",
|
2021-11-15 08:51:57 +00:00
|
|
|
native_unit_of_measurement=PRESSURE_PA,
|
2021-12-14 04:39:19 +00:00
|
|
|
device_class=SensorDeviceClass.PRESSURE,
|
2021-09-03 20:34:01 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
2021-12-16 20:25:24 +00:00
|
|
|
key="P1",
|
2021-09-03 20:34:01 +00:00
|
|
|
name="PM10",
|
|
|
|
icon="mdi:thought-bubble",
|
|
|
|
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
2021-12-16 20:25:24 +00:00
|
|
|
key="P2",
|
2021-09-03 20:34:01 +00:00
|
|
|
name="PM2.5",
|
|
|
|
icon="mdi:thought-bubble-outline",
|
|
|
|
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
2018-11-06 13:27:52 +00:00
|
|
|
|
2021-12-14 08:21:12 +00:00
|
|
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
2018-11-06 13:27:52 +00:00
|
|
|
|
|
|
|
|
2019-01-31 01:12:59 +00:00
|
|
|
@callback
|
|
|
|
def _async_fixup_sensor_id(hass, config_entry, sensor_id):
|
|
|
|
hass.config_entries.async_update_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
config_entry, data={**config_entry.data, CONF_SENSOR_ID: int(sensor_id)}
|
|
|
|
)
|
2019-01-31 01:12:59 +00:00
|
|
|
|
|
|
|
|
2018-11-06 13:27:52 +00:00
|
|
|
async def async_setup_entry(hass, config_entry):
|
|
|
|
"""Set up Luftdaten as config entry."""
|
2021-12-14 08:21:12 +00:00
|
|
|
hass.data.setdefault(
|
|
|
|
DOMAIN,
|
|
|
|
{
|
|
|
|
DATA_LUFTDATEN_CLIENT: {},
|
|
|
|
DATA_LUFTDATEN_LISTENER: {},
|
|
|
|
},
|
|
|
|
)
|
2018-11-06 13:27:52 +00:00
|
|
|
|
2019-01-31 01:12:59 +00:00
|
|
|
if not isinstance(config_entry.data[CONF_SENSOR_ID], int):
|
2019-07-31 19:25:30 +00:00
|
|
|
_async_fixup_sensor_id(hass, config_entry, config_entry.data[CONF_SENSOR_ID])
|
|
|
|
|
|
|
|
if (
|
|
|
|
config_entry.data[CONF_SENSOR_ID] in duplicate_stations(hass)
|
|
|
|
and config_entry.source == SOURCE_IMPORT
|
|
|
|
):
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Removing duplicate sensors for station %s",
|
|
|
|
config_entry.data[CONF_SENSOR_ID],
|
|
|
|
)
|
|
|
|
hass.async_create_task(hass.config_entries.async_remove(config_entry.entry_id))
|
2019-01-31 01:12:59 +00:00
|
|
|
return False
|
|
|
|
|
2018-11-06 13:27:52 +00:00
|
|
|
try:
|
|
|
|
luftdaten = LuftDatenData(
|
2021-12-04 08:16:00 +00:00
|
|
|
Luftdaten(config_entry.data[CONF_SENSOR_ID]),
|
2018-11-06 13:27:52 +00:00
|
|
|
config_entry.data.get(CONF_SENSORS, {}).get(
|
2021-09-03 20:34:01 +00:00
|
|
|
CONF_MONITORED_CONDITIONS, SENSOR_KEYS
|
2019-07-31 19:25:30 +00:00
|
|
|
),
|
|
|
|
)
|
2018-11-06 13:27:52 +00:00
|
|
|
await luftdaten.async_update()
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.data[DOMAIN][DATA_LUFTDATEN_CLIENT][config_entry.entry_id] = luftdaten
|
2020-08-28 11:50:32 +00:00
|
|
|
except LuftdatenError as err:
|
|
|
|
raise ConfigEntryNotReady from err
|
2018-11-06 13:27:52 +00:00
|
|
|
|
2021-04-27 16:49:13 +00:00
|
|
|
hass.config_entries.async_setup_platforms(config_entry, PLATFORMS)
|
2018-11-06 13:27:52 +00:00
|
|
|
|
|
|
|
async def refresh_sensors(event_time):
|
|
|
|
"""Refresh Luftdaten data."""
|
|
|
|
await luftdaten.async_update()
|
|
|
|
async_dispatcher_send(hass, TOPIC_UPDATE)
|
|
|
|
|
|
|
|
hass.data[DOMAIN][DATA_LUFTDATEN_LISTENER][
|
2019-07-31 19:25:30 +00:00
|
|
|
config_entry.entry_id
|
|
|
|
] = async_track_time_interval(
|
|
|
|
hass,
|
|
|
|
refresh_sensors,
|
|
|
|
hass.data[DOMAIN].get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL),
|
|
|
|
)
|
2018-11-06 13:27:52 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, config_entry):
|
|
|
|
"""Unload an Luftdaten config entry."""
|
|
|
|
remove_listener = hass.data[DOMAIN][DATA_LUFTDATEN_LISTENER].pop(
|
2019-07-31 19:25:30 +00:00
|
|
|
config_entry.entry_id
|
|
|
|
)
|
2018-11-06 13:27:52 +00:00
|
|
|
remove_listener()
|
|
|
|
|
|
|
|
hass.data[DOMAIN][DATA_LUFTDATEN_CLIENT].pop(config_entry.entry_id)
|
|
|
|
|
2021-04-27 16:49:13 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
|
2018-11-06 13:27:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LuftDatenData:
|
|
|
|
"""Define a generic Luftdaten object."""
|
|
|
|
|
|
|
|
def __init__(self, client, sensor_conditions):
|
|
|
|
"""Initialize the Luftdata object."""
|
|
|
|
self.client = client
|
|
|
|
self.data = {}
|
|
|
|
self.sensor_conditions = sensor_conditions
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Update sensor/binary sensor data."""
|
|
|
|
try:
|
|
|
|
await self.client.get_data()
|
|
|
|
|
2020-11-21 10:52:34 +00:00
|
|
|
if self.client.values:
|
|
|
|
self.data[DATA_LUFTDATEN] = self.client.values
|
|
|
|
self.data[DATA_LUFTDATEN].update(self.client.meta)
|
2018-11-06 13:27:52 +00:00
|
|
|
|
|
|
|
except LuftdatenError:
|
|
|
|
_LOGGER.error("Unable to retrieve data from luftdaten.info")
|