From 6fd4d7acaae3e8f2919cce3de80aeb660ea5aed2 Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Fri, 10 Jan 2025 19:16:25 +0200 Subject: [PATCH] Use runtime_data in LG webOS TV (#135301) --- homeassistant/components/webostv/__init__.py | 21 +++++++++---------- homeassistant/components/webostv/const.py | 1 - .../components/webostv/diagnostics.py | 7 +++---- homeassistant/components/webostv/helpers.py | 5 +++-- .../components/webostv/media_player.py | 15 +++++++------ homeassistant/components/webostv/notify.py | 9 +++++--- .../components/webostv/quality_scale.yaml | 2 +- .../components/webostv/test_device_trigger.py | 1 + 8 files changed, 31 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/webostv/__init__.py b/homeassistant/components/webostv/__init__.py index 410b3d853a1c6f..3a3ee8e4c7eea9 100644 --- a/homeassistant/components/webostv/__init__.py +++ b/homeassistant/components/webostv/__init__.py @@ -14,6 +14,7 @@ CONF_HOST, CONF_NAME, EVENT_HOMEASSISTANT_STOP, + Platform, ) from homeassistant.core import Event, HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed @@ -22,7 +23,6 @@ from .const import ( ATTR_CONFIG_ENTRY_ID, - DATA_CONFIG_ENTRY, DATA_HASS_CONFIG, DOMAIN, PLATFORMS, @@ -34,23 +34,23 @@ _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_on_stop(_event: Event) -> None: 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() diff --git a/homeassistant/components/webostv/const.py b/homeassistant/components/webostv/const.py index 0d839568f13dcf..65d964d8fd4ea2 100644 --- a/homeassistant/components/webostv/const.py +++ b/homeassistant/components/webostv/const.py @@ -9,7 +9,6 @@ DOMAIN = "webostv" PLATFORMS = [Platform.MEDIA_PLAYER] -DATA_CONFIG_ENTRY = "config_entry" DATA_HASS_CONFIG = "hass_config" DEFAULT_NAME = "LG webOS TV" diff --git a/homeassistant/components/webostv/diagnostics.py b/homeassistant/components/webostv/diagnostics.py index 1657fb71d26d8d..d5e2dac06dcf46 100644 --- a/homeassistant/components/webostv/diagnostics.py +++ b/homeassistant/components/webostv/diagnostics.py @@ -7,11 +7,10 @@ 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 @@ 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(), diff --git a/homeassistant/components/webostv/helpers.py b/homeassistant/components/webostv/helpers.py index edcfdcfed8b486..f6b5fa04d86472 100644 --- a/homeassistant/components/webostv/helpers.py +++ b/homeassistant/components/webostv/helpers.py @@ -9,7 +9,7 @@ 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: diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index 599eb48a69d38e..719e3edbf4b9b5 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -22,7 +22,6 @@ 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.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 @@ 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 diff --git a/homeassistant/components/webostv/notify.py b/homeassistant/components/webostv/notify.py index e46e3cb202d324..fde0e6ad6074c6 100644 --- a/homeassistant/components/webostv/notify.py +++ b/homeassistant/components/webostv/notify.py @@ -12,7 +12,7 @@ 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): diff --git a/homeassistant/components/webostv/quality_scale.yaml b/homeassistant/components/webostv/quality_scale.yaml index 1bf521ef2e2672..22c0b4155abe72 100644 --- a/homeassistant/components/webostv/quality_scale.yaml +++ b/homeassistant/components/webostv/quality_scale.yaml @@ -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 diff --git a/tests/components/webostv/test_device_trigger.py b/tests/components/webostv/test_device_trigger.py index 41045969335bae..5c3d7d63346f82 100644 --- a/tests/components/webostv/test_device_trigger.py +++ b/tests/components/webostv/test_device_trigger.py @@ -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(