diff --git a/homeassistant/components/webostv/__init__.py b/homeassistant/components/webostv/__init__.py index 3a3ee8e4c7e..186a7e68a64 100644 --- a/homeassistant/components/webostv/__init__.py +++ b/homeassistant/components/webostv/__init__.py @@ -19,6 +19,7 @@ from homeassistant.const import ( from homeassistant.core import Event, HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers import config_validation as cv, discovery +from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.typing import ConfigType from .const import ( @@ -50,7 +51,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: WebOsTvConfigEntry) -> b key = entry.data[CONF_CLIENT_SECRET] # Attempt a connection, but fail gracefully if tv is off for example. - entry.runtime_data = client = WebOsClient(host, key) + entry.runtime_data = client = WebOsClient( + host, key, client_session=async_get_clientsession(hass) + ) with suppress(*WEBOSTV_EXCEPTIONS): try: await client.connect() @@ -96,9 +99,15 @@ async def async_update_options(hass: HomeAssistant, entry: WebOsTvConfigEntry) - await hass.config_entries.async_reload(entry.entry_id) -async def async_control_connect(host: str, key: str | None) -> WebOsClient: +async def async_control_connect( + hass: HomeAssistant, host: str, key: str | None +) -> WebOsClient: """LG Connection.""" - client = WebOsClient(host, key) + client = WebOsClient( + host, + key, + client_session=async_get_clientsession(hass), + ) try: await client.connect() except WebOsTvPairError: diff --git a/homeassistant/components/webostv/config_flow.py b/homeassistant/components/webostv/config_flow.py index 6086fad8afd..f8125f0c0cf 100644 --- a/homeassistant/components/webostv/config_flow.py +++ b/homeassistant/components/webostv/config_flow.py @@ -69,7 +69,7 @@ class FlowHandler(ConfigFlow, domain=DOMAIN): if user_input is not None: try: - client = await async_control_connect(self._host, None) + client = await async_control_connect(self.hass, self._host, None) except WebOsTvPairError: errors["base"] = "error_pairing" except WEBOSTV_EXCEPTIONS: @@ -130,7 +130,7 @@ class FlowHandler(ConfigFlow, domain=DOMAIN): if user_input is not None: try: - client = await async_control_connect(self._host, None) + client = await async_control_connect(self.hass, self._host, None) except WebOsTvPairError: errors["base"] = "error_pairing" except WEBOSTV_EXCEPTIONS: @@ -154,7 +154,7 @@ class FlowHandler(ConfigFlow, domain=DOMAIN): client_key = reconfigure_entry.data.get(CONF_CLIENT_SECRET) try: - client = await async_control_connect(host, client_key) + client = await async_control_connect(self.hass, host, client_key) except WebOsTvPairError: errors["base"] = "error_pairing" except WEBOSTV_EXCEPTIONS: @@ -195,7 +195,7 @@ class OptionsFlowHandler(OptionsFlow): options_input = {CONF_SOURCES: user_input[CONF_SOURCES]} return self.async_create_entry(title="", data=options_input) # Get sources - sources_list = await async_get_sources(self.host, self.key) + sources_list = await async_get_sources(self.hass, self.host, self.key) if not sources_list: errors["base"] = "cannot_retrieve" diff --git a/homeassistant/components/webostv/const.py b/homeassistant/components/webostv/const.py index 65d964d8fd4..9c85c4cf5ac 100644 --- a/homeassistant/components/webostv/const.py +++ b/homeassistant/components/webostv/const.py @@ -2,8 +2,8 @@ import asyncio +import aiohttp from aiowebostv import WebOsTvCommandError -from websockets.exceptions import ConnectionClosed, ConnectionClosedOK from homeassistant.const import Platform @@ -27,11 +27,10 @@ SERVICE_SELECT_SOUND_OUTPUT = "select_sound_output" LIVE_TV_APP_ID = "com.webos.app.livetv" WEBOSTV_EXCEPTIONS = ( - OSError, - ConnectionClosed, - ConnectionClosedOK, - ConnectionRefusedError, + ConnectionResetError, WebOsTvCommandError, - TimeoutError, + aiohttp.ClientConnectorError, + aiohttp.ServerDisconnectedError, asyncio.CancelledError, + asyncio.TimeoutError, ) diff --git a/homeassistant/components/webostv/helpers.py b/homeassistant/components/webostv/helpers.py index 3aea860798a..f4563ef2394 100644 --- a/homeassistant/components/webostv/helpers.py +++ b/homeassistant/components/webostv/helpers.py @@ -72,10 +72,10 @@ def async_get_client_by_device_entry( ) -async def async_get_sources(host: str, key: str) -> list[str]: +async def async_get_sources(hass: HomeAssistant, host: str, key: str) -> list[str]: """Construct sources list.""" try: - client = await async_control_connect(host, key) + client = await async_control_connect(hass, host, key) except WEBOSTV_EXCEPTIONS: return [] diff --git a/homeassistant/components/webostv/manifest.json b/homeassistant/components/webostv/manifest.json index 627bb83572c..f1a8e163398 100644 --- a/homeassistant/components/webostv/manifest.json +++ b/homeassistant/components/webostv/manifest.json @@ -1,12 +1,12 @@ { "domain": "webostv", - "name": "LG webOS Smart TV", + "name": "LG webOS TV", "codeowners": ["@thecode"], "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/webostv", "iot_class": "local_push", "loggers": ["aiowebostv"], - "requirements": ["aiowebostv==0.5.0"], + "requirements": ["aiowebostv==0.6.0"], "ssdp": [ { "st": "urn:lge-com:service:webos-second-screen:1" diff --git a/homeassistant/components/webostv/quality_scale.yaml b/homeassistant/components/webostv/quality_scale.yaml index c4828e9e6dd..08c594d0298 100644 --- a/homeassistant/components/webostv/quality_scale.yaml +++ b/homeassistant/components/webostv/quality_scale.yaml @@ -72,7 +72,5 @@ rules: # Platinum async-dependency: done - inject-websession: - status: todo - comment: migrate to aiohttp + inject-websession: done strict-typing: done diff --git a/homeassistant/generated/integrations.json b/homeassistant/generated/integrations.json index 2ee871964c9..9a7167f5367 100644 --- a/homeassistant/generated/integrations.json +++ b/homeassistant/generated/integrations.json @@ -3340,7 +3340,7 @@ "integration_type": "hub", "config_flow": true, "iot_class": "local_push", - "name": "LG webOS Smart TV" + "name": "LG webOS TV" } } }, diff --git a/requirements_all.txt b/requirements_all.txt index 96f276097b0..ac0486c5324 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -416,7 +416,7 @@ aiowaqi==3.1.0 aiowatttime==0.1.1 # homeassistant.components.webostv -aiowebostv==0.5.0 +aiowebostv==0.6.0 # homeassistant.components.withings aiowithings==3.1.4 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 6b1053331cd..262015682af 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -398,7 +398,7 @@ aiowaqi==3.1.0 aiowatttime==0.1.1 # homeassistant.components.webostv -aiowebostv==0.5.0 +aiowebostv==0.6.0 # homeassistant.components.withings aiowithings==3.1.4 diff --git a/tests/components/webostv/test_config_flow.py b/tests/components/webostv/test_config_flow.py index 38c78bd087a..a52acae4b03 100644 --- a/tests/components/webostv/test_config_flow.py +++ b/tests/components/webostv/test_config_flow.py @@ -107,7 +107,7 @@ async def test_options_flow_cannot_retrieve(hass: HomeAssistant, client) -> None """Test options config flow cannot retrieve sources.""" entry = await setup_webostv(hass) - client.connect.side_effect = ConnectionRefusedError + client.connect.side_effect = ConnectionResetError result = await hass.config_entries.options.async_init(entry.entry_id) await hass.async_block_till_done() @@ -141,7 +141,7 @@ async def test_form_cannot_connect(hass: HomeAssistant, client) -> None: data=MOCK_USER_CONFIG, ) - client.connect.side_effect = ConnectionRefusedError + client.connect.side_effect = ConnectionResetError result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={} ) @@ -305,7 +305,7 @@ async def test_reauth_successful(hass: HomeAssistant, client) -> None: ("side_effect", "error"), [ (WebOsTvPairError, "error_pairing"), - (ConnectionRefusedError, "cannot_connect"), + (ConnectionResetError, "cannot_connect"), ], ) async def test_reauth_errors(hass: HomeAssistant, client, side_effect, error) -> None: @@ -360,7 +360,7 @@ async def test_reconfigure_successful(hass: HomeAssistant, client) -> None: ("side_effect", "error"), [ (WebOsTvPairError, "error_pairing"), - (ConnectionRefusedError, "cannot_connect"), + (ConnectionResetError, "cannot_connect"), ], ) async def test_reconfigure_errors( diff --git a/tests/components/webostv/test_notify.py b/tests/components/webostv/test_notify.py index b12cd0c7c6c..61c73d1b151 100644 --- a/tests/components/webostv/test_notify.py +++ b/tests/components/webostv/test_notify.py @@ -125,7 +125,7 @@ async def test_icon_not_found( ("side_effect", "error"), [ (WebOsTvPairError, "Pairing with TV failed"), - (ConnectionRefusedError, "TV unreachable"), + (ConnectionResetError, "TV unreachable"), ], ) async def test_connection_errors(