mirror of
https://github.com/home-assistant/core.git
synced 2026-04-18 07:56:03 +01:00
Use runtime_data in opensky integration (#167041)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,17 +6,16 @@ from aiohttp import BasicAuth
|
|||||||
from python_opensky import OpenSky
|
from python_opensky import OpenSky
|
||||||
from python_opensky.exceptions import OpenSkyError
|
from python_opensky.exceptions import OpenSkyError
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from .const import CONF_CONTRIBUTING_USER, DOMAIN, PLATFORMS
|
from .const import CONF_CONTRIBUTING_USER, PLATFORMS
|
||||||
from .coordinator import OpenSkyDataUpdateCoordinator
|
from .coordinator import OpenSkyConfigEntry, OpenSkyDataUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: OpenSkyConfigEntry) -> bool:
|
||||||
"""Set up opensky from a config entry."""
|
"""Set up opensky from a config entry."""
|
||||||
|
|
||||||
client = OpenSky(session=async_get_clientsession(hass))
|
client = OpenSky(session=async_get_clientsession(hass))
|
||||||
@@ -34,7 +33,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
coordinator = OpenSkyDataUpdateCoordinator(hass, entry, client)
|
coordinator = OpenSkyDataUpdateCoordinator(hass, entry, client)
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
entry.runtime_data = coordinator
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
entry.async_on_unload(entry.add_update_listener(update_listener))
|
entry.async_on_unload(entry.add_update_listener(update_listener))
|
||||||
@@ -42,12 +41,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: OpenSkyConfigEntry) -> bool:
|
||||||
"""Unload opensky config entry."""
|
"""Unload opensky config entry."""
|
||||||
|
|
||||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
|
|
||||||
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
async def update_listener(hass: HomeAssistant, entry: OpenSkyConfigEntry) -> None:
|
||||||
"""Handle options update."""
|
"""Handle options update."""
|
||||||
await hass.config_entries.async_reload(entry.entry_id)
|
await hass.config_entries.async_reload(entry.entry_id)
|
||||||
|
|||||||
@@ -9,12 +9,7 @@ from python_opensky import OpenSky
|
|||||||
from python_opensky.exceptions import OpenSkyUnauthenticatedError
|
from python_opensky.exceptions import OpenSkyUnauthenticatedError
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import (
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow
|
||||||
ConfigEntry,
|
|
||||||
ConfigFlow,
|
|
||||||
ConfigFlowResult,
|
|
||||||
OptionsFlow,
|
|
||||||
)
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_LATITUDE,
|
CONF_LATITUDE,
|
||||||
CONF_LONGITUDE,
|
CONF_LONGITUDE,
|
||||||
@@ -33,6 +28,7 @@ from .const import (
|
|||||||
DEFAULT_NAME,
|
DEFAULT_NAME,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
|
from .coordinator import OpenSkyConfigEntry
|
||||||
|
|
||||||
|
|
||||||
class OpenSkyConfigFlowHandler(ConfigFlow, domain=DOMAIN):
|
class OpenSkyConfigFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
@@ -41,7 +37,7 @@ class OpenSkyConfigFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@callback
|
||||||
def async_get_options_flow(
|
def async_get_options_flow(
|
||||||
config_entry: ConfigEntry,
|
config_entry: OpenSkyConfigEntry,
|
||||||
) -> OpenSkyOptionsFlowHandler:
|
) -> OpenSkyOptionsFlowHandler:
|
||||||
"""Get the options flow for this handler."""
|
"""Get the options flow for this handler."""
|
||||||
return OpenSkyOptionsFlowHandler()
|
return OpenSkyOptionsFlowHandler()
|
||||||
|
|||||||
@@ -30,14 +30,16 @@ from .const import (
|
|||||||
LOGGER,
|
LOGGER,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type OpenSkyConfigEntry = ConfigEntry[OpenSkyDataUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
class OpenSkyDataUpdateCoordinator(DataUpdateCoordinator[int]):
|
class OpenSkyDataUpdateCoordinator(DataUpdateCoordinator[int]):
|
||||||
"""An OpenSky Data Update Coordinator."""
|
"""An OpenSky Data Update Coordinator."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: OpenSkyConfigEntry
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, hass: HomeAssistant, config_entry: ConfigEntry, opensky: OpenSky
|
self, hass: HomeAssistant, config_entry: OpenSkyConfigEntry, opensky: OpenSky
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the OpenSky data coordinator."""
|
"""Initialize the OpenSky data coordinator."""
|
||||||
super().__init__(
|
super().__init__(
|
||||||
|
|||||||
@@ -10,17 +10,17 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN, MANUFACTURER
|
from .const import DOMAIN, MANUFACTURER
|
||||||
from .coordinator import OpenSkyDataUpdateCoordinator
|
from .coordinator import OpenSkyConfigEntry, OpenSkyDataUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: OpenSkyConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the entries."""
|
"""Initialize the entries."""
|
||||||
|
|
||||||
coordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator = entry.runtime_data
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
OpenSkySensor(
|
OpenSkySensor(
|
||||||
|
|||||||
Reference in New Issue
Block a user