mirror of
https://github.com/home-assistant/core.git
synced 2025-12-25 05:26:47 +00:00
Add Kaiterra integration (#26661)
* add Kaiterra integration * fix: split to multiple platforms * fix lint issues * fix formmating * fix: docstrings * fix: pylint issues * Apply suggestions from code review Co-Authored-By: Martin Hjelmare <marhje52@kth.se> * Adjust code based on suggestions * Update homeassistant/components/kaiterra/sensor.py Co-Authored-By: Martin Hjelmare <marhje52@kth.se>
This commit is contained in:
committed by
Martin Hjelmare
parent
f82f30dc62
commit
fbe85a2eb2
109
homeassistant/components/kaiterra/api_data.py
Normal file
109
homeassistant/components/kaiterra/api_data.py
Normal file
@@ -0,0 +1,109 @@
|
||||
"""Data for all Kaiterra devices."""
|
||||
from logging import getLogger
|
||||
|
||||
import asyncio
|
||||
|
||||
import async_timeout
|
||||
|
||||
from aiohttp.client_exceptions import ClientResponseError
|
||||
|
||||
from kaiterra_async_client import KaiterraAPIClient, AQIStandard, Units
|
||||
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
|
||||
from homeassistant.const import CONF_API_KEY, CONF_DEVICES, CONF_DEVICE_ID, CONF_TYPE
|
||||
|
||||
from .const import (
|
||||
AQI_SCALE,
|
||||
AQI_LEVEL,
|
||||
CONF_AQI_STANDARD,
|
||||
CONF_PREFERRED_UNITS,
|
||||
DISPATCHER_KAITERRA,
|
||||
)
|
||||
|
||||
_LOGGER = getLogger(__name__)
|
||||
|
||||
POLLUTANTS = {"rpm25c": "PM2.5", "rpm10c": "PM10", "rtvoc": "TVOC"}
|
||||
|
||||
|
||||
class KaiterraApiData:
|
||||
"""Get data from Kaiterra API."""
|
||||
|
||||
def __init__(self, hass, config, session):
|
||||
"""Initialize the API data object."""
|
||||
|
||||
api_key = config[CONF_API_KEY]
|
||||
aqi_standard = config[CONF_AQI_STANDARD]
|
||||
devices = config[CONF_DEVICES]
|
||||
units = config[CONF_PREFERRED_UNITS]
|
||||
|
||||
self._hass = hass
|
||||
self._api = KaiterraAPIClient(
|
||||
session,
|
||||
api_key=api_key,
|
||||
aqi_standard=AQIStandard.from_str(aqi_standard),
|
||||
preferred_units=[Units.from_str(unit) for unit in units],
|
||||
)
|
||||
self._devices_ids = [device[CONF_DEVICE_ID] for device in devices]
|
||||
self._devices = [
|
||||
f"/{device[CONF_TYPE]}s/{device[CONF_DEVICE_ID]}" for device in devices
|
||||
]
|
||||
self._scale = AQI_SCALE[aqi_standard]
|
||||
self._level = AQI_LEVEL[aqi_standard]
|
||||
self._update_listeners = []
|
||||
self.data = {}
|
||||
|
||||
async def async_update(self) -> None:
|
||||
"""Get the data from Kaiterra API."""
|
||||
|
||||
try:
|
||||
with async_timeout.timeout(10):
|
||||
data = await self._api.get_latest_sensor_readings(self._devices)
|
||||
except (ClientResponseError, asyncio.TimeoutError):
|
||||
_LOGGER.debug("Couldn't fetch data")
|
||||
self.data = {}
|
||||
async_dispatcher_send(self._hass, DISPATCHER_KAITERRA)
|
||||
|
||||
_LOGGER.debug("New data retrieved: %s", data)
|
||||
|
||||
try:
|
||||
self.data = {}
|
||||
for i, device in enumerate(data):
|
||||
if not device:
|
||||
self.data[self._devices_ids[i]] = {}
|
||||
continue
|
||||
|
||||
aqi, main_pollutant = None, None
|
||||
for sensor_name, sensor in device.items():
|
||||
points = sensor.get("points")
|
||||
|
||||
if not points:
|
||||
continue
|
||||
|
||||
point = points[0]
|
||||
sensor["value"] = point.get("value")
|
||||
|
||||
if "aqi" not in point:
|
||||
continue
|
||||
|
||||
sensor["aqi"] = point["aqi"]
|
||||
if not aqi or aqi < point["aqi"]:
|
||||
aqi = point["aqi"]
|
||||
main_pollutant = POLLUTANTS.get(sensor_name)
|
||||
|
||||
level = None
|
||||
for j in range(1, len(self._scale)):
|
||||
if aqi <= self._scale[j]:
|
||||
level = self._level[j - 1]
|
||||
break
|
||||
|
||||
device["aqi"] = {"value": aqi}
|
||||
device["aqi_level"] = {"value": level}
|
||||
device["aqi_pollutant"] = {"value": main_pollutant}
|
||||
|
||||
self.data[self._devices_ids[i]] = device
|
||||
|
||||
async_dispatcher_send(self._hass, DISPATCHER_KAITERRA)
|
||||
except IndexError as err:
|
||||
_LOGGER.error("Parsing error %s", err)
|
||||
async_dispatcher_send(self._hass, DISPATCHER_KAITERRA)
|
||||
Reference in New Issue
Block a user