1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-15 07:36:16 +00:00

huawei_lte test cleanups (#154961)

This commit is contained in:
Ville Skyttä
2025-10-22 23:36:48 +03:00
committed by GitHub
parent 4774ed508a
commit e90fe96b4e
6 changed files with 107 additions and 104 deletions

View File

@@ -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

View File

@@ -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",

View File

@@ -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()

View File

@@ -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"))

View File

@@ -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
)

View File

@@ -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)