Skip to content

Commit

Permalink
Use runtime_data in LG webOS TV (home-assistant#135301)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecode authored Jan 10, 2025
1 parent c4b4cad commit 6fd4d7a
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 30 deletions.
21 changes: 10 additions & 11 deletions homeassistant/components/webostv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
CONF_HOST,
CONF_NAME,
EVENT_HOMEASSISTANT_STOP,
Platform,
)
from homeassistant.core import Event, HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
Expand All @@ -22,7 +23,6 @@

from .const import (
ATTR_CONFIG_ENTRY_ID,
DATA_CONFIG_ENTRY,
DATA_HASS_CONFIG,
DOMAIN,
PLATFORMS,
Expand All @@ -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()
Expand All @@ -61,15 +61,14 @@ 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,
# have to use discovery to load platform.
hass.async_create_task(
discovery.async_load_platform(
hass,
"notify",
Platform.NOTIFY,
DOMAIN,
{
CONF_NAME: entry.title,
Expand All @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/webostv/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
7 changes: 3 additions & 4 deletions homeassistant/components/webostv/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/webostv/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 7 additions & 8 deletions homeassistant/components/webostv/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -87,16 +85,17 @@


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()

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](
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions homeassistant/components/webostv/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/webostv/quality_scale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/components/webostv/test_device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 6fd4d7a

Please sign in to comment.