Move gios coordinator to separate module (#117471)

This commit is contained in:
epenet 2024-05-15 11:26:04 +02:00 committed by GitHub
parent 65e6e1fa28
commit 48c03a6564
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 75 additions and 53 deletions

View File

@ -2,25 +2,18 @@
from __future__ import annotations
import asyncio
from dataclasses import dataclass
import logging
from aiohttp import ClientSession
from aiohttp.client_exceptions import ClientConnectorError
from gios import Gios
from gios.exceptions import GiosError
from gios.model import GiosSensors
from homeassistant.components.air_quality import DOMAIN as AIR_QUALITY_PLATFORM
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
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 homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import API_TIMEOUT, CONF_STATION_ID, DOMAIN, SCAN_INTERVAL
from .const import CONF_STATION_ID, DOMAIN
from .coordinator import GiosDataUpdateCoordinator
_LOGGER = logging.getLogger(__name__)
@ -77,23 +70,3 @@ async def async_setup_entry(hass: HomeAssistant, entry: GiosConfigEntry) -> bool
async def async_unload_entry(hass: HomeAssistant, entry: GiosConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
class GiosDataUpdateCoordinator(DataUpdateCoordinator[GiosSensors]): # pylint: disable=hass-enforce-coordinator-module
"""Define an object to hold GIOS data."""
def __init__(
self, hass: HomeAssistant, session: ClientSession, station_id: int
) -> None:
"""Class to manage fetching GIOS data API."""
self.gios = Gios(station_id, session)
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL)
async def _async_update_data(self) -> GiosSensors:
"""Update data via library."""
try:
async with asyncio.timeout(API_TIMEOUT):
return await self.gios.async_update()
except (GiosError, ClientConnectorError) as error:
raise UpdateFailed(error) from error

View File

@ -0,0 +1,39 @@
"""The GIOS component."""
from __future__ import annotations
import asyncio
import logging
from aiohttp import ClientSession
from aiohttp.client_exceptions import ClientConnectorError
from gios import Gios
from gios.exceptions import GiosError
from gios.model import GiosSensors
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import API_TIMEOUT, DOMAIN, SCAN_INTERVAL
_LOGGER = logging.getLogger(__name__)
class GiosDataUpdateCoordinator(DataUpdateCoordinator[GiosSensors]):
"""Define an object to hold GIOS data."""
def __init__(
self, hass: HomeAssistant, session: ClientSession, station_id: int
) -> None:
"""Class to manage fetching GIOS data API."""
self.gios = Gios(station_id, session)
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL)
async def _async_update_data(self) -> GiosSensors:
"""Update data via library."""
try:
async with asyncio.timeout(API_TIMEOUT):
return await self.gios.async_update()
except (GiosError, ClientConnectorError) as error:
raise UpdateFailed(error) from error

View File

@ -23,7 +23,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import GiosConfigEntry, GiosDataUpdateCoordinator
from . import GiosConfigEntry
from .const import (
ATTR_AQI,
ATTR_C6H6,
@ -38,6 +38,7 @@ from .const import (
MANUFACTURER,
URL,
)
from .coordinator import GiosDataUpdateCoordinator
_LOGGER = logging.getLogger(__name__)

View File

@ -37,18 +37,19 @@ async def init_integration(
with (
patch(
"homeassistant.components.gios.Gios._get_stations", return_value=STATIONS
"homeassistant.components.gios.coordinator.Gios._get_stations",
return_value=STATIONS,
),
patch(
"homeassistant.components.gios.Gios._get_station",
"homeassistant.components.gios.coordinator.Gios._get_station",
return_value=station,
),
patch(
"homeassistant.components.gios.Gios._get_all_sensors",
"homeassistant.components.gios.coordinator.Gios._get_all_sensors",
return_value=sensors,
),
patch(
"homeassistant.components.gios.Gios._get_indexes",
"homeassistant.components.gios.coordinator.Gios._get_indexes",
return_value=indexes,
),
):

View File

@ -35,7 +35,8 @@ async def test_show_form(hass: HomeAssistant) -> None:
async def test_invalid_station_id(hass: HomeAssistant) -> None:
"""Test that errors are shown when measuring station ID is invalid."""
with patch(
"homeassistant.components.gios.Gios._get_stations", return_value=STATIONS
"homeassistant.components.gios.coordinator.Gios._get_stations",
return_value=STATIONS,
):
flow = config_flow.GiosFlowHandler()
flow.hass = hass
@ -52,14 +53,15 @@ async def test_invalid_sensor_data(hass: HomeAssistant) -> None:
"""Test that errors are shown when sensor data is invalid."""
with (
patch(
"homeassistant.components.gios.Gios._get_stations", return_value=STATIONS
"homeassistant.components.gios.coordinator.Gios._get_stations",
return_value=STATIONS,
),
patch(
"homeassistant.components.gios.Gios._get_station",
"homeassistant.components.gios.coordinator.Gios._get_station",
return_value=json.loads(load_fixture("gios/station.json")),
),
patch(
"homeassistant.components.gios.Gios._get_sensor",
"homeassistant.components.gios.coordinator.Gios._get_sensor",
return_value={},
),
):
@ -75,7 +77,8 @@ async def test_invalid_sensor_data(hass: HomeAssistant) -> None:
async def test_cannot_connect(hass: HomeAssistant) -> None:
"""Test that errors are shown when cannot connect to GIOS server."""
with patch(
"homeassistant.components.gios.Gios._async_get", side_effect=ApiError("error")
"homeassistant.components.gios.coordinator.Gios._async_get",
side_effect=ApiError("error"),
):
flow = config_flow.GiosFlowHandler()
flow.hass = hass
@ -90,19 +93,19 @@ async def test_create_entry(hass: HomeAssistant) -> None:
"""Test that the user step works."""
with (
patch(
"homeassistant.components.gios.Gios._get_stations",
"homeassistant.components.gios.coordinator.Gios._get_stations",
return_value=STATIONS,
),
patch(
"homeassistant.components.gios.Gios._get_station",
"homeassistant.components.gios.coordinator.Gios._get_station",
return_value=json.loads(load_fixture("gios/station.json")),
),
patch(
"homeassistant.components.gios.Gios._get_all_sensors",
"homeassistant.components.gios.coordinator.Gios._get_all_sensors",
return_value=json.loads(load_fixture("gios/sensors.json")),
),
patch(
"homeassistant.components.gios.Gios._get_indexes",
"homeassistant.components.gios.coordinator.Gios._get_indexes",
return_value=json.loads(load_fixture("gios/indexes.json")),
),
):

View File

@ -35,7 +35,7 @@ async def test_config_not_ready(hass: HomeAssistant) -> None:
)
with patch(
"homeassistant.components.gios.Gios._get_stations",
"homeassistant.components.gios.coordinator.Gios._get_stations",
side_effect=ConnectionError(),
):
entry.add_to_hass(hass)
@ -77,17 +77,21 @@ async def test_migrate_device_and_config_entry(
with (
patch(
"homeassistant.components.gios.Gios._get_stations", return_value=STATIONS
"homeassistant.components.gios.coordinator.Gios._get_stations",
return_value=STATIONS,
),
patch(
"homeassistant.components.gios.Gios._get_station",
"homeassistant.components.gios.coordinator.Gios._get_station",
return_value=station,
),
patch(
"homeassistant.components.gios.Gios._get_all_sensors",
"homeassistant.components.gios.coordinator.Gios._get_all_sensors",
return_value=sensors,
),
patch("homeassistant.components.gios.Gios._get_indexes", return_value=indexes),
patch(
"homeassistant.components.gios.coordinator.Gios._get_indexes",
return_value=indexes,
),
):
config_entry.add_to_hass(hass)

View File

@ -51,7 +51,7 @@ async def test_availability(hass: HomeAssistant) -> None:
future = utcnow() + timedelta(minutes=60)
with patch(
"homeassistant.components.gios.Gios._get_all_sensors",
"homeassistant.components.gios.coordinator.Gios._get_all_sensors",
side_effect=ApiError("Unexpected error"),
):
async_fire_time_changed(hass, future)
@ -74,11 +74,11 @@ async def test_availability(hass: HomeAssistant) -> None:
future = utcnow() + timedelta(minutes=120)
with (
patch(
"homeassistant.components.gios.Gios._get_all_sensors",
"homeassistant.components.gios.coordinator.Gios._get_all_sensors",
return_value=incomplete_sensors,
),
patch(
"homeassistant.components.gios.Gios._get_indexes",
"homeassistant.components.gios.coordinator.Gios._get_indexes",
return_value={},
),
):
@ -103,10 +103,11 @@ async def test_availability(hass: HomeAssistant) -> None:
future = utcnow() + timedelta(minutes=180)
with (
patch(
"homeassistant.components.gios.Gios._get_all_sensors", return_value=sensors
"homeassistant.components.gios.coordinator.Gios._get_all_sensors",
return_value=sensors,
),
patch(
"homeassistant.components.gios.Gios._get_indexes",
"homeassistant.components.gios.coordinator.Gios._get_indexes",
return_value=indexes,
),
):