mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Store runtime data inside the config entry in SamsungTV (#116787)
This commit is contained in:
parent
ec46d4d644
commit
f143ed9eeb
@ -54,6 +54,8 @@ PLATFORMS = [Platform.MEDIA_PLAYER, Platform.REMOTE]
|
|||||||
|
|
||||||
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
||||||
|
|
||||||
|
SamsungTVConfigEntry = ConfigEntry[SamsungTVBridge]
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_get_device_bridge(
|
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})
|
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."""
|
"""Set up the Samsung TV platform."""
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
|
|
||||||
# Initialize bridge
|
# Initialize bridge
|
||||||
if entry.data.get(CONF_METHOD) == METHOD_ENCRYPTED_WEBSOCKET:
|
if entry.data.get(CONF_METHOD) == METHOD_ENCRYPTED_WEBSOCKET:
|
||||||
if not entry.data.get(CONF_TOKEN) or not entry.data.get(CONF_SESSION_ID):
|
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(debounced_reloader.async_shutdown)
|
||||||
entry.async_on_unload(entry.add_update_listener(debounced_reloader.async_call))
|
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)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
@ -250,11 +250,11 @@ async def _async_create_bridge_with_updated_data(
|
|||||||
return bridge
|
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 a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
if unload_ok:
|
if unload_ok:
|
||||||
bridge: SamsungTVBridge = hass.data[DOMAIN][entry.entry_id]
|
bridge = entry.runtime_data
|
||||||
LOGGER.debug("Stopping SamsungTVBridge %s", bridge.host)
|
LOGGER.debug("Stopping SamsungTVBridge %s", bridge.host)
|
||||||
await bridge.async_close_remote()
|
await bridge.async_close_remote()
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
@ -15,7 +15,6 @@ from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
|
|||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
from . import trigger
|
from . import trigger
|
||||||
from .const import DOMAIN
|
|
||||||
from .helpers import (
|
from .helpers import (
|
||||||
async_get_client_by_device_entry,
|
async_get_client_by_device_entry,
|
||||||
async_get_device_entry_by_device_id,
|
async_get_device_entry_by_device_id,
|
||||||
@ -43,8 +42,7 @@ async def async_validate_trigger_config(
|
|||||||
device_id = config[CONF_DEVICE_ID]
|
device_id = config[CONF_DEVICE_ID]
|
||||||
try:
|
try:
|
||||||
device = async_get_device_entry_by_device_id(hass, device_id)
|
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:
|
except ValueError as err:
|
||||||
raise InvalidDeviceAutomationConfig(err) from err
|
raise InvalidDeviceAutomationConfig(err) from err
|
||||||
|
|
||||||
|
@ -5,21 +5,20 @@ from __future__ import annotations
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.diagnostics import async_redact_data
|
from homeassistant.components.diagnostics import async_redact_data
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_TOKEN
|
from homeassistant.const import CONF_TOKEN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .bridge import SamsungTVBridge
|
from . import SamsungTVConfigEntry
|
||||||
from .const import CONF_SESSION_ID, DOMAIN
|
from .const import CONF_SESSION_ID
|
||||||
|
|
||||||
TO_REDACT = {CONF_TOKEN, CONF_SESSION_ID}
|
TO_REDACT = {CONF_TOKEN, CONF_SESSION_ID}
|
||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, entry: ConfigEntry
|
hass: HomeAssistant, entry: SamsungTVConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
bridge: SamsungTVBridge = hass.data[DOMAIN][entry.entry_id]
|
bridge = entry.runtime_data
|
||||||
return {
|
return {
|
||||||
"entry": async_redact_data(entry.as_dict(), TO_REDACT),
|
"entry": async_redact_data(entry.as_dict(), TO_REDACT),
|
||||||
"device_info": await bridge.async_device_info(),
|
"device_info": await bridge.async_device_info(),
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.helpers.device_registry import DeviceEntry
|
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.
|
Raises ValueError if client is not found.
|
||||||
"""
|
"""
|
||||||
domain_data: dict[str, SamsungTVBridge] = hass.data[DOMAIN]
|
|
||||||
for config_entry_id in device.config_entries:
|
for config_entry_id in device.config_entries:
|
||||||
if bridge := domain_data.get(config_entry_id):
|
entry = hass.config_entries.async_get_entry(config_entry_id)
|
||||||
return bridge
|
if (
|
||||||
|
entry
|
||||||
|
and entry.state == ConfigEntryState.LOADED
|
||||||
|
and hasattr(entry, "runtime_data")
|
||||||
|
and isinstance(entry.runtime_data, SamsungTVBridge)
|
||||||
|
):
|
||||||
|
return entry.runtime_data
|
||||||
|
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Device {device.id} is not from an existing {DOMAIN} config entry"
|
f"Device {device.id} is not from an existing {DOMAIN} config entry"
|
||||||
|
@ -38,6 +38,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
from homeassistant.helpers.trigger import PluggableAction
|
from homeassistant.helpers.trigger import PluggableAction
|
||||||
from homeassistant.util.async_ import create_eager_task
|
from homeassistant.util.async_ import create_eager_task
|
||||||
|
|
||||||
|
from . import SamsungTVConfigEntry
|
||||||
from .bridge import SamsungTVBridge, SamsungTVWSBridge
|
from .bridge import SamsungTVBridge, SamsungTVWSBridge
|
||||||
from .const import CONF_SSDP_RENDERING_CONTROL_LOCATION, DOMAIN, LOGGER
|
from .const import CONF_SSDP_RENDERING_CONTROL_LOCATION, DOMAIN, LOGGER
|
||||||
from .entity import SamsungTVEntity
|
from .entity import SamsungTVEntity
|
||||||
@ -65,10 +66,12 @@ APP_LIST_DELAY = 3
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: SamsungTVConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Samsung TV from a config entry."""
|
"""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)
|
async_add_entities([SamsungTVDevice(bridge, entry)], True)
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,19 +6,21 @@ from collections.abc import Iterable
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.remote import ATTR_NUM_REPEATS, RemoteEntity
|
from homeassistant.components.remote import ATTR_NUM_REPEATS, RemoteEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
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 .const import DOMAIN, LOGGER
|
from . import SamsungTVConfigEntry
|
||||||
|
from .const import LOGGER
|
||||||
from .entity import SamsungTVEntity
|
from .entity import SamsungTVEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: SamsungTVConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Samsung TV from a config entry."""
|
"""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)])
|
async_add_entities([SamsungTVRemote(bridge=bridge, config_entry=entry)])
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user