Make client tracker use common UniFi entity class (#84942)

* Make client tracker use common UniFi entity class

* Fix tests

* Fix mypy

* Remove legacy data

* Fix comment: skip else use return

* Minor change

* Remove missed stuff from previous rebase

* Import async_device_available_fn from entities.py rather than specifying it in device_tracker

* Avoid using asserts

* Keep explicit parenthesis for readability

* Allow loading entities on option changes
This commit is contained in:
Robert Svensson 2023-03-11 06:23:49 +01:00 committed by GitHub
parent d6a223f0e1
commit 288a4203ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 144 additions and 262 deletions

View file

@ -156,7 +156,7 @@ async def test_tracked_clients(
# State change signalling works
client_1["last_seen"] += 1
client_1["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
mock_unifi_websocket(message=MessageKey.CLIENT, data=client_1)
await hass.async_block_till_done()
@ -244,6 +244,7 @@ async def test_tracked_wireless_clients_event_source(
# New data
client["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
mock_unifi_websocket(message=MessageKey.CLIENT, data=client)
await hass.async_block_till_done()
assert hass.states.get("device_tracker.client").state == STATE_HOME
@ -703,6 +704,11 @@ async def test_option_ssid_filter(
mock_unifi_websocket(message=MessageKey.CLIENT, data=client_on_ssid2)
await hass.async_block_till_done()
new_time = dt_util.utcnow() + controller.option_detection_time
with patch("homeassistant.util.dt.utcnow", return_value=new_time):
async_fire_time_changed(hass, new_time)
await hass.async_block_till_done()
# SSID filter marks client as away
assert hass.states.get("device_tracker.client").state == STATE_NOT_HOME
@ -726,7 +732,7 @@ async def test_option_ssid_filter(
# Time pass to mark client as away
new_time = dt_util.utcnow() + controller.option_detection_time
new_time += controller.option_detection_time
with patch("homeassistant.util.dt.utcnow", return_value=new_time):
async_fire_time_changed(hass, new_time)
await hass.async_block_till_done()
@ -745,9 +751,7 @@ async def test_option_ssid_filter(
mock_unifi_websocket(message=MessageKey.CLIENT, data=client_on_ssid2)
await hass.async_block_till_done()
new_time = (
dt_util.utcnow() + controller.option_detection_time + timedelta(seconds=1)
)
new_time += controller.option_detection_time
with patch("homeassistant.util.dt.utcnow", return_value=new_time):
async_fire_time_changed(hass, new_time)
await hass.async_block_till_done()
@ -784,10 +788,9 @@ async def test_wireless_client_go_wired_issue(
# Client is wireless
client_state = hass.states.get("device_tracker.client")
assert client_state.state == STATE_HOME
assert client_state.attributes["is_wired"] is False
# Trigger wired bug
client["last_seen"] += 1
client["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
client["is_wired"] = True
mock_unifi_websocket(message=MessageKey.CLIENT, data=client)
await hass.async_block_till_done()
@ -795,7 +798,6 @@ async def test_wireless_client_go_wired_issue(
# Wired bug fix keeps client marked as wireless
client_state = hass.states.get("device_tracker.client")
assert client_state.state == STATE_HOME
assert client_state.attributes["is_wired"] is False
# Pass time
new_time = dt_util.utcnow() + controller.option_detection_time
@ -806,7 +808,6 @@ async def test_wireless_client_go_wired_issue(
# Marked as home according to the timer
client_state = hass.states.get("device_tracker.client")
assert client_state.state == STATE_NOT_HOME
assert client_state.attributes["is_wired"] is False
# Try to mark client as connected
client["last_seen"] += 1
@ -816,7 +817,6 @@ async def test_wireless_client_go_wired_issue(
# Make sure it don't go online again until wired bug disappears
client_state = hass.states.get("device_tracker.client")
assert client_state.state == STATE_NOT_HOME
assert client_state.attributes["is_wired"] is False
# Make client wireless
client["last_seen"] += 1
@ -827,7 +827,6 @@ async def test_wireless_client_go_wired_issue(
# Client is no longer affected by wired bug and can be marked online
client_state = hass.states.get("device_tracker.client")
assert client_state.state == STATE_HOME
assert client_state.attributes["is_wired"] is False
async def test_option_ignore_wired_bug(
@ -859,7 +858,6 @@ async def test_option_ignore_wired_bug(
# Client is wireless
client_state = hass.states.get("device_tracker.client")
assert client_state.state == STATE_HOME
assert client_state.attributes["is_wired"] is False
# Trigger wired bug
client["is_wired"] = True
@ -869,7 +867,6 @@ async def test_option_ignore_wired_bug(
# Wired bug in effect
client_state = hass.states.get("device_tracker.client")
assert client_state.state == STATE_HOME
assert client_state.attributes["is_wired"] is True
# pass time
new_time = dt_util.utcnow() + controller.option_detection_time
@ -880,7 +877,6 @@ async def test_option_ignore_wired_bug(
# Timer marks client as away
client_state = hass.states.get("device_tracker.client")
assert client_state.state == STATE_NOT_HOME
assert client_state.attributes["is_wired"] is True
# Mark client as connected again
client["last_seen"] += 1
@ -890,7 +886,6 @@ async def test_option_ignore_wired_bug(
# Ignoring wired bug allows client to go home again even while affected
client_state = hass.states.get("device_tracker.client")
assert client_state.state == STATE_HOME
assert client_state.attributes["is_wired"] is True
# Make client wireless
client["last_seen"] += 1
@ -901,7 +896,6 @@ async def test_option_ignore_wired_bug(
# Client is wireless and still connected
client_state = hass.states.get("device_tracker.client")
assert client_state.state == STATE_HOME
assert client_state.attributes["is_wired"] is False
async def test_restoring_client(