Explicitly pass in the config_entry in imgw_pib coordinator (#138144)

explicitly pass in the config_entry in coordinator
This commit is contained in:
Michael 2025-02-09 20:50:05 +01:00 committed by GitHub
parent 6d2f8b1076
commit b533cd3107
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 17 deletions

View File

@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass
import logging import logging
from aiohttp import ClientError from aiohttp import ClientError
@ -10,7 +9,6 @@ from imgw_pib import ImgwPib
from imgw_pib.exceptions import ApiError from imgw_pib.exceptions import ApiError
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_PLATFORM from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_PLATFORM
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.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
@ -18,21 +16,12 @@ from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import CONF_STATION_ID, DOMAIN from .const import CONF_STATION_ID, DOMAIN
from .coordinator import ImgwPibDataUpdateCoordinator from .coordinator import ImgwPibConfigEntry, ImgwPibData, ImgwPibDataUpdateCoordinator
PLATFORMS: list[Platform] = [Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.SENSOR]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
type ImgwPibConfigEntry = ConfigEntry[ImgwPibData]
@dataclass
class ImgwPibData:
"""Data for the IMGW-PIB integration."""
coordinator: ImgwPibDataUpdateCoordinator
async def async_setup_entry(hass: HomeAssistant, entry: ImgwPibConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ImgwPibConfigEntry) -> bool:
"""Set up IMGW-PIB from a config entry.""" """Set up IMGW-PIB from a config entry."""
@ -51,7 +40,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ImgwPibConfigEntry) -> b
except (ClientError, TimeoutError, ApiError) as err: except (ClientError, TimeoutError, ApiError) as err:
raise ConfigEntryNotReady from err raise ConfigEntryNotReady from err
coordinator = ImgwPibDataUpdateCoordinator(hass, imgwpib, station_id) coordinator = ImgwPibDataUpdateCoordinator(hass, entry, imgwpib, station_id)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
# Remove binary_sensor entities for which the endpoint has been blocked by IMGW-PIB API # Remove binary_sensor entities for which the endpoint has been blocked by IMGW-PIB API

View File

@ -1,9 +1,13 @@
"""Data Update Coordinator for IMGW-PIB integration.""" """Data Update Coordinator for IMGW-PIB integration."""
from __future__ import annotations
from dataclasses import dataclass
import logging import logging
from imgw_pib import ApiError, HydrologicalData, ImgwPib from imgw_pib import ApiError, HydrologicalData, ImgwPib
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
@ -13,12 +17,25 @@ from .const import DOMAIN, UPDATE_INTERVAL
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@dataclass
class ImgwPibData:
"""Data for the IMGW-PIB integration."""
coordinator: ImgwPibDataUpdateCoordinator
type ImgwPibConfigEntry = ConfigEntry[ImgwPibData]
class ImgwPibDataUpdateCoordinator(DataUpdateCoordinator[HydrologicalData]): class ImgwPibDataUpdateCoordinator(DataUpdateCoordinator[HydrologicalData]):
"""Class to manage fetching IMGW-PIB data API.""" """Class to manage fetching IMGW-PIB data API."""
config_entry: ImgwPibConfigEntry
def __init__( def __init__(
self, self,
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ImgwPibConfigEntry,
imgwpib: ImgwPib, imgwpib: ImgwPib,
station_id: str, station_id: str,
) -> None: ) -> None:
@ -33,7 +50,13 @@ class ImgwPibDataUpdateCoordinator(DataUpdateCoordinator[HydrologicalData]):
configuration_url=f"https://hydro.imgw.pl/#/station/hydro/{station_id}", configuration_url=f"https://hydro.imgw.pl/#/station/hydro/{station_id}",
) )
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL) super().__init__(
hass,
_LOGGER,
config_entry=config_entry,
name=DOMAIN,
update_interval=UPDATE_INTERVAL,
)
async def _async_update_data(self) -> HydrologicalData: async def _async_update_data(self) -> HydrologicalData:
"""Update data via internal method.""" """Update data via internal method."""

View File

@ -7,7 +7,7 @@ from typing import Any
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from . import ImgwPibConfigEntry from .coordinator import ImgwPibConfigEntry
async def async_get_config_entry_diagnostics( async def async_get_config_entry_diagnostics(

View File

@ -20,9 +20,8 @@ from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from . import ImgwPibConfigEntry
from .const import DOMAIN from .const import DOMAIN
from .coordinator import ImgwPibDataUpdateCoordinator from .coordinator import ImgwPibConfigEntry, ImgwPibDataUpdateCoordinator
from .entity import ImgwPibEntity from .entity import ImgwPibEntity
PARALLEL_UPDATES = 1 PARALLEL_UPDATES = 1