UniFi - Improve expected SSID filter behavior (#32785)
* Improve expected ssid filter behavior * Fix tests
This commit is contained in:
parent
b9ad40ed38
commit
fa63dc1e25
2 changed files with 32 additions and 10 deletions
|
@ -45,6 +45,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
option_track_clients = controller.option_track_clients
|
option_track_clients = controller.option_track_clients
|
||||||
option_track_devices = controller.option_track_devices
|
option_track_devices = controller.option_track_devices
|
||||||
option_track_wired_clients = controller.option_track_wired_clients
|
option_track_wired_clients = controller.option_track_wired_clients
|
||||||
|
option_ssid_filter = controller.option_ssid_filter
|
||||||
|
|
||||||
registry = await hass.helpers.entity_registry.async_get_registry()
|
registry = await hass.helpers.entity_registry.async_get_registry()
|
||||||
|
|
||||||
|
@ -86,6 +87,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
nonlocal option_track_clients
|
nonlocal option_track_clients
|
||||||
nonlocal option_track_devices
|
nonlocal option_track_devices
|
||||||
nonlocal option_track_wired_clients
|
nonlocal option_track_wired_clients
|
||||||
|
nonlocal option_ssid_filter
|
||||||
|
|
||||||
update = False
|
update = False
|
||||||
remove = set()
|
remove = set()
|
||||||
|
@ -116,6 +118,18 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
if isinstance(entity, UniFiClientTracker) and entity.is_wired:
|
if isinstance(entity, UniFiClientTracker) and entity.is_wired:
|
||||||
remove.add(mac)
|
remove.add(mac)
|
||||||
|
|
||||||
|
if option_ssid_filter != controller.option_ssid_filter:
|
||||||
|
option_ssid_filter = controller.option_ssid_filter
|
||||||
|
update = True
|
||||||
|
|
||||||
|
for mac, entity in tracked.items():
|
||||||
|
if (
|
||||||
|
isinstance(entity, UniFiClientTracker)
|
||||||
|
and not entity.is_wired
|
||||||
|
and entity.client.essid not in option_ssid_filter
|
||||||
|
):
|
||||||
|
remove.add(mac)
|
||||||
|
|
||||||
option_track_clients = controller.option_track_clients
|
option_track_clients = controller.option_track_clients
|
||||||
option_track_devices = controller.option_track_devices
|
option_track_devices = controller.option_track_devices
|
||||||
option_track_wired_clients = controller.option_track_wired_clients
|
option_track_wired_clients = controller.option_track_wired_clients
|
||||||
|
@ -157,8 +171,16 @@ def add_entities(controller, async_add_entities, tracked):
|
||||||
if item_id in tracked:
|
if item_id in tracked:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if tracker_class is UniFiClientTracker and (
|
if tracker_class is UniFiClientTracker:
|
||||||
not controller.option_track_wired_clients and items[item_id].is_wired
|
client = items[item_id]
|
||||||
|
|
||||||
|
if not controller.option_track_wired_clients and client.is_wired:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if (
|
||||||
|
controller.option_ssid_filter
|
||||||
|
and not client.is_wired
|
||||||
|
and client.essid not in controller.option_ssid_filter
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
|
@ -124,7 +124,7 @@ async def test_tracked_devices(hass):
|
||||||
devices_response=[DEVICE_1, DEVICE_2],
|
devices_response=[DEVICE_1, DEVICE_2],
|
||||||
known_wireless_clients=(CLIENT_4["mac"],),
|
known_wireless_clients=(CLIENT_4["mac"],),
|
||||||
)
|
)
|
||||||
assert len(hass.states.async_all()) == 7
|
assert len(hass.states.async_all()) == 6
|
||||||
|
|
||||||
client_1 = hass.states.get("device_tracker.client_1")
|
client_1 = hass.states.get("device_tracker.client_1")
|
||||||
assert client_1 is not None
|
assert client_1 is not None
|
||||||
|
@ -134,9 +134,9 @@ async def test_tracked_devices(hass):
|
||||||
assert client_2 is not None
|
assert client_2 is not None
|
||||||
assert client_2.state == "not_home"
|
assert client_2.state == "not_home"
|
||||||
|
|
||||||
|
# Client on SSID not in SSID filter
|
||||||
client_3 = hass.states.get("device_tracker.client_3")
|
client_3 = hass.states.get("device_tracker.client_3")
|
||||||
assert client_3 is not None
|
assert not client_3
|
||||||
assert client_3.state == "not_home"
|
|
||||||
|
|
||||||
# Wireless client with wired bug, if bug active on restart mark device away
|
# Wireless client with wired bug, if bug active on restart mark device away
|
||||||
client_4 = hass.states.get("device_tracker.client_4")
|
client_4 = hass.states.get("device_tracker.client_4")
|
||||||
|
@ -350,11 +350,11 @@ async def test_option_ssid_filter(hass):
|
||||||
controller = await setup_unifi_integration(
|
controller = await setup_unifi_integration(
|
||||||
hass, options={CONF_SSID_FILTER: ["ssid"]}, clients_response=[CLIENT_3],
|
hass, options={CONF_SSID_FILTER: ["ssid"]}, clients_response=[CLIENT_3],
|
||||||
)
|
)
|
||||||
assert len(hass.states.async_all()) == 2
|
assert len(hass.states.async_all()) == 1
|
||||||
|
|
||||||
# SSID filter active
|
# SSID filter active
|
||||||
client_3 = hass.states.get("device_tracker.client_3")
|
client_3 = hass.states.get("device_tracker.client_3")
|
||||||
assert client_3.state == "not_home"
|
assert not client_3
|
||||||
|
|
||||||
client_3_copy = copy(CLIENT_3)
|
client_3_copy = copy(CLIENT_3)
|
||||||
client_3_copy["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
client_3_copy["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
||||||
|
@ -364,7 +364,7 @@ async def test_option_ssid_filter(hass):
|
||||||
|
|
||||||
# SSID filter active even though time stamp should mark as home
|
# SSID filter active even though time stamp should mark as home
|
||||||
client_3 = hass.states.get("device_tracker.client_3")
|
client_3 = hass.states.get("device_tracker.client_3")
|
||||||
assert client_3.state == "not_home"
|
assert not client_3
|
||||||
|
|
||||||
# Remove SSID filter
|
# Remove SSID filter
|
||||||
hass.config_entries.async_update_entry(
|
hass.config_entries.async_update_entry(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue