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