Migrate GIOS to use DataUpdateCoordinator (#33306)
* Migrate to DataUpdateCoordinator * Simplify code
This commit is contained in:
1 parent
f7ae78f78e
commit
de2f506585
3 files changed
+78
-86
No files matched your search
@@ -1,5 +1,4 @@
|
||||
"""The GIOS component."""
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from aiohttp.client_exceptions import ClientConnectorError
|
||||
@@ -7,18 +6,17 @@ from async_timeout import timeout
|
||||
from gios import ApiError, Gios, NoStationError
|
||||
|
||||
from homeassistant.core import Config, HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.util import Throttle
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import CONF_STATION_ID, DATA_CLIENT, DEFAULT_SCAN_INTERVAL, DOMAIN
|
||||
from .const import CONF_STATION_ID, DOMAIN, SCAN_INTERVAL
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: Config) -> bool:
|
||||
"""Set up configured GIOS."""
|
||||
hass.data[DOMAIN] = {}
|
||||
hass.data[DOMAIN][DATA_CLIENT] = {}
|
||||
return True
|
||||
|
||||
|
||||
@@ -29,11 +27,14 @@ async def async_setup_entry(hass, config_entry):
|
||||
|
||||
websession = async_get_clientsession(hass)
|
||||
|
||||
gios = GiosData(websession, station_id)
|
||||
coordinator = GiosDataUpdateCoordinator(hass, websession, station_id)
|
||||
await coordinator.async_refresh()
|
||||
|
||||
await gios.async_update()
|
||||
if not coordinator.last_update_success:
|
||||
raise ConfigEntryNotReady
|
||||
|
||||
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = gios
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][config_entry.entry_id] = coordinator
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(config_entry, "air_quality")
|
||||
@@ -43,36 +44,27 @@ async def async_setup_entry(hass, config_entry):
|
||||
|
||||
async def async_unload_entry(hass, config_entry):
|
||||
"""Unload a config entry."""
|
||||
hass.data[DOMAIN][DATA_CLIENT].pop(config_entry.entry_id)
|
||||
hass.data[DOMAIN].pop(config_entry.entry_id)
|
||||
await hass.config_entries.async_forward_entry_unload(config_entry, "air_quality")
|
||||
return True
|
||||
|
||||
|
||||
class GiosData:
|
||||
class GiosDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
"""Define an object to hold GIOS data."""
|
||||
|
||||
def __init__(self, session, station_id):
|
||||
"""Initialize."""
|
||||
self._gios = Gios(station_id, session)
|
||||
self.station_id = station_id
|
||||
self.sensors = {}
|
||||
self.latitude = None
|
||||
self.longitude = None
|
||||
self.station_name = None
|
||||
self.available = True
|
||||
def __init__(self, hass, session, station_id):
|
||||
"""Class to manage fetching GIOS data API."""
|
||||
self.gios = Gios(station_id, session)
|
||||
|
||||
@Throttle(DEFAULT_SCAN_INTERVAL)
|
||||
async def async_update(self):
|
||||
"""Update GIOS data."""
|
||||
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL)
|
||||
|
||||
async def _async_update_data(self):
|
||||
"""Update data via library."""
|
||||
try:
|
||||
with timeout(30):
|
||||
await self._gios.update()
|
||||
except asyncio.TimeoutError:
|
||||
_LOGGER.error("Asyncio Timeout Error")
|
||||
await self.gios.update()
|
||||
except (ApiError, NoStationError, ClientConnectorError) as error:
|
||||
_LOGGER.error("GIOS data update failed: %s", error)
|
||||
self.available = self._gios.available
|
||||
self.latitude = self._gios.latitude
|
||||
self.longitude = self._gios.longitude
|
||||
self.station_name = self._gios.station_name
|
||||
self.sensors = self._gios.data
|
||||
raise UpdateFailed(error)
|
||||
if not self.gios.data:
|
||||
raise UpdateFailed("Invalid sensors data")
|
||||
return self.gios.data
|
||||
Reference in new issue
Block a user