mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Use runtime_data in LG webOS TV (#135301)
This commit is contained in:
parent
c4b4cad335
commit
6fd4d7acaa
@ -14,6 +14,7 @@ from homeassistant.const import (
|
||||
CONF_HOST,
|
||||
CONF_NAME,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import Event, HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
@ -22,7 +23,6 @@ from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .const import (
|
||||
ATTR_CONFIG_ENTRY_ID,
|
||||
DATA_CONFIG_ENTRY,
|
||||
DATA_HASS_CONFIG,
|
||||
DOMAIN,
|
||||
PLATFORMS,
|
||||
@ -34,23 +34,23 @@ CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
type WebOsTvConfigEntry = ConfigEntry[WebOsClient]
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up the LG WebOS TV platform."""
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN].setdefault(DATA_CONFIG_ENTRY, {})
|
||||
hass.data[DOMAIN][DATA_HASS_CONFIG] = config
|
||||
hass.data.setdefault(DOMAIN, {DATA_HASS_CONFIG: config})
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: WebOsTvConfigEntry) -> bool:
|
||||
"""Set the config entry up."""
|
||||
host = entry.data[CONF_HOST]
|
||||
key = entry.data[CONF_CLIENT_SECRET]
|
||||
|
||||
# Attempt a connection, but fail gracefully if tv is off for example.
|
||||
client = WebOsClient(host, key)
|
||||
entry.runtime_data = client = WebOsClient(host, key)
|
||||
with suppress(*WEBOSTV_EXCEPTIONS):
|
||||
try:
|
||||
await client.connect()
|
||||
@ -61,7 +61,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
# Update the stored key without triggering reauth
|
||||
update_client_key(hass, entry, client)
|
||||
|
||||
hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id] = client
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
# set up notify platform, no entry support for notify component yet,
|
||||
@ -69,7 +68,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
hass.async_create_task(
|
||||
discovery.async_load_platform(
|
||||
hass,
|
||||
"notify",
|
||||
Platform.NOTIFY,
|
||||
DOMAIN,
|
||||
{
|
||||
CONF_NAME: entry.title,
|
||||
@ -92,7 +91,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
async def async_update_options(hass: HomeAssistant, entry: WebOsTvConfigEntry) -> None:
|
||||
"""Update options."""
|
||||
await hass.config_entries.async_reload(entry.entry_id)
|
||||
|
||||
@ -122,10 +121,10 @@ def update_client_key(
|
||||
hass.config_entries.async_update_entry(entry, data=data)
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: WebOsTvConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
client = hass.data[DOMAIN][DATA_CONFIG_ENTRY].pop(entry.entry_id)
|
||||
client = entry.runtime_data
|
||||
await hass_notify.async_reload(hass, DOMAIN)
|
||||
client.clear_state_update_callbacks()
|
||||
await client.disconnect()
|
||||
|
@ -9,7 +9,6 @@ from homeassistant.const import Platform
|
||||
|
||||
DOMAIN = "webostv"
|
||||
PLATFORMS = [Platform.MEDIA_PLAYER]
|
||||
DATA_CONFIG_ENTRY = "config_entry"
|
||||
DATA_HASS_CONFIG = "hass_config"
|
||||
DEFAULT_NAME = "LG webOS TV"
|
||||
|
||||
|
@ -7,11 +7,10 @@ from typing import Any
|
||||
from aiowebostv import WebOsClient
|
||||
|
||||
from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_CLIENT_SECRET, CONF_HOST, CONF_UNIQUE_ID
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DATA_CONFIG_ENTRY, DOMAIN
|
||||
from . import WebOsTvConfigEntry
|
||||
|
||||
TO_REDACT = {
|
||||
CONF_CLIENT_SECRET,
|
||||
@ -25,10 +24,10 @@ TO_REDACT = {
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
hass: HomeAssistant, entry: WebOsTvConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
client: WebOsClient = hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id]
|
||||
client: WebOsClient = entry.runtime_data
|
||||
|
||||
client_data = {
|
||||
"is_registered": client.is_registered(),
|
||||
|
@ -9,7 +9,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.device_registry import DeviceEntry
|
||||
|
||||
from . import async_control_connect
|
||||
from .const import DATA_CONFIG_ENTRY, DOMAIN, LIVE_TV_APP_ID, WEBOSTV_EXCEPTIONS
|
||||
from .const import DOMAIN, LIVE_TV_APP_ID, WEBOSTV_EXCEPTIONS
|
||||
|
||||
|
||||
@callback
|
||||
@ -55,7 +55,8 @@ def async_get_client_by_device_entry(
|
||||
Raises ValueError if client is not found.
|
||||
"""
|
||||
for config_entry_id in device.config_entries:
|
||||
if client := hass.data[DOMAIN][DATA_CONFIG_ENTRY].get(config_entry_id):
|
||||
if entry := hass.config_entries.async_get_entry(config_entry_id):
|
||||
client = entry.runtime_data
|
||||
break
|
||||
|
||||
if not client:
|
||||
|
@ -22,7 +22,6 @@ from homeassistant.components.media_player import (
|
||||
MediaPlayerState,
|
||||
MediaType,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import ATTR_COMMAND, ATTR_SUPPORTED_FEATURES
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
@ -34,13 +33,12 @@ from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.helpers.trigger import PluggableAction
|
||||
from homeassistant.helpers.typing import VolDictType
|
||||
|
||||
from . import update_client_key
|
||||
from . import WebOsTvConfigEntry, update_client_key
|
||||
from .const import (
|
||||
ATTR_BUTTON,
|
||||
ATTR_PAYLOAD,
|
||||
ATTR_SOUND_OUTPUT,
|
||||
CONF_SOURCES,
|
||||
DATA_CONFIG_ENTRY,
|
||||
DOMAIN,
|
||||
LIVE_TV_APP_ID,
|
||||
SERVICE_BUTTON,
|
||||
@ -87,7 +85,9 @@ SERVICES = (
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
hass: HomeAssistant,
|
||||
entry: WebOsTvConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the LG webOS Smart TV platform."""
|
||||
platform = entity_platform.async_get_current_platform()
|
||||
@ -95,8 +95,7 @@ async def async_setup_entry(
|
||||
for service_name, schema, method in SERVICES:
|
||||
platform.async_register_entity_service(service_name, schema, method)
|
||||
|
||||
client = hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id]
|
||||
async_add_entities([LgWebOSMediaPlayerEntity(entry, client)])
|
||||
async_add_entities([LgWebOSMediaPlayerEntity(entry)])
|
||||
|
||||
|
||||
def cmd[_T: LgWebOSMediaPlayerEntity, **_P](
|
||||
@ -133,10 +132,10 @@ class LgWebOSMediaPlayerEntity(RestoreEntity, MediaPlayerEntity):
|
||||
_attr_has_entity_name = True
|
||||
_attr_name = None
|
||||
|
||||
def __init__(self, entry: ConfigEntry, client: WebOsClient) -> None:
|
||||
def __init__(self, entry: WebOsTvConfigEntry) -> None:
|
||||
"""Initialize the webos device."""
|
||||
self._entry = entry
|
||||
self._client = client
|
||||
self._client = entry.runtime_data
|
||||
self._attr_assumed_state = True
|
||||
self._device_name = entry.title
|
||||
self._attr_unique_id = entry.unique_id
|
||||
|
@ -12,7 +12,7 @@ from homeassistant.const import ATTR_ICON
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from .const import ATTR_CONFIG_ENTRY_ID, DATA_CONFIG_ENTRY, DOMAIN, WEBOSTV_EXCEPTIONS
|
||||
from .const import ATTR_CONFIG_ENTRY_ID, WEBOSTV_EXCEPTIONS
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -29,9 +29,12 @@ async def async_get_service(
|
||||
if discovery_info is None:
|
||||
return None
|
||||
|
||||
client = hass.data[DOMAIN][DATA_CONFIG_ENTRY][discovery_info[ATTR_CONFIG_ENTRY_ID]]
|
||||
config_entry = hass.config_entries.async_get_entry(
|
||||
discovery_info[ATTR_CONFIG_ENTRY_ID]
|
||||
)
|
||||
assert config_entry is not None
|
||||
|
||||
return LgWebOSNotificationService(client)
|
||||
return LgWebOSNotificationService(config_entry.runtime_data)
|
||||
|
||||
|
||||
class LgWebOSNotificationService(BaseNotificationService):
|
||||
|
@ -20,7 +20,7 @@ rules:
|
||||
entity-event-setup: done
|
||||
entity-unique-id: done
|
||||
has-entity-name: done
|
||||
runtime-data: todo
|
||||
runtime-data: done
|
||||
test-before-configure: done
|
||||
test-before-setup: done
|
||||
unique-config-entry: done
|
||||
|
@ -129,6 +129,7 @@ async def test_failure_scenarios(
|
||||
)
|
||||
|
||||
entry = MockConfigEntry(domain="fake", state=ConfigEntryState.LOADED, data={})
|
||||
entry.runtime_data = None
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
device = device_registry.async_get_or_create(
|
||||
|
Loading…
x
Reference in New Issue
Block a user