Store runtime data inside the config entry in SamsungTV (#116787)

pull/116812/head
Michael 2024-05-04 19:12:31 +02:00 committed by GitHub
parent ec46d4d644
commit f143ed9eeb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 31 additions and 23 deletions

View File

@ -54,6 +54,8 @@ PLATFORMS = [Platform.MEDIA_PLAYER, Platform.REMOTE]
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
SamsungTVConfigEntry = ConfigEntry[SamsungTVBridge]
@callback
def _async_get_device_bridge(
@ -123,10 +125,8 @@ async def _async_update_ssdp_locations(hass: HomeAssistant, entry: ConfigEntry)
hass.config_entries.async_update_entry(entry, data={**entry.data, **updates})
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: SamsungTVConfigEntry) -> bool:
"""Set up the Samsung TV platform."""
hass.data.setdefault(DOMAIN, {})
# Initialize bridge
if entry.data.get(CONF_METHOD) == METHOD_ENCRYPTED_WEBSOCKET:
if not entry.data.get(CONF_TOKEN) or not entry.data.get(CONF_SESSION_ID):
@ -161,7 +161,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.async_on_unload(debounced_reloader.async_shutdown)
entry.async_on_unload(entry.add_update_listener(debounced_reloader.async_call))
hass.data[DOMAIN][entry.entry_id] = bridge
entry.runtime_data = bridge
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
@ -250,11 +250,11 @@ async def _async_create_bridge_with_updated_data(
return bridge
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: SamsungTVConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
bridge: SamsungTVBridge = hass.data[DOMAIN][entry.entry_id]
bridge = entry.runtime_data
LOGGER.debug("Stopping SamsungTVBridge %s", bridge.host)
await bridge.async_close_remote()
return unload_ok

View File

@ -15,7 +15,6 @@ from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
from . import trigger
from .const import DOMAIN
from .helpers import (
async_get_client_by_device_entry,
async_get_device_entry_by_device_id,
@ -43,8 +42,7 @@ async def async_validate_trigger_config(
device_id = config[CONF_DEVICE_ID]
try:
device = async_get_device_entry_by_device_id(hass, device_id)
if DOMAIN in hass.data:
async_get_client_by_device_entry(hass, device)
async_get_client_by_device_entry(hass, device)
except ValueError as err:
raise InvalidDeviceAutomationConfig(err) from err

View File

@ -5,21 +5,20 @@ from __future__ import annotations
from typing import Any
from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_TOKEN
from homeassistant.core import HomeAssistant
from .bridge import SamsungTVBridge
from .const import CONF_SESSION_ID, DOMAIN
from . import SamsungTVConfigEntry
from .const import CONF_SESSION_ID
TO_REDACT = {CONF_TOKEN, CONF_SESSION_ID}
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry
hass: HomeAssistant, entry: SamsungTVConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
bridge: SamsungTVBridge = hass.data[DOMAIN][entry.entry_id]
bridge = entry.runtime_data
return {
"entry": async_redact_data(entry.as_dict(), TO_REDACT),
"device_info": await bridge.async_device_info(),

View File

@ -2,6 +2,7 @@
from __future__ import annotations
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.device_registry import DeviceEntry
@ -52,10 +53,15 @@ def async_get_client_by_device_entry(
Raises ValueError if client is not found.
"""
domain_data: dict[str, SamsungTVBridge] = hass.data[DOMAIN]
for config_entry_id in device.config_entries:
if bridge := domain_data.get(config_entry_id):
return bridge
entry = hass.config_entries.async_get_entry(config_entry_id)
if (
entry
and entry.state == ConfigEntryState.LOADED
and hasattr(entry, "runtime_data")
and isinstance(entry.runtime_data, SamsungTVBridge)
):
return entry.runtime_data
raise ValueError(
f"Device {device.id} is not from an existing {DOMAIN} config entry"

View File

@ -38,6 +38,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.trigger import PluggableAction
from homeassistant.util.async_ import create_eager_task
from . import SamsungTVConfigEntry
from .bridge import SamsungTVBridge, SamsungTVWSBridge
from .const import CONF_SSDP_RENDERING_CONTROL_LOCATION, DOMAIN, LOGGER
from .entity import SamsungTVEntity
@ -65,10 +66,12 @@ APP_LIST_DELAY = 3
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
entry: SamsungTVConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Samsung TV from a config entry."""
bridge = hass.data[DOMAIN][entry.entry_id]
bridge = entry.runtime_data
async_add_entities([SamsungTVDevice(bridge, entry)], True)

View File

@ -6,19 +6,21 @@ from collections.abc import Iterable
from typing import Any
from homeassistant.components.remote import ATTR_NUM_REPEATS, RemoteEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN, LOGGER
from . import SamsungTVConfigEntry
from .const import LOGGER
from .entity import SamsungTVEntity
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
entry: SamsungTVConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Samsung TV from a config entry."""
bridge = hass.data[DOMAIN][entry.entry_id]
bridge = entry.runtime_data
async_add_entities([SamsungTVRemote(bridge=bridge, config_entry=entry)])