From e90fe96b4e8a1aaab801d8d3aae4bbe7ca5b97b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 22 Oct 2025 23:36:48 +0300 Subject: [PATCH] huawei_lte test cleanups (#154961) --- .../components/huawei_lte/quality_scale.yaml | 2 +- tests/components/huawei_lte/__init__.py | 24 +--- tests/components/huawei_lte/test_button.py | 36 +++--- .../components/huawei_lte/test_diagnostics.py | 15 +-- tests/components/huawei_lte/test_select.py | 19 +-- .../{test_switches.py => test_switch.py} | 115 ++++++++++-------- 6 files changed, 107 insertions(+), 104 deletions(-) rename tests/components/huawei_lte/{test_switches.py => test_switch.py} (53%) diff --git a/homeassistant/components/huawei_lte/quality_scale.yaml b/homeassistant/components/huawei_lte/quality_scale.yaml index 05cfb812da1..871329e7401 100644 --- a/homeassistant/components/huawei_lte/quality_scale.yaml +++ b/homeassistant/components/huawei_lte/quality_scale.yaml @@ -41,7 +41,7 @@ rules: reauthentication-flow: done test-coverage: status: todo - comment: Get percentage up there, add missing actual action press invocations in button tests' suspended state tests, rename test_switch.py to test_switch.py + make its functions receive hass as first parameter where applicable. + comment: Get percentage up there, add missing actual action press invocations in button tests' suspended state tests. # Gold devices: done diff --git a/tests/components/huawei_lte/__init__.py b/tests/components/huawei_lte/__init__.py index f9f16a2473c..de93908c78a 100644 --- a/tests/components/huawei_lte/__init__.py +++ b/tests/components/huawei_lte/__init__.py @@ -5,26 +5,8 @@ from unittest.mock import MagicMock from huawei_lte_api.enums.cradle import ConnectionStatusEnum -def magic_client(multi_basic_settings_value: dict) -> MagicMock: - """Mock huawei_lte.Client.""" - information = MagicMock(return_value={"SerialNumber": "test-serial-number"}) - check_notifications = MagicMock(return_value={"SmsStorageFull": 0}) - status = MagicMock( - return_value={"ConnectionStatus": ConnectionStatusEnum.CONNECTED.value} - ) - multi_basic_settings = MagicMock(return_value=multi_basic_settings_value) - wifi_feature_switch = MagicMock(return_value={"wifi24g_switch_enable": 1}) - device = MagicMock(information=information) - monitoring = MagicMock(check_notifications=check_notifications, status=status) - wlan = MagicMock( - multi_basic_settings=multi_basic_settings, - wifi_feature_switch=wifi_feature_switch, - ) - return MagicMock(device=device, monitoring=monitoring, wlan=wlan) - - -def magic_client_full() -> MagicMock: - """Extended mock for huawei_lte.Client with all API methods.""" +def magic_client() -> MagicMock: + """Mock huawei_lte.Client with all API methods.""" information = MagicMock( return_value={ "DeviceName": "Test Router", @@ -121,7 +103,7 @@ def magic_client_full() -> MagicMock: ) status = MagicMock( return_value={ - "ConnectionStatus": "901", + "ConnectionStatus": str(ConnectionStatusEnum.CONNECTED.value), "WifiConnectionStatus": None, "SignalStrength": None, "SignalIcon": "5", diff --git a/tests/components/huawei_lte/test_button.py b/tests/components/huawei_lte/test_button.py index c99c08c436c..1bffdc8a411 100644 --- a/tests/components/huawei_lte/test_button.py +++ b/tests/components/huawei_lte/test_button.py @@ -22,23 +22,25 @@ MOCK_CONF_URL = "http://huawei-lte.example.com" @patch("homeassistant.components.huawei_lte.Connection", MagicMock()) -@patch("homeassistant.components.huawei_lte.Client", return_value=magic_client({})) -async def test_clear_traffic_statistics(client, hass: HomeAssistant) -> None: +async def test_clear_traffic_statistics(hass: HomeAssistant) -> None: """Test clear traffic statistics button.""" huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: MOCK_CONF_URL}) huawei_lte.add_to_hass(hass) - await hass.config_entries.async_setup(huawei_lte.entry_id) + client = magic_client() + with patch("homeassistant.components.huawei_lte.Client", return_value=client): + await hass.config_entries.async_setup(huawei_lte.entry_id) await hass.async_block_till_done() + await hass.services.async_call( BUTTON_DOMAIN, SERVICE_PRESS, - {ATTR_ENTITY_ID: f"button.lte_{BUTTON_KEY_CLEAR_TRAFFIC_STATISTICS}"}, + {ATTR_ENTITY_ID: f"button.test_router_{BUTTON_KEY_CLEAR_TRAFFIC_STATISTICS}"}, blocking=True, ) await hass.async_block_till_done() - client.return_value.monitoring.set_clear_traffic.assert_called_once() + client.monitoring.set_clear_traffic.assert_called_once() - client.return_value.monitoring.set_clear_traffic.reset_mock() + client.monitoring.set_clear_traffic.reset_mock() await hass.services.async_call( DOMAIN, SERVICE_SUSPEND_INTEGRATION, @@ -46,27 +48,31 @@ async def test_clear_traffic_statistics(client, hass: HomeAssistant) -> None: blocking=True, ) await hass.async_block_till_done() - client.return_value.monitoring.set_clear_traffic.assert_not_called() + client.monitoring.set_clear_traffic.assert_not_called() -@patch("homeassistant.components.huawei_lte.Connection", MagicMock()) -@patch("homeassistant.components.huawei_lte.Client", return_value=magic_client({})) -async def test_restart(client, hass: HomeAssistant) -> None: +async def test_restart(hass: HomeAssistant) -> None: """Test restart button.""" huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: MOCK_CONF_URL}) huawei_lte.add_to_hass(hass) - await hass.config_entries.async_setup(huawei_lte.entry_id) + client = magic_client() + with ( + patch("homeassistant.components.huawei_lte.Connection", MagicMock()), + patch("homeassistant.components.huawei_lte.Client", return_value=client), + ): + await hass.config_entries.async_setup(huawei_lte.entry_id) await hass.async_block_till_done() + await hass.services.async_call( BUTTON_DOMAIN, SERVICE_PRESS, - {ATTR_ENTITY_ID: f"button.lte_{BUTTON_KEY_RESTART}"}, + {ATTR_ENTITY_ID: f"button.test_router_{BUTTON_KEY_RESTART}"}, blocking=True, ) await hass.async_block_till_done() - client.return_value.device.set_control.assert_called_with(ControlModeEnum.REBOOT) + client.device.set_control.assert_called_with(ControlModeEnum.REBOOT) - client.return_value.device.set_control.reset_mock() + client.device.set_control.reset_mock() await hass.services.async_call( DOMAIN, SERVICE_SUSPEND_INTEGRATION, @@ -74,4 +80,4 @@ async def test_restart(client, hass: HomeAssistant) -> None: blocking=True, ) await hass.async_block_till_done() - client.return_value.device.set_control.assert_not_called() + client.device.set_control.assert_not_called() diff --git a/tests/components/huawei_lte/test_diagnostics.py b/tests/components/huawei_lte/test_diagnostics.py index e63ba94e9be..33b48996443 100644 --- a/tests/components/huawei_lte/test_diagnostics.py +++ b/tests/components/huawei_lte/test_diagnostics.py @@ -9,30 +9,31 @@ from homeassistant.components.huawei_lte.const import DOMAIN from homeassistant.const import CONF_URL from homeassistant.core import HomeAssistant -from . import magic_client_full +from . import magic_client from tests.common import MockConfigEntry from tests.components.diagnostics import get_diagnostics_for_config_entry from tests.typing import ClientSessionGenerator -@patch("homeassistant.components.huawei_lte.Connection", MagicMock()) -@patch("homeassistant.components.huawei_lte.Client") async def test_entry_diagnostics( - client, hass: HomeAssistant, hass_client: ClientSessionGenerator, snapshot: SnapshotAssertion, ) -> None: """Test config entry diagnostics.""" - client.return_value = magic_client_full() huawei_lte = MockConfigEntry( domain=DOMAIN, data={CONF_URL: "http://huawei-lte.example.com"} ) huawei_lte.add_to_hass(hass) - await hass.config_entries.async_setup(huawei_lte.entry_id) + with ( + patch("homeassistant.components.huawei_lte.Connection", MagicMock()), + patch( + "homeassistant.components.huawei_lte.Client", return_value=magic_client() + ), + ): + await hass.config_entries.async_setup(huawei_lte.entry_id) await hass.async_block_till_done() result = await get_diagnostics_for_config_entry(hass, hass_client, huawei_lte) - assert result == snapshot(exclude=props("entry_id", "created_at", "modified_at")) diff --git a/tests/components/huawei_lte/test_select.py b/tests/components/huawei_lte/test_select.py index 85a0fcfdf0c..71a87e486ea 100644 --- a/tests/components/huawei_lte/test_select.py +++ b/tests/components/huawei_lte/test_select.py @@ -16,20 +16,23 @@ from . import magic_client from tests.common import MockConfigEntry -SELECT_NETWORK_MODE = "select.lte_preferred_network_mode" +SELECT_NETWORK_MODE = "select.test_router_preferred_network_mode" -@patch("homeassistant.components.huawei_lte.Connection", MagicMock()) -@patch("homeassistant.components.huawei_lte.Client") -async def test_set_net_mode(client, hass: HomeAssistant) -> None: +async def test_set_net_mode(hass: HomeAssistant) -> None: """Test setting network mode.""" - client.return_value = magic_client({}) huawei_lte = MockConfigEntry( domain=DOMAIN, data={CONF_URL: "http://huawei-lte.example.com"} ) huawei_lte.add_to_hass(hass) - await hass.config_entries.async_setup(huawei_lte.entry_id) + client = magic_client() + with ( + patch("homeassistant.components.huawei_lte.Connection", MagicMock()), + patch("homeassistant.components.huawei_lte.Client", return_value=client), + ): + await hass.config_entries.async_setup(huawei_lte.entry_id) await hass.async_block_till_done() + await hass.services.async_call( SELECT_DOMAIN, SERVICE_SELECT_OPTION, @@ -40,7 +43,7 @@ async def test_set_net_mode(client, hass: HomeAssistant) -> None: blocking=True, ) await hass.async_block_till_done() - client.return_value.net.set_net_mode.assert_called_once() - client.return_value.net.set_net_mode.assert_called_with( + client.net.set_net_mode.assert_called_once() + client.net.set_net_mode.assert_called_with( LTEBandEnum.ALL, NetworkBandEnum.ALL, NetworkModeEnum.MODE_4G_3G_AUTO.value ) diff --git a/tests/components/huawei_lte/test_switches.py b/tests/components/huawei_lte/test_switch.py similarity index 53% rename from tests/components/huawei_lte/test_switches.py rename to tests/components/huawei_lte/test_switch.py index 288416c8c99..8e595d1dab6 100644 --- a/tests/components/huawei_lte/test_switches.py +++ b/tests/components/huawei_lte/test_switch.py @@ -16,55 +16,62 @@ from . import magic_client from tests.common import MockConfigEntry -SWITCH_WIFI_GUEST_NETWORK = "switch.lte_wi_fi_guest_network" +SWITCH_WIFI_GUEST_NETWORK = "switch.test_router_wi_fi_guest_network" -@patch("homeassistant.components.huawei_lte.Connection", MagicMock()) -@patch("homeassistant.components.huawei_lte.Client", return_value=magic_client({})) async def test_huawei_lte_wifi_guest_network_config_entry_when_network_is_not_present( - client, - hass: HomeAssistant, - entity_registry: er.EntityRegistry, + hass: HomeAssistant, entity_registry: er.EntityRegistry ) -> None: """Test switch wifi guest network config entry when network is not present.""" huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: "http://huawei-lte"}) huawei_lte.add_to_hass(hass) - await hass.config_entries.async_setup(huawei_lte.entry_id) + with ( + patch("homeassistant.components.huawei_lte.Connection", MagicMock()), + patch( + "homeassistant.components.huawei_lte.Client", return_value=magic_client() + ), + ): + await hass.config_entries.async_setup(huawei_lte.entry_id) await hass.async_block_till_done() + assert not entity_registry.async_is_registered(SWITCH_WIFI_GUEST_NETWORK) -@patch("homeassistant.components.huawei_lte.Connection", MagicMock()) -@patch( - "homeassistant.components.huawei_lte.Client", - return_value=magic_client( - {"Ssids": {"Ssid": [{"wifiisguestnetwork": "1", "WifiEnable": "0"}]}} - ), -) async def test_huawei_lte_wifi_guest_network_config_entry_when_network_is_present( - client, - hass: HomeAssistant, - entity_registry: er.EntityRegistry, + hass: HomeAssistant, entity_registry: er.EntityRegistry ) -> None: """Test switch wifi guest network config entry when network is present.""" huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: "http://huawei-lte"}) huawei_lte.add_to_hass(hass) - await hass.config_entries.async_setup(huawei_lte.entry_id) + client = magic_client() + client.wlan.multi_basic_settings.return_value = { + "Ssids": {"Ssid": [{"wifiisguestnetwork": "1", "WifiEnable": "0"}]} + } + with ( + patch("homeassistant.components.huawei_lte.Connection", MagicMock()), + patch("homeassistant.components.huawei_lte.Client", return_value=client), + ): + await hass.config_entries.async_setup(huawei_lte.entry_id) await hass.async_block_till_done() + assert entity_registry.async_is_registered(SWITCH_WIFI_GUEST_NETWORK) -@patch("homeassistant.components.huawei_lte.Connection", MagicMock()) -@patch("homeassistant.components.huawei_lte.Client") -async def test_turn_on_switch_wifi_guest_network(client, hass: HomeAssistant) -> None: +async def test_turn_on_switch_wifi_guest_network(hass: HomeAssistant) -> None: """Test switch wifi guest network turn on method.""" - client.return_value = magic_client( - {"Ssids": {"Ssid": [{"wifiisguestnetwork": "1", "WifiEnable": "0"}]}} - ) huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: "http://huawei-lte"}) huawei_lte.add_to_hass(hass) - await hass.config_entries.async_setup(huawei_lte.entry_id) + client = magic_client() + client.wlan.multi_basic_settings.return_value = { + "Ssids": {"Ssid": [{"wifiisguestnetwork": "1", "WifiEnable": "0"}]} + } + with ( + patch("homeassistant.components.huawei_lte.Connection", MagicMock()), + patch("homeassistant.components.huawei_lte.Client", return_value=client), + ): + await hass.config_entries.async_setup(huawei_lte.entry_id) await hass.async_block_till_done() + await hass.services.async_call( SWITCH_DOMAIN, SERVICE_TURN_ON, @@ -73,20 +80,24 @@ async def test_turn_on_switch_wifi_guest_network(client, hass: HomeAssistant) -> ) await hass.async_block_till_done() assert hass.states.is_state(SWITCH_WIFI_GUEST_NETWORK, STATE_ON) - client.return_value.wlan.wifi_guest_network_switch.assert_called_once_with(True) + client.wlan.wifi_guest_network_switch.assert_called_once_with(True) -@patch("homeassistant.components.huawei_lte.Connection", MagicMock()) -@patch("homeassistant.components.huawei_lte.Client") -async def test_turn_off_switch_wifi_guest_network(client, hass: HomeAssistant) -> None: +async def test_turn_off_switch_wifi_guest_network(hass: HomeAssistant) -> None: """Test switch wifi guest network turn off method.""" - client.return_value = magic_client( - {"Ssids": {"Ssid": [{"wifiisguestnetwork": "1", "WifiEnable": "1"}]}} - ) huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: "http://huawei-lte"}) huawei_lte.add_to_hass(hass) - await hass.config_entries.async_setup(huawei_lte.entry_id) + client = magic_client() + client.wlan.multi_basic_settings.return_value = { + "Ssids": {"Ssid": [{"wifiisguestnetwork": "1", "WifiEnable": "1"}]} + } + with ( + patch("homeassistant.components.huawei_lte.Connection", MagicMock()), + patch("homeassistant.components.huawei_lte.Client", return_value=client), + ): + await hass.config_entries.async_setup(huawei_lte.entry_id) await hass.async_block_till_done() + await hass.services.async_call( SWITCH_DOMAIN, SERVICE_TURN_OFF, @@ -95,18 +106,11 @@ async def test_turn_off_switch_wifi_guest_network(client, hass: HomeAssistant) - ) await hass.async_block_till_done() assert hass.states.is_state(SWITCH_WIFI_GUEST_NETWORK, STATE_OFF) - client.return_value.wlan.wifi_guest_network_switch.assert_called_with(False) + client.wlan.wifi_guest_network_switch.assert_called_with(False) -@patch("homeassistant.components.huawei_lte.Connection", MagicMock()) -@patch( - "homeassistant.components.huawei_lte.Client", - return_value=magic_client({"Ssids": {"Ssid": "str"}}), -) async def test_huawei_lte_wifi_guest_network_config_entry_when_ssid_is_str( - client, - hass: HomeAssistant, - entity_registry: er.EntityRegistry, + hass: HomeAssistant, entity_registry: er.EntityRegistry ) -> None: """Test switch wifi guest network config entry when ssid is a str. @@ -114,20 +118,20 @@ async def test_huawei_lte_wifi_guest_network_config_entry_when_ssid_is_str( """ huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: "http://huawei-lte"}) huawei_lte.add_to_hass(hass) - await hass.config_entries.async_setup(huawei_lte.entry_id) + client = magic_client() + client.wlan.multi_basic_settings.return_value = {"Ssids": {"Ssid": "str"}} + with ( + patch("homeassistant.components.huawei_lte.Connection", MagicMock()), + patch("homeassistant.components.huawei_lte.Client", return_value=client), + ): + await hass.config_entries.async_setup(huawei_lte.entry_id) await hass.async_block_till_done() + assert not entity_registry.async_is_registered(SWITCH_WIFI_GUEST_NETWORK) -@patch("homeassistant.components.huawei_lte.Connection", MagicMock()) -@patch( - "homeassistant.components.huawei_lte.Client", - return_value=magic_client({"Ssids": {"Ssid": None}}), -) async def test_huawei_lte_wifi_guest_network_config_entry_when_ssid_is_none( - client, - hass: HomeAssistant, - entity_registry: er.EntityRegistry, + hass: HomeAssistant, entity_registry: er.EntityRegistry ) -> None: """Test switch wifi guest network config entry when ssid is a None. @@ -135,6 +139,13 @@ async def test_huawei_lte_wifi_guest_network_config_entry_when_ssid_is_none( """ huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: "http://huawei-lte"}) huawei_lte.add_to_hass(hass) - await hass.config_entries.async_setup(huawei_lte.entry_id) + client = magic_client() + client.wlan.multi_basic_settings.return_value = {"Ssids": {"Ssid": None}} + with ( + patch("homeassistant.components.huawei_lte.Connection", MagicMock()), + patch("homeassistant.components.huawei_lte.Client", return_value=client), + ): + await hass.config_entries.async_setup(huawei_lte.entry_id) await hass.async_block_till_done() + assert not entity_registry.async_is_registered(SWITCH_WIFI_GUEST_NETWORK)