diff --git a/homeassistant/components/webostv/__init__.py b/homeassistant/components/webostv/__init__.py index cd5485d4fd2..7852ca568a0 100644 --- a/homeassistant/components/webostv/__init__.py +++ b/homeassistant/components/webostv/__init__.py @@ -75,8 +75,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: host = entry.data[CONF_HOST] key = entry.data[CONF_CLIENT_SECRET] - wrapper = WebOsClientWrapper(host, client_key=key) - await wrapper.connect() + # Attempt a connection, but fail gracefully if tv is off for example. + client = WebOsClient(host, key) + with suppress(*WEBOSTV_EXCEPTIONS, WebOsTvPairError): + await client.connect() async def async_service_handler(service: ServiceCall) -> None: method = SERVICE_TO_METHOD[service.service] @@ -90,7 +92,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: DOMAIN, service, async_service_handler, schema=schema ) - hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id] = wrapper + 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, @@ -113,7 +115,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_on_stop(_event: Event) -> None: """Unregister callbacks and disconnect.""" - await wrapper.shutdown() + client.clear_state_update_callbacks() + await client.disconnect() entry.async_on_unload( hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_on_stop) @@ -145,7 +148,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: if unload_ok: client = hass.data[DOMAIN][DATA_CONFIG_ENTRY].pop(entry.entry_id) await hass_notify.async_reload(hass, DOMAIN) - await client.shutdown() + client.clear_state_update_callbacks() + await client.disconnect() # unregister service calls, check if this is the last entry to unload if unload_ok and not hass.data[DOMAIN][DATA_CONFIG_ENTRY]: @@ -153,25 +157,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.services.async_remove(DOMAIN, service) return unload_ok - - -class WebOsClientWrapper: - """Wrapper for a WebOS TV client with Home Assistant specific functions.""" - - def __init__(self, host: str, client_key: str) -> None: - """Set up the client.""" - self.host = host - self.client_key = client_key - self.client: WebOsClient | None = None - - async def connect(self) -> None: - """Attempt a connection, but fail gracefully if tv is off for example.""" - self.client = WebOsClient(self.host, self.client_key) - with suppress(*WEBOSTV_EXCEPTIONS, WebOsTvPairError): - await self.client.connect() - - async def shutdown(self) -> None: - """Unregister callbacks and disconnect.""" - assert self.client - self.client.clear_state_update_callbacks() - await self.client.disconnect() diff --git a/homeassistant/components/webostv/device_trigger.py b/homeassistant/components/webostv/device_trigger.py index 14854383ec8..c7e5701af02 100644 --- a/homeassistant/components/webostv/device_trigger.py +++ b/homeassistant/components/webostv/device_trigger.py @@ -16,7 +16,7 @@ from homeassistant.helpers.typing import ConfigType from . import trigger from .const import DOMAIN from .helpers import ( - async_get_client_wrapper_by_device_entry, + async_get_client_by_device_entry, async_get_device_entry_by_device_id, ) from .triggers.turn_on import ( @@ -43,7 +43,7 @@ async def async_validate_trigger_config( try: device = async_get_device_entry_by_device_id(hass, device_id) if DOMAIN in hass.data: - async_get_client_wrapper_by_device_entry(hass, device) + async_get_client_by_device_entry(hass, device) except ValueError as err: raise InvalidDeviceAutomationConfig(err) from err diff --git a/homeassistant/components/webostv/diagnostics.py b/homeassistant/components/webostv/diagnostics.py index ce62f51b540..5d88d61aa9d 100644 --- a/homeassistant/components/webostv/diagnostics.py +++ b/homeassistant/components/webostv/diagnostics.py @@ -27,7 +27,7 @@ async def async_get_config_entry_diagnostics( hass: HomeAssistant, entry: ConfigEntry ) -> dict[str, Any]: """Return diagnostics for a config entry.""" - client: WebOsClient = hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id].client + client: WebOsClient = hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id] client_data = { "is_registered": client.is_registered(), diff --git a/homeassistant/components/webostv/helpers.py b/homeassistant/components/webostv/helpers.py index 4f1ab9dfebe..1de1070a2f1 100644 --- a/homeassistant/components/webostv/helpers.py +++ b/homeassistant/components/webostv/helpers.py @@ -1,11 +1,13 @@ """Helper functions for webOS Smart TV.""" from __future__ import annotations +from aiowebostv import WebOsClient + from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.device_registry import DeviceEntry -from . import WebOsClientWrapper, async_control_connect +from . import async_control_connect from .const import DATA_CONFIG_ENTRY, DOMAIN, LIVE_TV_APP_ID, WEBOSTV_EXCEPTIONS @@ -46,25 +48,24 @@ def async_get_device_id_from_entity_id(hass: HomeAssistant, entity_id: str) -> s @callback -def async_get_client_wrapper_by_device_entry( +def async_get_client_by_device_entry( hass: HomeAssistant, device: DeviceEntry -) -> WebOsClientWrapper: +) -> WebOsClient: """ - Get WebOsClientWrapper from Device Registry by device entry. + Get WebOsClient from Device Registry by device entry. - Raises ValueError if client wrapper is not found. + Raises ValueError if client is not found. """ for config_entry_id in device.config_entries: - wrapper: WebOsClientWrapper | None - if wrapper := hass.data[DOMAIN][DATA_CONFIG_ENTRY].get(config_entry_id): + if client := hass.data[DOMAIN][DATA_CONFIG_ENTRY].get(config_entry_id): break - if not wrapper: + if not client: raise ValueError( f"Device {device.id} is not from an existing {DOMAIN} config entry" ) - return wrapper + return client async def async_get_sources(host: str, key: str) -> list[str]: diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index 1d7c92741a8..53c7fb66825 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -39,7 +39,6 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.trigger import PluggableAction -from . import WebOsClientWrapper from .const import ( ATTR_PAYLOAD, ATTR_SOUND_OUTPUT, @@ -83,9 +82,9 @@ async def async_setup_entry( assert unique_id name = config_entry.title sources = config_entry.options.get(CONF_SOURCES) - wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id] + client = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id] - async_add_entities([LgWebOSMediaPlayerEntity(wrapper, name, sources, unique_id)]) + async_add_entities([LgWebOSMediaPlayerEntity(client, name, sources, unique_id)]) _T = TypeVar("_T", bound="LgWebOSMediaPlayerEntity") @@ -126,14 +125,13 @@ class LgWebOSMediaPlayerEntity(RestoreEntity, MediaPlayerEntity): def __init__( self, - wrapper: WebOsClientWrapper, + client: WebOsClient, name: str, sources: list[str] | None, unique_id: str, ) -> None: """Initialize the webos device.""" - self._wrapper = wrapper - self._client: WebOsClient = wrapper.client + self._client = client self._attr_assumed_state = True self._attr_name = name self._attr_unique_id = unique_id diff --git a/homeassistant/components/webostv/notify.py b/homeassistant/components/webostv/notify.py index 82e61856187..c4cefc3cffe 100644 --- a/homeassistant/components/webostv/notify.py +++ b/homeassistant/components/webostv/notify.py @@ -26,9 +26,7 @@ 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] - ].client + client = hass.data[DOMAIN][DATA_CONFIG_ENTRY][discovery_info[ATTR_CONFIG_ENTRY_ID]] return LgWebOSNotificationService(client)