Skip to content

Commit

Permalink
Handle wired bug on restart (#30276)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane610 authored and andrewsayre committed Dec 30, 2019
1 parent bad3557 commit 41d2d1f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 3 additions & 0 deletions homeassistant/components/unifi/device_tracker.py
Expand Up @@ -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."""
Expand Down
6 changes: 6 additions & 0 deletions tests/components/unifi/test_controller.py
Expand Up @@ -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:
Expand All @@ -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)

Expand Down
21 changes: 19 additions & 2 deletions tests/components/unifi/test_device_tracker.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit 41d2d1f

Please sign in to comment.