From 681705394d6533550845148796ca74bc268f52cc Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Sun, 17 Mar 2024 17:42:48 +0100 Subject: [PATCH] Remove deprecated `hass.components` from network helper function (#113615) * Remove deprecated `hass.components` from network helper function * Remove deprecated use of `hass.components` in alexa camera tests --- homeassistant/helpers/network.py | 12 ++++++++--- tests/components/alexa/test_smart_home.py | 10 ++++----- tests/helpers/test_network.py | 25 +++++++++-------------- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/homeassistant/helpers/network.py b/homeassistant/helpers/network.py index 510639b5506..ed6339f9996 100644 --- a/homeassistant/helpers/network.py +++ b/homeassistant/helpers/network.py @@ -5,7 +5,6 @@ from __future__ import annotations from collections.abc import Callable from contextlib import suppress from ipaddress import ip_address -from typing import cast from hass_nabucasa import remote import yarl @@ -299,9 +298,16 @@ def _get_external_url( def _get_cloud_url(hass: HomeAssistant, require_current_request: bool = False) -> str: """Get external Home Assistant Cloud URL of this instance.""" if "cloud" in hass.config.components: + # Local import to avoid circular dependencies + # pylint: disable-next=import-outside-toplevel + from homeassistant.components.cloud import ( + CloudNotAvailable, + async_remote_ui_url, + ) + try: - cloud_url = yarl.URL(cast(str, hass.components.cloud.async_remote_ui_url())) - except hass.components.cloud.CloudNotAvailable as err: + cloud_url = yarl.URL(async_remote_ui_url(hass)) + except CloudNotAvailable as err: raise NoURLAvailableError from err if not require_current_request or cloud_url.host == _get_request_host(): diff --git a/tests/components/alexa/test_smart_home.py b/tests/components/alexa/test_smart_home.py index 5204b32821c..b7e6a5e53ac 100644 --- a/tests/components/alexa/test_smart_home.py +++ b/tests/components/alexa/test_smart_home.py @@ -5464,9 +5464,8 @@ async def test_camera_discovery(hass: HomeAssistant, mock_stream: None) -> None: ) hass.config.components.add("cloud") - with patch.object( - hass.components.cloud, - "async_remote_ui_url", + with patch( + "homeassistant.components.cloud.async_remote_ui_url", return_value="https://example.nabu.casa", ): appliance = await discovery_test(device, hass) @@ -5495,9 +5494,8 @@ async def test_camera_discovery_without_stream(hass: HomeAssistant) -> None: ) hass.config.components.add("cloud") - with patch.object( - hass.components.cloud, - "async_remote_ui_url", + with patch( + "homeassistant.components.cloud.async_remote_ui_url", return_value="https://example.nabu.casa", ): appliance = await discovery_test(device, hass) diff --git a/tests/helpers/test_network.py b/tests/helpers/test_network.py index cbffdc88e19..3c0793290d0 100644 --- a/tests/helpers/test_network.py +++ b/tests/helpers/test_network.py @@ -362,9 +362,8 @@ async def test_get_cloud_url(hass: HomeAssistant) -> None: assert hass.config.external_url is None hass.config.components.add("cloud") - with patch.object( - hass.components.cloud, - "async_remote_ui_url", + with patch( + "homeassistant.components.cloud.async_remote_ui_url", return_value="https://example.nabu.casa", ): assert _get_cloud_url(hass) == "https://example.nabu.casa" @@ -387,9 +386,8 @@ async def test_get_cloud_url(hass: HomeAssistant) -> None: ), pytest.raises(NoURLAvailableError): _get_cloud_url(hass, require_current_request=True) - with patch.object( - hass.components.cloud, - "async_remote_ui_url", + with patch( + "homeassistant.components.cloud.async_remote_ui_url", side_effect=cloud.CloudNotAvailable, ), pytest.raises(NoURLAvailableError): _get_cloud_url(hass) @@ -410,9 +408,8 @@ async def test_get_external_url_cloud_fallback(hass: HomeAssistant) -> None: # Add Cloud to the previous test hass.config.components.add("cloud") - with patch.object( - hass.components.cloud, - "async_remote_ui_url", + with patch( + "homeassistant.components.cloud.async_remote_ui_url", return_value="https://example.nabu.casa", ): assert _get_external_url(hass, allow_cloud=False) == "http://1.1.1.1:8123" @@ -436,9 +433,8 @@ async def test_get_external_url_cloud_fallback(hass: HomeAssistant) -> None: # Add Cloud to the previous test hass.config.components.add("cloud") - with patch.object( - hass.components.cloud, - "async_remote_ui_url", + with patch( + "homeassistant.components.cloud.async_remote_ui_url", return_value="https://example.nabu.casa", ): assert _get_external_url(hass, allow_cloud=False) == "https://example.com" @@ -710,9 +706,8 @@ async def test_is_hass_url(hass: HomeAssistant) -> None: assert is_hass_url(hass, "http://example.com:443") is False assert is_hass_url(hass, "http://example.com") is False - with patch.object( - hass.components.cloud, - "async_remote_ui_url", + with patch( + "homeassistant.components.cloud.async_remote_ui_url", return_value="https://example.nabu.casa", ): assert is_hass_url(hass, "https://example.nabu.casa") is False