Explicitly pass in the config_entry in github coordinator (#137834)

explicitly pass in the config_entry in coordinator
This commit is contained in:
Michael 2025-02-08 13:17:25 +01:00 committed by GitHub
parent 0efdceef27
commit 13f6f045f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 15 additions and 11 deletions

View File

@ -4,7 +4,6 @@ from __future__ import annotations
from aiogithubapi import GitHubAPI from aiogithubapi import GitHubAPI
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ACCESS_TOKEN, Platform from homeassistant.const import CONF_ACCESS_TOKEN, Platform
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
@ -14,14 +13,11 @@ from homeassistant.helpers.aiohttp_client import (
) )
from .const import CONF_REPOSITORIES, DOMAIN, LOGGER from .const import CONF_REPOSITORIES, DOMAIN, LOGGER
from .coordinator import GitHubDataUpdateCoordinator from .coordinator import GithubConfigEntry, GitHubDataUpdateCoordinator
PLATFORMS: list[Platform] = [Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.SENSOR]
type GithubConfigEntry = ConfigEntry[dict[str, GitHubDataUpdateCoordinator]]
async def async_setup_entry(hass: HomeAssistant, entry: GithubConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: GithubConfigEntry) -> bool:
"""Set up GitHub from a config entry.""" """Set up GitHub from a config entry."""
client = GitHubAPI( client = GitHubAPI(
@ -36,6 +32,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: GithubConfigEntry) -> bo
for repository in repositories: for repository in repositories:
coordinator = GitHubDataUpdateCoordinator( coordinator = GitHubDataUpdateCoordinator(
hass=hass, hass=hass,
config_entry=entry,
client=client, client=client,
repository=repository, repository=repository,
) )
@ -57,7 +54,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: GithubConfigEntry) -> bo
@callback @callback
def async_cleanup_device_registry( def async_cleanup_device_registry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: GithubConfigEntry,
) -> None: ) -> None:
"""Remove entries form device registry if we no longer track the repository.""" """Remove entries form device registry if we no longer track the repository."""
device_registry = dr.async_get(hass) device_registry = dr.async_get(hass)
@ -92,6 +89,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: GithubConfigEntry) -> b
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: async def async_reload_entry(hass: HomeAssistant, entry: GithubConfigEntry) -> None:
"""Handle an options update.""" """Handle an options update."""
await hass.config_entries.async_reload(entry.entry_id) await hass.config_entries.async_reload(entry.entry_id)

View File

@ -13,6 +13,7 @@ from aiogithubapi import (
GitHubResponseModel, GitHubResponseModel,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
@ -98,13 +99,18 @@ query ($owner: String!, $repository: String!) {
} }
""" """
type GithubConfigEntry = ConfigEntry[dict[str, GitHubDataUpdateCoordinator]]
class GitHubDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): class GitHubDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
"""Data update coordinator for the GitHub integration.""" """Data update coordinator for the GitHub integration."""
config_entry: GithubConfigEntry
def __init__( def __init__(
self, self,
hass: HomeAssistant, hass: HomeAssistant,
config_entry: GithubConfigEntry,
client: GitHubAPI, client: GitHubAPI,
repository: str, repository: str,
) -> None: ) -> None:
@ -118,6 +124,7 @@ class GitHubDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
super().__init__( super().__init__(
hass, hass,
LOGGER, LOGGER,
config_entry=config_entry,
name=repository, name=repository,
update_interval=FALLBACK_UPDATE_INTERVAL, update_interval=FALLBACK_UPDATE_INTERVAL,
) )

View File

@ -6,7 +6,6 @@ from typing import Any
from aiogithubapi import GitHubAPI, GitHubException from aiogithubapi import GitHubAPI, GitHubException
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ACCESS_TOKEN from homeassistant.const import CONF_ACCESS_TOKEN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import ( from homeassistant.helpers.aiohttp_client import (
@ -14,10 +13,12 @@ from homeassistant.helpers.aiohttp_client import (
async_get_clientsession, async_get_clientsession,
) )
from .coordinator import GithubConfigEntry
async def async_get_config_entry_diagnostics( async def async_get_config_entry_diagnostics(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: GithubConfigEntry,
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Return diagnostics for a config entry.""" """Return diagnostics for a config entry."""
data = {"options": {**config_entry.options}} data = {"options": {**config_entry.options}}

View File

@ -18,9 +18,8 @@ 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 GithubConfigEntry
from .const import DOMAIN from .const import DOMAIN
from .coordinator import GitHubDataUpdateCoordinator from .coordinator import GithubConfigEntry, GitHubDataUpdateCoordinator
@dataclass(frozen=True, kw_only=True) @dataclass(frozen=True, kw_only=True)