Store runtime data inside the config entry in Apple TV (#117032)

store runtime data inside the config entry
This commit is contained in:
Michael 2024-05-07 22:13:53 +02:00 committed by GitHub
parent 7f7d025b44
commit 9557ea902e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 19 deletions

View File

@ -73,8 +73,10 @@ DEVICE_EXCEPTIONS = (
exceptions.DeviceIdMissingError, exceptions.DeviceIdMissingError,
) )
AppleTvConfigEntry = ConfigEntry["AppleTVManager"]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: AppleTvConfigEntry) -> bool:
"""Set up a config entry for Apple TV.""" """Set up a config entry for Apple TV."""
manager = AppleTVManager(hass, entry) manager = AppleTVManager(hass, entry)
@ -95,7 +97,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
) )
raise ConfigEntryNotReady(f"{address}: {ex}") from ex raise ConfigEntryNotReady(f"{address}: {ex}") from ex
hass.data.setdefault(DOMAIN, {})[entry.unique_id] = manager entry.runtime_data = manager
async def on_hass_stop(event: Event) -> None: async def on_hass_stop(event: Event) -> None:
"""Stop push updates when hass stops.""" """Stop push updates when hass stops."""
@ -104,6 +106,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.async_on_unload( entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop)
) )
entry.async_on_unload(manager.disconnect)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
await manager.init() await manager.init()
@ -113,13 +116,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload an Apple TV config entry.""" """Unload an Apple TV config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
manager = hass.data[DOMAIN].pop(entry.unique_id)
await manager.disconnect()
return unload_ok
class AppleTVEntity(Entity): class AppleTVEntity(Entity):

View File

@ -37,15 +37,13 @@ from homeassistant.components.media_player import (
RepeatMode, RepeatMode,
async_process_play_media_url, async_process_play_media_url,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from . import AppleTVEntity, AppleTVManager from . import AppleTvConfigEntry, AppleTVEntity, AppleTVManager
from .browse_media import build_app_list from .browse_media import build_app_list
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -100,13 +98,13 @@ SUPPORT_FEATURE_MAPPING = {
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: AppleTvConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Load Apple TV media player based on a config entry.""" """Load Apple TV media player based on a config entry."""
name: str = config_entry.data[CONF_NAME] name: str = config_entry.data[CONF_NAME]
assert config_entry.unique_id is not None assert config_entry.unique_id is not None
manager: AppleTVManager = hass.data[DOMAIN][config_entry.unique_id] manager = config_entry.runtime_data
async_add_entities([AppleTvMediaPlayer(name, config_entry.unique_id, manager)]) async_add_entities([AppleTvMediaPlayer(name, config_entry.unique_id, manager)])

View File

@ -15,13 +15,11 @@ from homeassistant.components.remote import (
DEFAULT_HOLD_SECS, DEFAULT_HOLD_SECS,
RemoteEntity, RemoteEntity,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import AppleTVEntity, AppleTVManager from . import AppleTvConfigEntry, AppleTVEntity
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -38,14 +36,14 @@ COMMAND_TO_ATTRIBUTE = {
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: AppleTvConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Load Apple TV remote based on a config entry.""" """Load Apple TV remote based on a config entry."""
name: str = config_entry.data[CONF_NAME] name: str = config_entry.data[CONF_NAME]
# apple_tv config entries always have a unique id # apple_tv config entries always have a unique id
assert config_entry.unique_id is not None assert config_entry.unique_id is not None
manager: AppleTVManager = hass.data[DOMAIN][config_entry.unique_id] manager = config_entry.runtime_data
async_add_entities([AppleTVRemote(name, config_entry.unique_id, manager)]) async_add_entities([AppleTVRemote(name, config_entry.unique_id, manager)])