Fix failing UniFi tests related to utcnow (#119131)

* test

* Fix missed test
This commit is contained in:
Robert Svensson 2024-06-08 17:58:47 +02:00 committed by GitHub
parent c49ca5ed56
commit 915658daa1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 92 additions and 115 deletions

View File

@ -704,10 +704,19 @@ async def test_option_track_devices(
assert hass.states.get("device_tracker.device") assert hass.states.get("device_tracker.device")
@pytest.mark.parametrize( @pytest.mark.usefixtures("mock_device_registry")
"client_payload", async def test_option_ssid_filter(
[ hass: HomeAssistant,
[ mock_unifi_websocket,
config_entry_factory: Callable[[], ConfigEntry],
client_payload: list[dict[str, Any]],
) -> None:
"""Test the SSID filter works.
Client will travel from a supported SSID to an unsupported ssid.
Client on SSID2 will be removed on change of options.
"""
client_payload += [
{ {
"essid": "ssid", "essid": "ssid",
"hostname": "client", "hostname": "client",
@ -723,27 +732,15 @@ async def test_option_track_devices(
"mac": "00:00:00:00:00:02", "mac": "00:00:00:00:00:02",
}, },
] ]
], config_entry = await config_entry_factory()
)
@pytest.mark.usefixtures("mock_device_registry")
async def test_option_ssid_filter(
hass: HomeAssistant,
mock_unifi_websocket,
config_entry_setup: ConfigEntry,
client_payload: list[dict[str, Any]],
) -> None:
"""Test the SSID filter works.
Client will travel from a supported SSID to an unsupported ssid.
Client on SSID2 will be removed on change of options.
"""
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2 assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2
assert hass.states.get("device_tracker.client").state == STATE_HOME assert hass.states.get("device_tracker.client").state == STATE_HOME
assert hass.states.get("device_tracker.client_on_ssid2").state == STATE_NOT_HOME assert hass.states.get("device_tracker.client_on_ssid2").state == STATE_NOT_HOME
# Setting SSID filter will remove clients outside of filter # Setting SSID filter will remove clients outside of filter
hass.config_entries.async_update_entry( hass.config_entries.async_update_entry(
config_entry_setup, options={CONF_SSID_FILTER: ["ssid"]} config_entry, options={CONF_SSID_FILTER: ["ssid"]}
) )
await hass.async_block_till_done() await hass.async_block_till_done()
@ -766,8 +763,7 @@ async def test_option_ssid_filter(
new_time = dt_util.utcnow() + timedelta( new_time = dt_util.utcnow() + timedelta(
seconds=( seconds=(
config_entry_setup.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME) config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME) + 1
+ 1
) )
) )
with freeze_time(new_time): with freeze_time(new_time):
@ -781,9 +777,7 @@ async def test_option_ssid_filter(
assert not hass.states.get("device_tracker.client_on_ssid2") assert not hass.states.get("device_tracker.client_on_ssid2")
# Remove SSID filter # Remove SSID filter
hass.config_entries.async_update_entry( hass.config_entries.async_update_entry(config_entry, options={CONF_SSID_FILTER: []})
config_entry_setup, options={CONF_SSID_FILTER: []}
)
await hass.async_block_till_done() await hass.async_block_till_done()
client["last_seen"] += 1 client["last_seen"] += 1
@ -797,8 +791,7 @@ async def test_option_ssid_filter(
# Time pass to mark client as away # Time pass to mark client as away
new_time += timedelta( new_time += timedelta(
seconds=( seconds=(
config_entry_setup.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME) config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME) + 1
+ 1
) )
) )
with freeze_time(new_time): with freeze_time(new_time):
@ -820,9 +813,7 @@ async def test_option_ssid_filter(
await hass.async_block_till_done() await hass.async_block_till_done()
new_time += timedelta( new_time += timedelta(
seconds=( seconds=(config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME))
config_entry_setup.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME)
)
) )
with freeze_time(new_time): with freeze_time(new_time):
async_fire_time_changed(hass, new_time) async_fire_time_changed(hass, new_time)
@ -831,10 +822,18 @@ async def test_option_ssid_filter(
assert hass.states.get("device_tracker.client_on_ssid2").state == STATE_NOT_HOME assert hass.states.get("device_tracker.client_on_ssid2").state == STATE_NOT_HOME
@pytest.mark.parametrize( @pytest.mark.usefixtures("mock_device_registry")
"client_payload", async def test_wireless_client_go_wired_issue(
[ hass: HomeAssistant,
[ mock_unifi_websocket,
config_entry_factory: Callable[[], ConfigEntry],
client_payload: list[dict[str, Any]],
) -> None:
"""Test the solution to catch wireless device go wired UniFi issue.
UniFi Network has a known issue that when a wireless device goes away it sometimes gets marked as wired.
"""
client_payload.append(
{ {
"essid": "ssid", "essid": "ssid",
"hostname": "client", "hostname": "client",
@ -843,20 +842,9 @@ async def test_option_ssid_filter(
"last_seen": dt_util.as_timestamp(dt_util.utcnow()), "last_seen": dt_util.as_timestamp(dt_util.utcnow()),
"mac": "00:00:00:00:00:01", "mac": "00:00:00:00:00:01",
} }
]
],
) )
@pytest.mark.usefixtures("mock_device_registry") config_entry = await config_entry_factory()
async def test_wireless_client_go_wired_issue(
hass: HomeAssistant,
mock_unifi_websocket,
config_entry_setup: ConfigEntry,
client_payload: list[dict[str, Any]],
) -> None:
"""Test the solution to catch wireless device go wired UniFi issue.
UniFi Network has a known issue that when a wireless device goes away it sometimes gets marked as wired.
"""
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1 assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
# Client is wireless # Client is wireless
@ -876,9 +864,7 @@ async def test_wireless_client_go_wired_issue(
# Pass time # Pass time
new_time = dt_util.utcnow() + timedelta( new_time = dt_util.utcnow() + timedelta(
seconds=( seconds=(config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME))
config_entry_setup.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME)
)
) )
with freeze_time(new_time): with freeze_time(new_time):
async_fire_time_changed(hass, new_time) async_fire_time_changed(hass, new_time)
@ -909,10 +895,15 @@ async def test_wireless_client_go_wired_issue(
@pytest.mark.parametrize("config_entry_options", [{CONF_IGNORE_WIRED_BUG: True}]) @pytest.mark.parametrize("config_entry_options", [{CONF_IGNORE_WIRED_BUG: True}])
@pytest.mark.parametrize( @pytest.mark.usefixtures("mock_device_registry")
"client_payload", async def test_option_ignore_wired_bug(
[ hass: HomeAssistant,
[ mock_unifi_websocket,
config_entry_factory: Callable[[], ConfigEntry],
client_payload: list[dict[str, Any]],
) -> None:
"""Test option to ignore wired bug."""
client_payload.append(
{ {
"ap_mac": "00:00:00:00:02:01", "ap_mac": "00:00:00:00:02:01",
"essid": "ssid", "essid": "ssid",
@ -922,17 +913,9 @@ async def test_wireless_client_go_wired_issue(
"last_seen": dt_util.as_timestamp(dt_util.utcnow()), "last_seen": dt_util.as_timestamp(dt_util.utcnow()),
"mac": "00:00:00:00:00:01", "mac": "00:00:00:00:00:01",
} }
]
],
) )
@pytest.mark.usefixtures("mock_device_registry") config_entry = await config_entry_factory()
async def test_option_ignore_wired_bug(
hass: HomeAssistant,
mock_unifi_websocket,
config_entry_setup: ConfigEntry,
client_payload: list[dict[str, Any]],
) -> None:
"""Test option to ignore wired bug."""
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1 assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
# Client is wireless # Client is wireless
@ -951,9 +934,7 @@ async def test_option_ignore_wired_bug(
# pass time # pass time
new_time = dt_util.utcnow() + timedelta( new_time = dt_util.utcnow() + timedelta(
seconds=config_entry_setup.options.get( seconds=config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME)
CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME
)
) )
with freeze_time(new_time): with freeze_time(new_time):
async_fire_time_changed(hass, new_time) async_fire_time_changed(hass, new_time)

View File

@ -4,6 +4,7 @@ from collections.abc import Callable
from copy import deepcopy from copy import deepcopy
from datetime import timedelta from datetime import timedelta
from http import HTTPStatus from http import HTTPStatus
from typing import Any
from unittest.mock import patch from unittest.mock import patch
import aiounifi import aiounifi
@ -313,27 +314,25 @@ async def test_reset_fails(
assert config_entry.state is ConfigEntryState.LOADED assert config_entry.state is ConfigEntryState.LOADED
@pytest.mark.parametrize( async def test_connection_state_signalling(
"client_payload", hass: HomeAssistant,
[ mock_device_registry,
[ websocket_mock,
config_entry_factory: Callable[[], ConfigEntry],
client_payload: list[dict[str, Any]],
) -> None:
"""Verify connection statesignalling and connection state are working."""
client_payload.append(
{ {
"hostname": "client", "hostname": "client",
"ip": "10.0.0.1", "ip": "10.0.0.1",
"is_wired": True, "is_wired": True,
"last_seen": dt_util.as_timestamp(dt_util.utcnow()), "last_seen": dt_util.as_timestamp(dt_util.utcnow()),
"mac": "00:00:00:00:00:01", "mac": "00:00:00:00:00:01",
}, }
]
],
) )
async def test_connection_state_signalling( await config_entry_factory()
hass: HomeAssistant,
mock_device_registry,
config_entry_setup: ConfigEntry,
websocket_mock,
) -> None:
"""Verify connection statesignalling and connection state are working."""
# Controller is connected # Controller is connected
assert hass.states.get("device_tracker.client").state == "home" assert hass.states.get("device_tracker.client").state == "home"

View File

@ -682,10 +682,17 @@ async def test_poe_port_switches(
assert hass.states.get("sensor.mock_name_port_1_poe_power") assert hass.states.get("sensor.mock_name_port_1_poe_power")
@pytest.mark.parametrize( @pytest.mark.parametrize("wlan_payload", [[WLAN]])
"client_payload", async def test_wlan_client_sensors(
[ hass: HomeAssistant,
[ entity_registry: er.EntityRegistry,
mock_unifi_websocket,
websocket_mock,
config_entry_factory: Callable[[], ConfigEntry],
client_payload: list[dict[str, Any]],
) -> None:
"""Verify that WLAN client sensors are working as expected."""
client_payload += [
{ {
"essid": "SSID 1", "essid": "SSID 1",
"is_wired": False, "is_wired": False,
@ -707,18 +714,8 @@ async def test_poe_port_switches(
"tx_bytes-r": 6789000000, "tx_bytes-r": 6789000000,
}, },
] ]
], await config_entry_factory()
)
@pytest.mark.parametrize("wlan_payload", [[WLAN]])
@pytest.mark.usefixtures("config_entry_setup")
async def test_wlan_client_sensors(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_unifi_websocket,
websocket_mock,
client_payload: list[dict[str, Any]],
) -> None:
"""Verify that WLAN client sensors are working as expected."""
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 1 assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 1
ent_reg_entry = entity_registry.async_get("sensor.ssid_1") ent_reg_entry = entity_registry.async_get("sensor.ssid_1")