diff --git a/homeassistant/components/gios/__init__.py b/homeassistant/components/gios/__init__.py index 6c49ddd9020..a9435f02401 100644 --- a/homeassistant/components/gios/__init__.py +++ b/homeassistant/components/gios/__init__.py @@ -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 diff --git a/homeassistant/components/gios/coordinator.py b/homeassistant/components/gios/coordinator.py new file mode 100644 index 00000000000..17b4b89174f --- /dev/null +++ b/homeassistant/components/gios/coordinator.py @@ -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 diff --git a/homeassistant/components/gios/sensor.py b/homeassistant/components/gios/sensor.py index 244e741a086..69e198d34df 100644 --- a/homeassistant/components/gios/sensor.py +++ b/homeassistant/components/gios/sensor.py @@ -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__) diff --git a/tests/components/gios/__init__.py b/tests/components/gios/__init__.py index d5c43c8acc0..435b3209199 100644 --- a/tests/components/gios/__init__.py +++ b/tests/components/gios/__init__.py @@ -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, ), ): diff --git a/tests/components/gios/test_config_flow.py b/tests/components/gios/test_config_flow.py index a96b065574a..d81758b0de0 100644 --- a/tests/components/gios/test_config_flow.py +++ b/tests/components/gios/test_config_flow.py @@ -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")), ), ): diff --git a/tests/components/gios/test_init.py b/tests/components/gios/test_init.py index e5f3454bcd9..bf954d48548 100644 --- a/tests/components/gios/test_init.py +++ b/tests/components/gios/test_init.py @@ -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) diff --git a/tests/components/gios/test_sensor.py b/tests/components/gios/test_sensor.py index b24d88ccb8d..d9096916106 100644 --- a/tests/components/gios/test_sensor.py +++ b/tests/components/gios/test_sensor.py @@ -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, ), ):