Skip to content

Commit e90fe96

Browse files
authored
huawei_lte test cleanups (#154961)
1 parent 4774ed5 commit e90fe96

File tree

6 files changed

+107
-104
lines changed

6 files changed

+107
-104
lines changed

homeassistant/components/huawei_lte/quality_scale.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ rules:
4141
reauthentication-flow: done
4242
test-coverage:
4343
status: todo
44-
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.
44+
comment: Get percentage up there, add missing actual action press invocations in button tests' suspended state tests.
4545

4646
# Gold
4747
devices: done

tests/components/huawei_lte/__init__.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,8 @@
55
from huawei_lte_api.enums.cradle import ConnectionStatusEnum
66

77

8-
def magic_client(multi_basic_settings_value: dict) -> MagicMock:
9-
"""Mock huawei_lte.Client."""
10-
information = MagicMock(return_value={"SerialNumber": "test-serial-number"})
11-
check_notifications = MagicMock(return_value={"SmsStorageFull": 0})
12-
status = MagicMock(
13-
return_value={"ConnectionStatus": ConnectionStatusEnum.CONNECTED.value}
14-
)
15-
multi_basic_settings = MagicMock(return_value=multi_basic_settings_value)
16-
wifi_feature_switch = MagicMock(return_value={"wifi24g_switch_enable": 1})
17-
device = MagicMock(information=information)
18-
monitoring = MagicMock(check_notifications=check_notifications, status=status)
19-
wlan = MagicMock(
20-
multi_basic_settings=multi_basic_settings,
21-
wifi_feature_switch=wifi_feature_switch,
22-
)
23-
return MagicMock(device=device, monitoring=monitoring, wlan=wlan)
24-
25-
26-
def magic_client_full() -> MagicMock:
27-
"""Extended mock for huawei_lte.Client with all API methods."""
8+
def magic_client() -> MagicMock:
9+
"""Mock huawei_lte.Client with all API methods."""
2810
information = MagicMock(
2911
return_value={
3012
"DeviceName": "Test Router",
@@ -121,7 +103,7 @@ def magic_client_full() -> MagicMock:
121103
)
122104
status = MagicMock(
123105
return_value={
124-
"ConnectionStatus": "901",
106+
"ConnectionStatus": str(ConnectionStatusEnum.CONNECTED.value),
125107
"WifiConnectionStatus": None,
126108
"SignalStrength": None,
127109
"SignalIcon": "5",

tests/components/huawei_lte/test_button.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,56 +22,62 @@
2222

2323

2424
@patch("homeassistant.components.huawei_lte.Connection", MagicMock())
25-
@patch("homeassistant.components.huawei_lte.Client", return_value=magic_client({}))
26-
async def test_clear_traffic_statistics(client, hass: HomeAssistant) -> None:
25+
async def test_clear_traffic_statistics(hass: HomeAssistant) -> None:
2726
"""Test clear traffic statistics button."""
2827
huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: MOCK_CONF_URL})
2928
huawei_lte.add_to_hass(hass)
30-
await hass.config_entries.async_setup(huawei_lte.entry_id)
29+
client = magic_client()
30+
with patch("homeassistant.components.huawei_lte.Client", return_value=client):
31+
await hass.config_entries.async_setup(huawei_lte.entry_id)
3132
await hass.async_block_till_done()
33+
3234
await hass.services.async_call(
3335
BUTTON_DOMAIN,
3436
SERVICE_PRESS,
35-
{ATTR_ENTITY_ID: f"button.lte_{BUTTON_KEY_CLEAR_TRAFFIC_STATISTICS}"},
37+
{ATTR_ENTITY_ID: f"button.test_router_{BUTTON_KEY_CLEAR_TRAFFIC_STATISTICS}"},
3638
blocking=True,
3739
)
3840
await hass.async_block_till_done()
39-
client.return_value.monitoring.set_clear_traffic.assert_called_once()
41+
client.monitoring.set_clear_traffic.assert_called_once()
4042

41-
client.return_value.monitoring.set_clear_traffic.reset_mock()
43+
client.monitoring.set_clear_traffic.reset_mock()
4244
await hass.services.async_call(
4345
DOMAIN,
4446
SERVICE_SUSPEND_INTEGRATION,
4547
{CONF_URL: MOCK_CONF_URL},
4648
blocking=True,
4749
)
4850
await hass.async_block_till_done()
49-
client.return_value.monitoring.set_clear_traffic.assert_not_called()
51+
client.monitoring.set_clear_traffic.assert_not_called()
5052

5153

52-
@patch("homeassistant.components.huawei_lte.Connection", MagicMock())
53-
@patch("homeassistant.components.huawei_lte.Client", return_value=magic_client({}))
54-
async def test_restart(client, hass: HomeAssistant) -> None:
54+
async def test_restart(hass: HomeAssistant) -> None:
5555
"""Test restart button."""
5656
huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: MOCK_CONF_URL})
5757
huawei_lte.add_to_hass(hass)
58-
await hass.config_entries.async_setup(huawei_lte.entry_id)
58+
client = magic_client()
59+
with (
60+
patch("homeassistant.components.huawei_lte.Connection", MagicMock()),
61+
patch("homeassistant.components.huawei_lte.Client", return_value=client),
62+
):
63+
await hass.config_entries.async_setup(huawei_lte.entry_id)
5964
await hass.async_block_till_done()
65+
6066
await hass.services.async_call(
6167
BUTTON_DOMAIN,
6268
SERVICE_PRESS,
63-
{ATTR_ENTITY_ID: f"button.lte_{BUTTON_KEY_RESTART}"},
69+
{ATTR_ENTITY_ID: f"button.test_router_{BUTTON_KEY_RESTART}"},
6470
blocking=True,
6571
)
6672
await hass.async_block_till_done()
67-
client.return_value.device.set_control.assert_called_with(ControlModeEnum.REBOOT)
73+
client.device.set_control.assert_called_with(ControlModeEnum.REBOOT)
6874

69-
client.return_value.device.set_control.reset_mock()
75+
client.device.set_control.reset_mock()
7076
await hass.services.async_call(
7177
DOMAIN,
7278
SERVICE_SUSPEND_INTEGRATION,
7379
{CONF_URL: MOCK_CONF_URL},
7480
blocking=True,
7581
)
7682
await hass.async_block_till_done()
77-
client.return_value.device.set_control.assert_not_called()
83+
client.device.set_control.assert_not_called()

tests/components/huawei_lte/test_diagnostics.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,31 @@
99
from homeassistant.const import CONF_URL
1010
from homeassistant.core import HomeAssistant
1111

12-
from . import magic_client_full
12+
from . import magic_client
1313

1414
from tests.common import MockConfigEntry
1515
from tests.components.diagnostics import get_diagnostics_for_config_entry
1616
from tests.typing import ClientSessionGenerator
1717

1818

19-
@patch("homeassistant.components.huawei_lte.Connection", MagicMock())
20-
@patch("homeassistant.components.huawei_lte.Client")
2119
async def test_entry_diagnostics(
22-
client,
2320
hass: HomeAssistant,
2421
hass_client: ClientSessionGenerator,
2522
snapshot: SnapshotAssertion,
2623
) -> None:
2724
"""Test config entry diagnostics."""
28-
client.return_value = magic_client_full()
2925
huawei_lte = MockConfigEntry(
3026
domain=DOMAIN, data={CONF_URL: "http://huawei-lte.example.com"}
3127
)
3228
huawei_lte.add_to_hass(hass)
33-
await hass.config_entries.async_setup(huawei_lte.entry_id)
29+
with (
30+
patch("homeassistant.components.huawei_lte.Connection", MagicMock()),
31+
patch(
32+
"homeassistant.components.huawei_lte.Client", return_value=magic_client()
33+
),
34+
):
35+
await hass.config_entries.async_setup(huawei_lte.entry_id)
3436
await hass.async_block_till_done()
3537

3638
result = await get_diagnostics_for_config_entry(hass, hass_client, huawei_lte)
37-
3839
assert result == snapshot(exclude=props("entry_id", "created_at", "modified_at"))

tests/components/huawei_lte/test_select.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@
1616

1717
from tests.common import MockConfigEntry
1818

19-
SELECT_NETWORK_MODE = "select.lte_preferred_network_mode"
19+
SELECT_NETWORK_MODE = "select.test_router_preferred_network_mode"
2020

2121

22-
@patch("homeassistant.components.huawei_lte.Connection", MagicMock())
23-
@patch("homeassistant.components.huawei_lte.Client")
24-
async def test_set_net_mode(client, hass: HomeAssistant) -> None:
22+
async def test_set_net_mode(hass: HomeAssistant) -> None:
2523
"""Test setting network mode."""
26-
client.return_value = magic_client({})
2724
huawei_lte = MockConfigEntry(
2825
domain=DOMAIN, data={CONF_URL: "http://huawei-lte.example.com"}
2926
)
3027
huawei_lte.add_to_hass(hass)
31-
await hass.config_entries.async_setup(huawei_lte.entry_id)
28+
client = magic_client()
29+
with (
30+
patch("homeassistant.components.huawei_lte.Connection", MagicMock()),
31+
patch("homeassistant.components.huawei_lte.Client", return_value=client),
32+
):
33+
await hass.config_entries.async_setup(huawei_lte.entry_id)
3234
await hass.async_block_till_done()
35+
3336
await hass.services.async_call(
3437
SELECT_DOMAIN,
3538
SERVICE_SELECT_OPTION,
@@ -40,7 +43,7 @@ async def test_set_net_mode(client, hass: HomeAssistant) -> None:
4043
blocking=True,
4144
)
4245
await hass.async_block_till_done()
43-
client.return_value.net.set_net_mode.assert_called_once()
44-
client.return_value.net.set_net_mode.assert_called_with(
46+
client.net.set_net_mode.assert_called_once()
47+
client.net.set_net_mode.assert_called_with(
4548
LTEBandEnum.ALL, NetworkBandEnum.ALL, NetworkModeEnum.MODE_4G_3G_AUTO.value
4649
)

tests/components/huawei_lte/test_switches.py renamed to tests/components/huawei_lte/test_switch.py

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,55 +16,62 @@
1616

1717
from tests.common import MockConfigEntry
1818

19-
SWITCH_WIFI_GUEST_NETWORK = "switch.lte_wi_fi_guest_network"
19+
SWITCH_WIFI_GUEST_NETWORK = "switch.test_router_wi_fi_guest_network"
2020

2121

22-
@patch("homeassistant.components.huawei_lte.Connection", MagicMock())
23-
@patch("homeassistant.components.huawei_lte.Client", return_value=magic_client({}))
2422
async def test_huawei_lte_wifi_guest_network_config_entry_when_network_is_not_present(
25-
client,
26-
hass: HomeAssistant,
27-
entity_registry: er.EntityRegistry,
23+
hass: HomeAssistant, entity_registry: er.EntityRegistry
2824
) -> None:
2925
"""Test switch wifi guest network config entry when network is not present."""
3026
huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: "http://huawei-lte"})
3127
huawei_lte.add_to_hass(hass)
32-
await hass.config_entries.async_setup(huawei_lte.entry_id)
28+
with (
29+
patch("homeassistant.components.huawei_lte.Connection", MagicMock()),
30+
patch(
31+
"homeassistant.components.huawei_lte.Client", return_value=magic_client()
32+
),
33+
):
34+
await hass.config_entries.async_setup(huawei_lte.entry_id)
3335
await hass.async_block_till_done()
36+
3437
assert not entity_registry.async_is_registered(SWITCH_WIFI_GUEST_NETWORK)
3538

3639

37-
@patch("homeassistant.components.huawei_lte.Connection", MagicMock())
38-
@patch(
39-
"homeassistant.components.huawei_lte.Client",
40-
return_value=magic_client(
41-
{"Ssids": {"Ssid": [{"wifiisguestnetwork": "1", "WifiEnable": "0"}]}}
42-
),
43-
)
4440
async def test_huawei_lte_wifi_guest_network_config_entry_when_network_is_present(
45-
client,
46-
hass: HomeAssistant,
47-
entity_registry: er.EntityRegistry,
41+
hass: HomeAssistant, entity_registry: er.EntityRegistry
4842
) -> None:
4943
"""Test switch wifi guest network config entry when network is present."""
5044
huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: "http://huawei-lte"})
5145
huawei_lte.add_to_hass(hass)
52-
await hass.config_entries.async_setup(huawei_lte.entry_id)
46+
client = magic_client()
47+
client.wlan.multi_basic_settings.return_value = {
48+
"Ssids": {"Ssid": [{"wifiisguestnetwork": "1", "WifiEnable": "0"}]}
49+
}
50+
with (
51+
patch("homeassistant.components.huawei_lte.Connection", MagicMock()),
52+
patch("homeassistant.components.huawei_lte.Client", return_value=client),
53+
):
54+
await hass.config_entries.async_setup(huawei_lte.entry_id)
5355
await hass.async_block_till_done()
56+
5457
assert entity_registry.async_is_registered(SWITCH_WIFI_GUEST_NETWORK)
5558

5659

57-
@patch("homeassistant.components.huawei_lte.Connection", MagicMock())
58-
@patch("homeassistant.components.huawei_lte.Client")
59-
async def test_turn_on_switch_wifi_guest_network(client, hass: HomeAssistant) -> None:
60+
async def test_turn_on_switch_wifi_guest_network(hass: HomeAssistant) -> None:
6061
"""Test switch wifi guest network turn on method."""
61-
client.return_value = magic_client(
62-
{"Ssids": {"Ssid": [{"wifiisguestnetwork": "1", "WifiEnable": "0"}]}}
63-
)
6462
huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: "http://huawei-lte"})
6563
huawei_lte.add_to_hass(hass)
66-
await hass.config_entries.async_setup(huawei_lte.entry_id)
64+
client = magic_client()
65+
client.wlan.multi_basic_settings.return_value = {
66+
"Ssids": {"Ssid": [{"wifiisguestnetwork": "1", "WifiEnable": "0"}]}
67+
}
68+
with (
69+
patch("homeassistant.components.huawei_lte.Connection", MagicMock()),
70+
patch("homeassistant.components.huawei_lte.Client", return_value=client),
71+
):
72+
await hass.config_entries.async_setup(huawei_lte.entry_id)
6773
await hass.async_block_till_done()
74+
6875
await hass.services.async_call(
6976
SWITCH_DOMAIN,
7077
SERVICE_TURN_ON,
@@ -73,20 +80,24 @@ async def test_turn_on_switch_wifi_guest_network(client, hass: HomeAssistant) ->
7380
)
7481
await hass.async_block_till_done()
7582
assert hass.states.is_state(SWITCH_WIFI_GUEST_NETWORK, STATE_ON)
76-
client.return_value.wlan.wifi_guest_network_switch.assert_called_once_with(True)
83+
client.wlan.wifi_guest_network_switch.assert_called_once_with(True)
7784

7885

79-
@patch("homeassistant.components.huawei_lte.Connection", MagicMock())
80-
@patch("homeassistant.components.huawei_lte.Client")
81-
async def test_turn_off_switch_wifi_guest_network(client, hass: HomeAssistant) -> None:
86+
async def test_turn_off_switch_wifi_guest_network(hass: HomeAssistant) -> None:
8287
"""Test switch wifi guest network turn off method."""
83-
client.return_value = magic_client(
84-
{"Ssids": {"Ssid": [{"wifiisguestnetwork": "1", "WifiEnable": "1"}]}}
85-
)
8688
huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: "http://huawei-lte"})
8789
huawei_lte.add_to_hass(hass)
88-
await hass.config_entries.async_setup(huawei_lte.entry_id)
90+
client = magic_client()
91+
client.wlan.multi_basic_settings.return_value = {
92+
"Ssids": {"Ssid": [{"wifiisguestnetwork": "1", "WifiEnable": "1"}]}
93+
}
94+
with (
95+
patch("homeassistant.components.huawei_lte.Connection", MagicMock()),
96+
patch("homeassistant.components.huawei_lte.Client", return_value=client),
97+
):
98+
await hass.config_entries.async_setup(huawei_lte.entry_id)
8999
await hass.async_block_till_done()
100+
90101
await hass.services.async_call(
91102
SWITCH_DOMAIN,
92103
SERVICE_TURN_OFF,
@@ -95,46 +106,46 @@ async def test_turn_off_switch_wifi_guest_network(client, hass: HomeAssistant) -
95106
)
96107
await hass.async_block_till_done()
97108
assert hass.states.is_state(SWITCH_WIFI_GUEST_NETWORK, STATE_OFF)
98-
client.return_value.wlan.wifi_guest_network_switch.assert_called_with(False)
109+
client.wlan.wifi_guest_network_switch.assert_called_with(False)
99110

100111

101-
@patch("homeassistant.components.huawei_lte.Connection", MagicMock())
102-
@patch(
103-
"homeassistant.components.huawei_lte.Client",
104-
return_value=magic_client({"Ssids": {"Ssid": "str"}}),
105-
)
106112
async def test_huawei_lte_wifi_guest_network_config_entry_when_ssid_is_str(
107-
client,
108-
hass: HomeAssistant,
109-
entity_registry: er.EntityRegistry,
113+
hass: HomeAssistant, entity_registry: er.EntityRegistry
110114
) -> None:
111115
"""Test switch wifi guest network config entry when ssid is a str.
112116
113117
Issue #76244. Huawai models: H312-371, E5372 and E8372.
114118
"""
115119
huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: "http://huawei-lte"})
116120
huawei_lte.add_to_hass(hass)
117-
await hass.config_entries.async_setup(huawei_lte.entry_id)
121+
client = magic_client()
122+
client.wlan.multi_basic_settings.return_value = {"Ssids": {"Ssid": "str"}}
123+
with (
124+
patch("homeassistant.components.huawei_lte.Connection", MagicMock()),
125+
patch("homeassistant.components.huawei_lte.Client", return_value=client),
126+
):
127+
await hass.config_entries.async_setup(huawei_lte.entry_id)
118128
await hass.async_block_till_done()
129+
119130
assert not entity_registry.async_is_registered(SWITCH_WIFI_GUEST_NETWORK)
120131

121132

122-
@patch("homeassistant.components.huawei_lte.Connection", MagicMock())
123-
@patch(
124-
"homeassistant.components.huawei_lte.Client",
125-
return_value=magic_client({"Ssids": {"Ssid": None}}),
126-
)
127133
async def test_huawei_lte_wifi_guest_network_config_entry_when_ssid_is_none(
128-
client,
129-
hass: HomeAssistant,
130-
entity_registry: er.EntityRegistry,
134+
hass: HomeAssistant, entity_registry: er.EntityRegistry
131135
) -> None:
132136
"""Test switch wifi guest network config entry when ssid is a None.
133137
134138
Issue #76244.
135139
"""
136140
huawei_lte = MockConfigEntry(domain=DOMAIN, data={CONF_URL: "http://huawei-lte"})
137141
huawei_lte.add_to_hass(hass)
138-
await hass.config_entries.async_setup(huawei_lte.entry_id)
142+
client = magic_client()
143+
client.wlan.multi_basic_settings.return_value = {"Ssids": {"Ssid": None}}
144+
with (
145+
patch("homeassistant.components.huawei_lte.Connection", MagicMock()),
146+
patch("homeassistant.components.huawei_lte.Client", return_value=client),
147+
):
148+
await hass.config_entries.async_setup(huawei_lte.entry_id)
139149
await hass.async_block_till_done()
150+
140151
assert not entity_registry.async_is_registered(SWITCH_WIFI_GUEST_NETWORK)

0 commit comments

Comments
 (0)