1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Improve UniFi tests (#45871)

This commit is contained in:
Robert Svensson
2021-02-05 16:31:47 +01:00
committed by GitHub
parent 7144c5f316
commit ae2c7e4c74
6 changed files with 315 additions and 274 deletions

View File

@@ -25,7 +25,6 @@ from homeassistant.components.unifi.const import (
)
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.helpers import entity_registry
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from .test_controller import ENTRY_CONFIG, setup_unifi_integration
@@ -151,27 +150,19 @@ EVENT_DEVICE_2_UPGRADED = {
}
async def test_platform_manually_configured(hass):
"""Test that nothing happens when configuring unifi through device tracker platform."""
assert (
await async_setup_component(
hass, TRACKER_DOMAIN, {TRACKER_DOMAIN: {"platform": UNIFI_DOMAIN}}
)
is False
)
assert UNIFI_DOMAIN not in hass.data
async def test_no_clients(hass):
async def test_no_clients(hass, aioclient_mock):
"""Test the update_clients function when no clients are found."""
await setup_unifi_integration(hass)
await setup_unifi_integration(hass, aioclient_mock)
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 0
async def test_tracked_wireless_clients(hass):
async def test_tracked_wireless_clients(hass, aioclient_mock):
"""Test the update_items function with some clients."""
controller = await setup_unifi_integration(hass, clients_response=[CLIENT_1])
config_entry = await setup_unifi_integration(
hass, aioclient_mock, clients_response=[CLIENT_1]
)
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
client_1 = hass.states.get("device_tracker.client_1")
@@ -224,17 +215,19 @@ async def test_tracked_wireless_clients(hass):
assert client_1.state == "home"
async def test_tracked_clients(hass):
async def test_tracked_clients(hass, aioclient_mock):
"""Test the update_items function with some clients."""
client_4_copy = copy(CLIENT_4)
client_4_copy["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
controller = await setup_unifi_integration(
config_entry = await setup_unifi_integration(
hass,
aioclient_mock,
options={CONF_SSID_FILTER: ["ssid"]},
clients_response=[CLIENT_1, CLIENT_2, CLIENT_3, CLIENT_5, client_4_copy],
known_wireless_clients=(CLIENT_4["mac"],),
)
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 4
client_1 = hass.states.get("device_tracker.client_1")
@@ -269,12 +262,14 @@ async def test_tracked_clients(hass):
assert client_1.state == "home"
async def test_tracked_devices(hass):
async def test_tracked_devices(hass, aioclient_mock):
"""Test the update_items function with some devices."""
controller = await setup_unifi_integration(
config_entry = await setup_unifi_integration(
hass,
aioclient_mock,
devices_response=[DEVICE_1, DEVICE_2],
)
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2
device_1 = hass.states.get("device_tracker.device_1")
@@ -338,11 +333,12 @@ async def test_tracked_devices(hass):
assert device.sw_version == EVENT_DEVICE_2_UPGRADED["version_to"]
async def test_remove_clients(hass):
async def test_remove_clients(hass, aioclient_mock):
"""Test the remove_items function with some clients."""
controller = await setup_unifi_integration(
hass, clients_response=[CLIENT_1, CLIENT_2]
config_entry = await setup_unifi_integration(
hass, aioclient_mock, clients_response=[CLIENT_1, CLIENT_2]
)
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2
client_1 = hass.states.get("device_tracker.client_1")
@@ -367,13 +363,15 @@ async def test_remove_clients(hass):
assert wired_client is not None
async def test_controller_state_change(hass):
async def test_controller_state_change(hass, aioclient_mock):
"""Verify entities state reflect on controller becoming unavailable."""
controller = await setup_unifi_integration(
config_entry = await setup_unifi_integration(
hass,
aioclient_mock,
clients_response=[CLIENT_1],
devices_response=[DEVICE_1],
)
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2
client_1 = hass.states.get("device_tracker.client_1")
@@ -405,10 +403,11 @@ async def test_controller_state_change(hass):
assert device_1.state == "home"
async def test_option_track_clients(hass):
async def test_option_track_clients(hass, aioclient_mock):
"""Test the tracking of clients can be turned off."""
controller = await setup_unifi_integration(
config_entry = await setup_unifi_integration(
hass,
aioclient_mock,
clients_response=[CLIENT_1, CLIENT_2],
devices_response=[DEVICE_1],
)
@@ -424,7 +423,7 @@ async def test_option_track_clients(hass):
assert device_1 is not None
hass.config_entries.async_update_entry(
controller.config_entry,
config_entry,
options={CONF_TRACK_CLIENTS: False},
)
await hass.async_block_till_done()
@@ -439,7 +438,7 @@ async def test_option_track_clients(hass):
assert device_1 is not None
hass.config_entries.async_update_entry(
controller.config_entry,
config_entry,
options={CONF_TRACK_CLIENTS: True},
)
await hass.async_block_till_done()
@@ -454,10 +453,11 @@ async def test_option_track_clients(hass):
assert device_1 is not None
async def test_option_track_wired_clients(hass):
async def test_option_track_wired_clients(hass, aioclient_mock):
"""Test the tracking of wired clients can be turned off."""
controller = await setup_unifi_integration(
config_entry = await setup_unifi_integration(
hass,
aioclient_mock,
clients_response=[CLIENT_1, CLIENT_2],
devices_response=[DEVICE_1],
)
@@ -473,7 +473,7 @@ async def test_option_track_wired_clients(hass):
assert device_1 is not None
hass.config_entries.async_update_entry(
controller.config_entry,
config_entry,
options={CONF_TRACK_WIRED_CLIENTS: False},
)
await hass.async_block_till_done()
@@ -488,7 +488,7 @@ async def test_option_track_wired_clients(hass):
assert device_1 is not None
hass.config_entries.async_update_entry(
controller.config_entry,
config_entry,
options={CONF_TRACK_WIRED_CLIENTS: True},
)
await hass.async_block_till_done()
@@ -503,10 +503,11 @@ async def test_option_track_wired_clients(hass):
assert device_1 is not None
async def test_option_track_devices(hass):
async def test_option_track_devices(hass, aioclient_mock):
"""Test the tracking of devices can be turned off."""
controller = await setup_unifi_integration(
config_entry = await setup_unifi_integration(
hass,
aioclient_mock,
clients_response=[CLIENT_1, CLIENT_2],
devices_response=[DEVICE_1],
)
@@ -522,7 +523,7 @@ async def test_option_track_devices(hass):
assert device_1 is not None
hass.config_entries.async_update_entry(
controller.config_entry,
config_entry,
options={CONF_TRACK_DEVICES: False},
)
await hass.async_block_till_done()
@@ -537,7 +538,7 @@ async def test_option_track_devices(hass):
assert device_1 is None
hass.config_entries.async_update_entry(
controller.config_entry,
config_entry,
options={CONF_TRACK_DEVICES: True},
)
await hass.async_block_till_done()
@@ -552,7 +553,7 @@ async def test_option_track_devices(hass):
assert device_1 is not None
async def test_option_ssid_filter(hass):
async def test_option_ssid_filter(hass, aioclient_mock):
"""Test the SSID filter works.
Client 1 will travel from a supported SSID to an unsupported ssid.
@@ -561,9 +562,10 @@ async def test_option_ssid_filter(hass):
client_1_copy = copy(CLIENT_1)
client_1_copy["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
controller = await setup_unifi_integration(
hass, clients_response=[client_1_copy, CLIENT_3]
config_entry = await setup_unifi_integration(
hass, aioclient_mock, clients_response=[client_1_copy, CLIENT_3]
)
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2
client_1 = hass.states.get("device_tracker.client_1")
@@ -574,7 +576,7 @@ async def test_option_ssid_filter(hass):
# Setting SSID filter will remove clients outside of filter
hass.config_entries.async_update_entry(
controller.config_entry,
config_entry,
options={CONF_SSID_FILTER: ["ssid"]},
)
await hass.async_block_till_done()
@@ -609,7 +611,7 @@ async def test_option_ssid_filter(hass):
# Remove SSID filter
hass.config_entries.async_update_entry(
controller.config_entry,
config_entry,
options={CONF_SSID_FILTER: []},
)
await hass.async_block_till_done()
@@ -656,7 +658,7 @@ async def test_option_ssid_filter(hass):
assert client_3.state == "not_home"
async def test_wireless_client_go_wired_issue(hass):
async def test_wireless_client_go_wired_issue(hass, aioclient_mock):
"""Test the solution to catch wireless device go wired UniFi issue.
UniFi has a known issue that when a wireless device goes away it sometimes gets marked as wired.
@@ -664,7 +666,10 @@ async def test_wireless_client_go_wired_issue(hass):
client_1_client = copy(CLIENT_1)
client_1_client["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
controller = await setup_unifi_integration(hass, clients_response=[client_1_client])
config_entry = await setup_unifi_integration(
hass, aioclient_mock, clients_response=[client_1_client]
)
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
# Client is wireless
@@ -717,14 +722,18 @@ async def test_wireless_client_go_wired_issue(hass):
assert client_1.attributes["is_wired"] is False
async def test_option_ignore_wired_bug(hass):
async def test_option_ignore_wired_bug(hass, aioclient_mock):
"""Test option to ignore wired bug."""
client_1_client = copy(CLIENT_1)
client_1_client["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
controller = await setup_unifi_integration(
hass, options={CONF_IGNORE_WIRED_BUG: True}, clients_response=[client_1_client]
config_entry = await setup_unifi_integration(
hass,
aioclient_mock,
options={CONF_IGNORE_WIRED_BUG: True},
clients_response=[client_1_client],
)
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
# Client is wireless
@@ -777,7 +786,7 @@ async def test_option_ignore_wired_bug(hass):
assert client_1.attributes["is_wired"] is False
async def test_restoring_client(hass):
async def test_restoring_client(hass, aioclient_mock):
"""Test the update_items function with some clients."""
config_entry = config_entries.ConfigEntry(
version=1,
@@ -809,6 +818,7 @@ async def test_restoring_client(hass):
await setup_unifi_integration(
hass,
aioclient_mock,
options={CONF_BLOCK_CLIENT: True},
clients_response=[CLIENT_2],
clients_all_response=[CLIENT_1],
@@ -819,10 +829,11 @@ async def test_restoring_client(hass):
assert device_1 is not None
async def test_dont_track_clients(hass):
async def test_dont_track_clients(hass, aioclient_mock):
"""Test don't track clients config works."""
controller = await setup_unifi_integration(
config_entry = await setup_unifi_integration(
hass,
aioclient_mock,
options={CONF_TRACK_CLIENTS: False},
clients_response=[CLIENT_1],
devices_response=[DEVICE_1],
@@ -836,7 +847,7 @@ async def test_dont_track_clients(hass):
assert device_1 is not None
hass.config_entries.async_update_entry(
controller.config_entry,
config_entry,
options={CONF_TRACK_CLIENTS: True},
)
await hass.async_block_till_done()
@@ -850,10 +861,11 @@ async def test_dont_track_clients(hass):
assert device_1 is not None
async def test_dont_track_devices(hass):
async def test_dont_track_devices(hass, aioclient_mock):
"""Test don't track devices config works."""
controller = await setup_unifi_integration(
config_entry = await setup_unifi_integration(
hass,
aioclient_mock,
options={CONF_TRACK_DEVICES: False},
clients_response=[CLIENT_1],
devices_response=[DEVICE_1],
@@ -867,7 +879,7 @@ async def test_dont_track_devices(hass):
assert device_1 is None
hass.config_entries.async_update_entry(
controller.config_entry,
config_entry,
options={CONF_TRACK_DEVICES: True},
)
await hass.async_block_till_done()
@@ -881,10 +893,11 @@ async def test_dont_track_devices(hass):
assert device_1 is not None
async def test_dont_track_wired_clients(hass):
async def test_dont_track_wired_clients(hass, aioclient_mock):
"""Test don't track wired clients config works."""
controller = await setup_unifi_integration(
config_entry = await setup_unifi_integration(
hass,
aioclient_mock,
options={CONF_TRACK_WIRED_CLIENTS: False},
clients_response=[CLIENT_1, CLIENT_2],
)
@@ -897,7 +910,7 @@ async def test_dont_track_wired_clients(hass):
assert client_2 is None
hass.config_entries.async_update_entry(
controller.config_entry,
config_entry,
options={CONF_TRACK_WIRED_CLIENTS: True},
)
await hass.async_block_till_done()