UniFi - Improve expected SSID filter behavior (#32785)

* Improve expected ssid filter behavior

* Fix tests
This commit is contained in:
Robert Svensson 2020-03-16 12:10:45 +01:00 committed by GitHub
parent b9ad40ed38
commit fa63dc1e25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 10 deletions

View file

@ -45,6 +45,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
option_track_clients = controller.option_track_clients
option_track_devices = controller.option_track_devices
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()
@ -86,6 +87,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
nonlocal option_track_clients
nonlocal option_track_devices
nonlocal option_track_wired_clients
nonlocal option_ssid_filter
update = False
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:
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_devices = controller.option_track_devices
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:
continue
if tracker_class is UniFiClientTracker and (
not controller.option_track_wired_clients and items[item_id].is_wired
if tracker_class is UniFiClientTracker:
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

View file

@ -124,7 +124,7 @@ async def test_tracked_devices(hass):
devices_response=[DEVICE_1, DEVICE_2],
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")
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.state == "not_home"
# Client on SSID not in SSID filter
client_3 = hass.states.get("device_tracker.client_3")
assert client_3 is not None
assert client_3.state == "not_home"
assert not client_3
# Wireless client with wired bug, if bug active on restart mark device away
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(
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
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["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
client_3 = hass.states.get("device_tracker.client_3")
assert client_3.state == "not_home"
assert not client_3
# Remove SSID filter
hass.config_entries.async_update_entry(