Move gios coordinator to separate module (#117471)
This commit is contained in:
parent
65e6e1fa28
commit
48c03a6564
7 changed files with 75 additions and 53 deletions
|
@ -2,25 +2,18 @@
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
import logging
|
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.components.air_quality import DOMAIN as AIR_QUALITY_PLATFORM
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
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__)
|
_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:
|
async def async_unload_entry(hass: HomeAssistant, entry: GiosConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
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
|
|
||||||
|
|
39
homeassistant/components/gios/coordinator.py
Normal file
39
homeassistant/components/gios/coordinator.py
Normal 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
|
|
@ -23,7 +23,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import GiosConfigEntry, GiosDataUpdateCoordinator
|
from . import GiosConfigEntry
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_AQI,
|
ATTR_AQI,
|
||||||
ATTR_C6H6,
|
ATTR_C6H6,
|
||||||
|
@ -38,6 +38,7 @@ from .const import (
|
||||||
MANUFACTURER,
|
MANUFACTURER,
|
||||||
URL,
|
URL,
|
||||||
)
|
)
|
||||||
|
from .coordinator import GiosDataUpdateCoordinator
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
|
@ -37,18 +37,19 @@ async def init_integration(
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_stations", return_value=STATIONS
|
"homeassistant.components.gios.coordinator.Gios._get_stations",
|
||||||
|
return_value=STATIONS,
|
||||||
),
|
),
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_station",
|
"homeassistant.components.gios.coordinator.Gios._get_station",
|
||||||
return_value=station,
|
return_value=station,
|
||||||
),
|
),
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_all_sensors",
|
"homeassistant.components.gios.coordinator.Gios._get_all_sensors",
|
||||||
return_value=sensors,
|
return_value=sensors,
|
||||||
),
|
),
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_indexes",
|
"homeassistant.components.gios.coordinator.Gios._get_indexes",
|
||||||
return_value=indexes,
|
return_value=indexes,
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
|
|
|
@ -35,7 +35,8 @@ async def test_show_form(hass: HomeAssistant) -> None:
|
||||||
async def test_invalid_station_id(hass: HomeAssistant) -> None:
|
async def test_invalid_station_id(hass: HomeAssistant) -> None:
|
||||||
"""Test that errors are shown when measuring station ID is invalid."""
|
"""Test that errors are shown when measuring station ID is invalid."""
|
||||||
with patch(
|
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 = config_flow.GiosFlowHandler()
|
||||||
flow.hass = hass
|
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."""
|
"""Test that errors are shown when sensor data is invalid."""
|
||||||
with (
|
with (
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_stations", return_value=STATIONS
|
"homeassistant.components.gios.coordinator.Gios._get_stations",
|
||||||
|
return_value=STATIONS,
|
||||||
),
|
),
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_station",
|
"homeassistant.components.gios.coordinator.Gios._get_station",
|
||||||
return_value=json.loads(load_fixture("gios/station.json")),
|
return_value=json.loads(load_fixture("gios/station.json")),
|
||||||
),
|
),
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_sensor",
|
"homeassistant.components.gios.coordinator.Gios._get_sensor",
|
||||||
return_value={},
|
return_value={},
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
|
@ -75,7 +77,8 @@ async def test_invalid_sensor_data(hass: HomeAssistant) -> None:
|
||||||
async def test_cannot_connect(hass: HomeAssistant) -> None:
|
async def test_cannot_connect(hass: HomeAssistant) -> None:
|
||||||
"""Test that errors are shown when cannot connect to GIOS server."""
|
"""Test that errors are shown when cannot connect to GIOS server."""
|
||||||
with patch(
|
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 = config_flow.GiosFlowHandler()
|
||||||
flow.hass = hass
|
flow.hass = hass
|
||||||
|
@ -90,19 +93,19 @@ async def test_create_entry(hass: HomeAssistant) -> None:
|
||||||
"""Test that the user step works."""
|
"""Test that the user step works."""
|
||||||
with (
|
with (
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_stations",
|
"homeassistant.components.gios.coordinator.Gios._get_stations",
|
||||||
return_value=STATIONS,
|
return_value=STATIONS,
|
||||||
),
|
),
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_station",
|
"homeassistant.components.gios.coordinator.Gios._get_station",
|
||||||
return_value=json.loads(load_fixture("gios/station.json")),
|
return_value=json.loads(load_fixture("gios/station.json")),
|
||||||
),
|
),
|
||||||
patch(
|
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")),
|
return_value=json.loads(load_fixture("gios/sensors.json")),
|
||||||
),
|
),
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_indexes",
|
"homeassistant.components.gios.coordinator.Gios._get_indexes",
|
||||||
return_value=json.loads(load_fixture("gios/indexes.json")),
|
return_value=json.loads(load_fixture("gios/indexes.json")),
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
|
|
|
@ -35,7 +35,7 @@ async def test_config_not_ready(hass: HomeAssistant) -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.gios.Gios._get_stations",
|
"homeassistant.components.gios.coordinator.Gios._get_stations",
|
||||||
side_effect=ConnectionError(),
|
side_effect=ConnectionError(),
|
||||||
):
|
):
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
@ -77,17 +77,21 @@ async def test_migrate_device_and_config_entry(
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_stations", return_value=STATIONS
|
"homeassistant.components.gios.coordinator.Gios._get_stations",
|
||||||
|
return_value=STATIONS,
|
||||||
),
|
),
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_station",
|
"homeassistant.components.gios.coordinator.Gios._get_station",
|
||||||
return_value=station,
|
return_value=station,
|
||||||
),
|
),
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_all_sensors",
|
"homeassistant.components.gios.coordinator.Gios._get_all_sensors",
|
||||||
return_value=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)
|
config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ async def test_availability(hass: HomeAssistant) -> None:
|
||||||
|
|
||||||
future = utcnow() + timedelta(minutes=60)
|
future = utcnow() + timedelta(minutes=60)
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.gios.Gios._get_all_sensors",
|
"homeassistant.components.gios.coordinator.Gios._get_all_sensors",
|
||||||
side_effect=ApiError("Unexpected error"),
|
side_effect=ApiError("Unexpected error"),
|
||||||
):
|
):
|
||||||
async_fire_time_changed(hass, future)
|
async_fire_time_changed(hass, future)
|
||||||
|
@ -74,11 +74,11 @@ async def test_availability(hass: HomeAssistant) -> None:
|
||||||
future = utcnow() + timedelta(minutes=120)
|
future = utcnow() + timedelta(minutes=120)
|
||||||
with (
|
with (
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_all_sensors",
|
"homeassistant.components.gios.coordinator.Gios._get_all_sensors",
|
||||||
return_value=incomplete_sensors,
|
return_value=incomplete_sensors,
|
||||||
),
|
),
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_indexes",
|
"homeassistant.components.gios.coordinator.Gios._get_indexes",
|
||||||
return_value={},
|
return_value={},
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
|
@ -103,10 +103,11 @@ async def test_availability(hass: HomeAssistant) -> None:
|
||||||
future = utcnow() + timedelta(minutes=180)
|
future = utcnow() + timedelta(minutes=180)
|
||||||
with (
|
with (
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_all_sensors", return_value=sensors
|
"homeassistant.components.gios.coordinator.Gios._get_all_sensors",
|
||||||
|
return_value=sensors,
|
||||||
),
|
),
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.gios.Gios._get_indexes",
|
"homeassistant.components.gios.coordinator.Gios._get_indexes",
|
||||||
return_value=indexes,
|
return_value=indexes,
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
|
|
Loading…
Add table
Reference in a new issue