diff --git a/homeassistant/components/unifi/device_tracker.py b/homeassistant/components/unifi/device_tracker.py index 6dfa6a6ecad3fe..8b45a0f227bc40 100644 --- a/homeassistant/components/unifi/device_tracker.py +++ b/homeassistant/components/unifi/device_tracker.py @@ -130,6 +130,9 @@ def __init__(self, client, controller): self.is_wired = self.client.mac not in controller.wireless_clients self.wired_bug = None + if self.is_wired != self.client.is_wired: + self.wired_bug = dt_util.utcnow() - self.controller.option_detection_time + @property def entity_registry_enabled_default(self): """Return if the entity should be enabled when first added to the entity registry.""" diff --git a/tests/components/unifi/test_controller.py b/tests/components/unifi/test_controller.py index c86e2f11538982..ddd16f948cb43e 100644 --- a/tests/components/unifi/test_controller.py +++ b/tests/components/unifi/test_controller.py @@ -59,6 +59,7 @@ async def setup_unifi_integration( clients_response, devices_response, clients_all_response, + known_wireless_clients=None, ): """Create the UniFi controller.""" if UNIFI_CONFIG not in hass.data: @@ -76,6 +77,11 @@ async def setup_unifi_integration( entry_id=1, ) + if known_wireless_clients: + hass.data[UNIFI_WIRELESS_CLIENTS].update_data( + known_wireless_clients, config_entry + ) + mock_client_responses = deque() mock_client_responses.append(clients_response) diff --git a/tests/components/unifi/test_device_tracker.py b/tests/components/unifi/test_device_tracker.py index 42ba83a16124ec..5c1505653a3e1a 100644 --- a/tests/components/unifi/test_device_tracker.py +++ b/tests/components/unifi/test_device_tracker.py @@ -43,6 +43,14 @@ "last_seen": 1562600145, "mac": "00:00:00:00:00:03", } +CLIENT_4 = { + "essid": "ssid", + "hostname": "client_4", + "ip": "10.0.0.4", + "is_wired": True, + "last_seen": 1562600145, + "mac": "00:00:00:00:00:04", +} DEVICE_1 = { "board_rev": 3, @@ -102,16 +110,20 @@ async def test_no_clients(hass): async def test_tracked_devices(hass): """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( hass, ENTRY_CONFIG, options={CONF_SSID_FILTER: ["ssid"]}, sites=SITES, - clients_response=[CLIENT_1, CLIENT_2, CLIENT_3], + clients_response=[CLIENT_1, CLIENT_2, CLIENT_3, client_4_copy], devices_response=[DEVICE_1, DEVICE_2], clients_all_response={}, + known_wireless_clients=(CLIENT_4["mac"],), ) - assert len(hass.states.async_all()) == 5 + assert len(hass.states.async_all()) == 6 client_1 = hass.states.get("device_tracker.client_1") assert client_1 is not None @@ -124,6 +136,11 @@ async def test_tracked_devices(hass): client_3 = hass.states.get("device_tracker.client_3") assert client_3 is None + # Wireless client with wired bug, if bug active on restart mark device away + client_4 = hass.states.get("device_tracker.client_4") + assert client_4 is not None + assert client_4.state == "not_home" + device_1 = hass.states.get("device_tracker.device_1") assert device_1 is not None assert device_1.state == "not_home"