diff --git a/homeassistant/components/unifi/device_tracker.py b/homeassistant/components/unifi/device_tracker.py index e5d3bcfa82b..07e96a45fce 100644 --- a/homeassistant/components/unifi/device_tracker.py +++ b/homeassistant/components/unifi/device_tracker.py @@ -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,10 +171,18 @@ 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 - ): - continue + 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 tracked[item_id] = tracker_class(items[item_id], controller) new_tracked.append(tracked[item_id]) diff --git a/tests/components/unifi/test_device_tracker.py b/tests/components/unifi/test_device_tracker.py index 1d314c1fe86..bb15ff65fb9 100644 --- a/tests/components/unifi/test_device_tracker.py +++ b/tests/components/unifi/test_device_tracker.py @@ -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(