mirror of
https://github.com/home-assistant/core.git
synced 2026-09-27 01:46:11 -04:00
Simplify Airly data update coordinator (#181938)
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
"""The Airly integration."""
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from homeassistant.components.air_quality import DOMAIN as AIR_QUALITY_DOMAIN
|
||||
@@ -9,7 +8,7 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import CONF_USE_NEAREST, DOMAIN, MIN_UPDATE_INTERVAL
|
||||
from .const import CONF_USE_NEAREST, DOMAIN
|
||||
from .coordinator import AirlyConfigEntry, AirlyDataUpdateCoordinator
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
@@ -55,8 +54,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: AirlyConfigEntry) -> boo
|
||||
|
||||
websession = async_get_clientsession(hass)
|
||||
|
||||
update_interval = timedelta(minutes=MIN_UPDATE_INTERVAL)
|
||||
|
||||
coordinator = AirlyDataUpdateCoordinator(
|
||||
hass,
|
||||
entry,
|
||||
@@ -64,7 +61,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: AirlyConfigEntry) -> boo
|
||||
api_key,
|
||||
latitude,
|
||||
longitude,
|
||||
update_interval,
|
||||
use_nearest,
|
||||
)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
@@ -73,7 +73,6 @@ class AirlyDataUpdateCoordinator(DataUpdateCoordinator[dict[str, str | float | i
|
||||
api_key: str,
|
||||
latitude: float,
|
||||
longitude: float,
|
||||
update_interval: timedelta,
|
||||
use_nearest: bool,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
@@ -82,31 +81,31 @@ class AirlyDataUpdateCoordinator(DataUpdateCoordinator[dict[str, str | float | i
|
||||
# Currently, Airly only supports Polish and English
|
||||
language = "pl" if hass.config.language == "pl" else "en"
|
||||
self.airly = Airly(api_key, session, language=language)
|
||||
self.use_nearest = use_nearest
|
||||
|
||||
if use_nearest:
|
||||
self.measurements = self.airly.create_measurements_session_nearest(
|
||||
latitude, longitude, max_distance_km=5
|
||||
)
|
||||
else:
|
||||
self.measurements = self.airly.create_measurements_session_point(
|
||||
latitude, longitude
|
||||
)
|
||||
|
||||
super().__init__(
|
||||
hass,
|
||||
_LOGGER,
|
||||
config_entry=config_entry,
|
||||
name=DOMAIN,
|
||||
update_interval=update_interval,
|
||||
update_interval=timedelta(minutes=MIN_UPDATE_INTERVAL),
|
||||
)
|
||||
|
||||
@override
|
||||
async def _async_update_data(self) -> dict[str, str | float | int]:
|
||||
"""Update data via library."""
|
||||
data: dict[str, str | float | int] = {}
|
||||
if self.use_nearest:
|
||||
measurements = self.airly.create_measurements_session_nearest(
|
||||
self.latitude, self.longitude, max_distance_km=5
|
||||
)
|
||||
else:
|
||||
measurements = self.airly.create_measurements_session_point(
|
||||
self.latitude, self.longitude
|
||||
)
|
||||
try:
|
||||
async with timeout(DEFAULT_TIMEOUT):
|
||||
await measurements.update()
|
||||
await self.measurements.update()
|
||||
except (AirlyError, ClientConnectorError, TimeoutError) as error:
|
||||
raise UpdateFailed(
|
||||
translation_domain=DOMAIN,
|
||||
@@ -131,9 +130,9 @@ class AirlyDataUpdateCoordinator(DataUpdateCoordinator[dict[str, str | float | i
|
||||
self.airly.requests_remaining,
|
||||
)
|
||||
|
||||
values = measurements.current["values"]
|
||||
index = measurements.current["indexes"][0]
|
||||
standards = measurements.current["standards"]
|
||||
values = self.measurements.current["values"]
|
||||
index = self.measurements.current["indexes"][0]
|
||||
standards = self.measurements.current["standards"]
|
||||
|
||||
if index["description"] == NO_AIRLY_SENSORS:
|
||||
raise UpdateFailed(
|
||||
|
||||
Reference in New Issue
Block a user