Use runtime_data in github (#120362)

pull/120386/head
G Johansson 2024-06-25 08:02:57 +02:00 committed by GitHub
parent aa8427abe5
commit 59998bc48a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 16 deletions

View File

@ -19,10 +19,11 @@ from .coordinator import GitHubDataUpdateCoordinator
PLATFORMS: list[Platform] = [Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up GitHub from a config entry."""
hass.data.setdefault(DOMAIN, {})
type GithubConfigEntry = ConfigEntry[dict[str, GitHubDataUpdateCoordinator]]
async def async_setup_entry(hass: HomeAssistant, entry: GithubConfigEntry) -> bool:
"""Set up GitHub from a config entry."""
client = GitHubAPI(
token=entry.data[CONF_ACCESS_TOKEN],
session=async_get_clientsession(hass),
@ -31,6 +32,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
repositories: list[str] = entry.options[CONF_REPOSITORIES]
entry.runtime_data = {}
for repository in repositories:
coordinator = GitHubDataUpdateCoordinator(
hass=hass,
@ -43,7 +45,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if not entry.pref_disable_polling:
await coordinator.subscribe()
hass.data[DOMAIN][repository] = coordinator
entry.runtime_data[repository] = coordinator
async_cleanup_device_registry(hass=hass, entry=entry)
@ -81,15 +83,13 @@ def async_cleanup_device_registry(
break
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: GithubConfigEntry) -> bool:
"""Unload a config entry."""
repositories: dict[str, GitHubDataUpdateCoordinator] = hass.data[DOMAIN]
repositories = entry.runtime_data
for coordinator in repositories.values():
coordinator.unsubscribe()
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data.pop(DOMAIN)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:

View File

@ -14,9 +14,6 @@ from homeassistant.helpers.aiohttp_client import (
async_get_clientsession,
)
from .const import DOMAIN
from .coordinator import GitHubDataUpdateCoordinator
async def async_get_config_entry_diagnostics(
hass: HomeAssistant,
@ -37,7 +34,7 @@ async def async_get_config_entry_diagnostics(
else:
data["rate_limit"] = rate_limit_response.data.as_dict
repositories: dict[str, GitHubDataUpdateCoordinator] = hass.data[DOMAIN]
repositories = config_entry.runtime_data
data["repositories"] = {}
for repository, coordinator in repositories.items():

View File

@ -11,7 +11,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
@ -19,6 +18,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import GithubConfigEntry
from .const import DOMAIN
from .coordinator import GitHubDataUpdateCoordinator
@ -145,11 +145,11 @@ SENSOR_DESCRIPTIONS: tuple[GitHubSensorEntityDescription, ...] = (
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: GithubConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up GitHub sensor based on a config entry."""
repositories: dict[str, GitHubDataUpdateCoordinator] = hass.data[DOMAIN]
repositories = entry.runtime_data
async_add_entities(
(
GitHubSensorEntity(coordinator, description)