2019-09-11 19:35:30 +00:00
|
|
|
"""The tests for the UniFi device tracker platform."""
|
2019-07-14 19:57:09 +00:00
|
|
|
from copy import copy
|
|
|
|
from datetime import timedelta
|
2016-02-19 23:19:03 +00:00
|
|
|
|
2020-05-04 17:29:49 +00:00
|
|
|
from aiounifi.controller import (
|
|
|
|
MESSAGE_CLIENT,
|
|
|
|
MESSAGE_CLIENT_REMOVED,
|
|
|
|
MESSAGE_DEVICE,
|
|
|
|
MESSAGE_EVENT,
|
|
|
|
SIGNAL_CONNECTION_STATE,
|
|
|
|
)
|
2020-04-16 22:08:53 +00:00
|
|
|
from aiounifi.websocket import SIGNAL_DATA, STATE_DISCONNECTED, STATE_RUNNING
|
2019-12-10 19:05:18 +00:00
|
|
|
|
2019-07-14 19:57:09 +00:00
|
|
|
from homeassistant import config_entries
|
2020-04-23 14:48:24 +00:00
|
|
|
from homeassistant.components.device_tracker import DOMAIN as TRACKER_DOMAIN
|
2019-07-14 19:57:09 +00:00
|
|
|
from homeassistant.components.unifi.const import (
|
2020-03-12 10:56:50 +00:00
|
|
|
CONF_BLOCK_CLIENT,
|
2020-04-17 06:39:01 +00:00
|
|
|
CONF_IGNORE_WIRED_BUG,
|
2019-08-21 20:22:42 +00:00
|
|
|
CONF_SSID_FILTER,
|
2020-02-13 00:15:08 +00:00
|
|
|
CONF_TRACK_CLIENTS,
|
2019-08-31 20:04:04 +00:00
|
|
|
CONF_TRACK_DEVICES,
|
|
|
|
CONF_TRACK_WIRED_CLIENTS,
|
2020-04-23 14:48:24 +00:00
|
|
|
DOMAIN as UNIFI_DOMAIN,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-10-07 19:55:35 +00:00
|
|
|
from homeassistant.const import STATE_UNAVAILABLE
|
2019-07-29 21:13:04 +00:00
|
|
|
from homeassistant.helpers import entity_registry
|
2019-07-14 19:57:09 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
import homeassistant.util.dt as dt_util
|
2019-06-02 16:24:13 +00:00
|
|
|
|
2020-01-03 18:23:17 +00:00
|
|
|
from .test_controller import ENTRY_CONFIG, setup_unifi_integration
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
from tests.async_mock import patch
|
2020-05-04 17:29:49 +00:00
|
|
|
from tests.common import async_fire_time_changed
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2019-07-14 19:57:09 +00:00
|
|
|
CLIENT_1 = {
|
2020-05-04 17:29:49 +00:00
|
|
|
"ap_mac": "00:00:00:00:02:01",
|
2019-07-31 19:25:30 +00:00
|
|
|
"essid": "ssid",
|
|
|
|
"hostname": "client_1",
|
|
|
|
"ip": "10.0.0.1",
|
|
|
|
"is_wired": False,
|
|
|
|
"last_seen": 1562600145,
|
|
|
|
"mac": "00:00:00:00:00:01",
|
2019-07-14 19:57:09 +00:00
|
|
|
}
|
|
|
|
CLIENT_2 = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"hostname": "client_2",
|
|
|
|
"ip": "10.0.0.2",
|
|
|
|
"is_wired": True,
|
|
|
|
"last_seen": 1562600145,
|
|
|
|
"mac": "00:00:00:00:00:02",
|
|
|
|
"name": "Wired Client",
|
2019-07-14 19:57:09 +00:00
|
|
|
}
|
|
|
|
CLIENT_3 = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"essid": "ssid2",
|
|
|
|
"hostname": "client_3",
|
|
|
|
"ip": "10.0.0.3",
|
|
|
|
"is_wired": False,
|
|
|
|
"last_seen": 1562600145,
|
|
|
|
"mac": "00:00:00:00:00:03",
|
2019-07-14 19:57:09 +00:00
|
|
|
}
|
2019-12-30 18:40:52 +00:00
|
|
|
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",
|
|
|
|
}
|
2020-02-27 19:50:34 +00:00
|
|
|
CLIENT_5 = {
|
|
|
|
"essid": "ssid",
|
|
|
|
"hostname": "client_5",
|
|
|
|
"ip": "10.0.0.5",
|
|
|
|
"is_wired": True,
|
|
|
|
"last_seen": None,
|
|
|
|
"mac": "00:00:00:00:00:05",
|
|
|
|
}
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2019-07-30 08:05:51 +00:00
|
|
|
DEVICE_1 = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"board_rev": 3,
|
|
|
|
"device_id": "mock-id",
|
|
|
|
"has_fan": True,
|
|
|
|
"fan_level": 0,
|
|
|
|
"ip": "10.0.1.1",
|
|
|
|
"last_seen": 1562600145,
|
|
|
|
"mac": "00:00:00:00:01:01",
|
|
|
|
"model": "US16P150",
|
|
|
|
"name": "device_1",
|
2019-08-31 20:04:04 +00:00
|
|
|
"overheating": True,
|
2019-08-11 20:40:44 +00:00
|
|
|
"state": 1,
|
2019-07-31 19:25:30 +00:00
|
|
|
"type": "usw",
|
2019-08-31 20:04:04 +00:00
|
|
|
"upgradable": True,
|
2019-07-31 19:25:30 +00:00
|
|
|
"version": "4.0.42.10433",
|
2019-07-30 08:05:51 +00:00
|
|
|
}
|
2019-08-06 21:55:36 +00:00
|
|
|
DEVICE_2 = {
|
|
|
|
"board_rev": 3,
|
|
|
|
"device_id": "mock-id",
|
|
|
|
"has_fan": True,
|
|
|
|
"ip": "10.0.1.1",
|
|
|
|
"mac": "00:00:00:00:01:01",
|
|
|
|
"model": "US16P150",
|
|
|
|
"name": "device_1",
|
2019-08-11 20:40:44 +00:00
|
|
|
"state": 0,
|
2019-08-06 21:55:36 +00:00
|
|
|
"type": "usw",
|
|
|
|
"version": "4.0.42.10433",
|
|
|
|
}
|
2019-07-30 08:05:51 +00:00
|
|
|
|
2020-05-04 17:29:49 +00:00
|
|
|
EVENT_CLIENT_1_WIRELESS_CONNECTED = {
|
|
|
|
"user": CLIENT_1["mac"],
|
|
|
|
"ssid": CLIENT_1["essid"],
|
|
|
|
"ap": CLIENT_1["ap_mac"],
|
|
|
|
"radio": "na",
|
|
|
|
"channel": "44",
|
|
|
|
"hostname": CLIENT_1["hostname"],
|
|
|
|
"key": "EVT_WU_Connected",
|
|
|
|
"subsystem": "wlan",
|
|
|
|
"site_id": "name",
|
|
|
|
"time": 1587753456179,
|
|
|
|
"datetime": "2020-04-24T18:37:36Z",
|
|
|
|
"msg": f'User{[CLIENT_1["mac"]]} has connected to AP[{CLIENT_1["ap_mac"]}] with SSID "{CLIENT_1["essid"]}" on "channel 44(na)"',
|
|
|
|
"_id": "5ea331fa30c49e00f90ddc1a",
|
|
|
|
}
|
|
|
|
|
|
|
|
EVENT_CLIENT_1_WIRELESS_DISCONNECTED = {
|
|
|
|
"user": CLIENT_1["mac"],
|
|
|
|
"ssid": CLIENT_1["essid"],
|
|
|
|
"hostname": CLIENT_1["hostname"],
|
|
|
|
"ap": CLIENT_1["ap_mac"],
|
|
|
|
"duration": 467,
|
|
|
|
"bytes": 459039,
|
|
|
|
"key": "EVT_WU_Disconnected",
|
|
|
|
"subsystem": "wlan",
|
|
|
|
"site_id": "name",
|
|
|
|
"time": 1587752927000,
|
|
|
|
"datetime": "2020-04-24T18:28:47Z",
|
|
|
|
"msg": f'User{[CLIENT_1["mac"]]} disconnected from "{CLIENT_1["essid"]}" (7m 47s connected, 448.28K bytes, last AP[{CLIENT_1["ap_mac"]}])',
|
|
|
|
"_id": "5ea32ff730c49e00f90dca1a",
|
|
|
|
}
|
|
|
|
|
2019-07-14 19:57:09 +00:00
|
|
|
|
|
|
|
async def test_platform_manually_configured(hass):
|
2019-10-01 04:19:51 +00:00
|
|
|
"""Test that nothing happens when configuring unifi through device tracker platform."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert (
|
|
|
|
await async_setup_component(
|
2020-04-23 14:48:24 +00:00
|
|
|
hass, TRACKER_DOMAIN, {TRACKER_DOMAIN: {"platform": UNIFI_DOMAIN}}
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-10-01 04:19:51 +00:00
|
|
|
is False
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert UNIFI_DOMAIN not in hass.data
|
2019-07-14 19:57:09 +00:00
|
|
|
|
|
|
|
|
2019-10-03 20:23:25 +00:00
|
|
|
async def test_no_clients(hass):
|
2019-07-14 19:57:09 +00:00
|
|
|
"""Test the update_clients function when no clients are found."""
|
2020-01-03 18:23:17 +00:00
|
|
|
await setup_unifi_integration(hass)
|
2019-07-25 14:56:56 +00:00
|
|
|
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 0
|
2019-07-14 19:57:09 +00:00
|
|
|
|
|
|
|
|
2020-05-04 17:29:49 +00:00
|
|
|
async def test_tracked_wireless_clients(hass):
|
|
|
|
"""Test the update_items function with some clients."""
|
|
|
|
controller = await setup_unifi_integration(hass, clients_response=[CLIENT_1])
|
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is not None
|
|
|
|
assert client_1.state == "not_home"
|
|
|
|
|
|
|
|
# State change signalling works without events
|
|
|
|
client_1_copy = copy(CLIENT_1)
|
|
|
|
client_1_copy["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
|
|
|
controller.api.websocket._data = {
|
|
|
|
"meta": {"message": MESSAGE_CLIENT},
|
|
|
|
"data": [client_1_copy],
|
|
|
|
}
|
|
|
|
controller.api.session_handler(SIGNAL_DATA)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1.state == "home"
|
|
|
|
|
|
|
|
# State change signalling works with events
|
|
|
|
controller.api.websocket._data = {
|
|
|
|
"meta": {"message": MESSAGE_EVENT},
|
|
|
|
"data": [EVENT_CLIENT_1_WIRELESS_DISCONNECTED],
|
|
|
|
}
|
|
|
|
controller.api.session_handler(SIGNAL_DATA)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1.state == "home"
|
|
|
|
|
|
|
|
async_fire_time_changed(hass, dt_util.utcnow() + controller.option_detection_time)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1.state == "not_home"
|
|
|
|
|
|
|
|
controller.api.websocket._data = {
|
|
|
|
"meta": {"message": MESSAGE_EVENT},
|
|
|
|
"data": [EVENT_CLIENT_1_WIRELESS_CONNECTED],
|
|
|
|
}
|
|
|
|
controller.api.session_handler(SIGNAL_DATA)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1.state == "home"
|
|
|
|
|
|
|
|
# test wired bug
|
|
|
|
|
|
|
|
|
2019-10-03 20:23:25 +00:00
|
|
|
async def test_tracked_devices(hass):
|
2019-07-14 19:57:09 +00:00
|
|
|
"""Test the update_items function with some clients."""
|
2019-12-30 18:40:52 +00:00
|
|
|
client_4_copy = copy(CLIENT_4)
|
|
|
|
client_4_copy["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
|
|
|
|
2019-10-03 20:23:25 +00:00
|
|
|
controller = await setup_unifi_integration(
|
|
|
|
hass,
|
|
|
|
options={CONF_SSID_FILTER: ["ssid"]},
|
2020-02-27 19:50:34 +00:00
|
|
|
clients_response=[CLIENT_1, CLIENT_2, CLIENT_3, CLIENT_5, client_4_copy],
|
2019-10-03 20:23:25 +00:00
|
|
|
devices_response=[DEVICE_1, DEVICE_2],
|
2019-12-30 18:40:52 +00:00
|
|
|
known_wireless_clients=(CLIENT_4["mac"],),
|
2019-10-03 20:23:25 +00:00
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 5
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
2019-07-30 08:05:51 +00:00
|
|
|
assert client_1 is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert client_1.state == "not_home"
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
client_2 = hass.states.get("device_tracker.wired_client")
|
2019-07-30 08:05:51 +00:00
|
|
|
assert client_2 is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert client_2.state == "not_home"
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2020-03-16 11:10:45 +00:00
|
|
|
# Client on SSID not in SSID filter
|
2019-07-31 19:25:30 +00:00
|
|
|
client_3 = hass.states.get("device_tracker.client_3")
|
2020-03-16 11:10:45 +00:00
|
|
|
assert not client_3
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2019-12-30 18:40:52 +00:00
|
|
|
# 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"
|
|
|
|
|
2020-02-27 19:50:34 +00:00
|
|
|
# A client that has never been seen should be marked away.
|
|
|
|
client_5 = hass.states.get("device_tracker.client_5")
|
|
|
|
assert client_5 is not None
|
|
|
|
assert client_5.state == "not_home"
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
2019-07-30 08:05:51 +00:00
|
|
|
assert device_1 is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert device_1.state == "not_home"
|
2019-07-30 08:05:51 +00:00
|
|
|
|
2020-02-13 00:15:08 +00:00
|
|
|
# State change signalling works
|
2019-07-30 08:05:51 +00:00
|
|
|
client_1_copy = copy(CLIENT_1)
|
2019-07-31 19:25:30 +00:00
|
|
|
client_1_copy["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
2020-05-04 17:29:49 +00:00
|
|
|
event = {"meta": {"message": MESSAGE_CLIENT}, "data": [client_1_copy]}
|
2020-01-31 19:23:25 +00:00
|
|
|
controller.api.message_handler(event)
|
2019-07-30 08:05:51 +00:00
|
|
|
device_1_copy = copy(DEVICE_1)
|
2019-07-31 19:25:30 +00:00
|
|
|
device_1_copy["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
2020-05-04 17:29:49 +00:00
|
|
|
event = {"meta": {"message": MESSAGE_DEVICE}, "data": [device_1_copy]}
|
2020-01-31 19:23:25 +00:00
|
|
|
controller.api.message_handler(event)
|
2019-07-14 19:57:09 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1.state == "home"
|
2019-07-30 08:05:51 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
|
|
|
assert device_1.state == "home"
|
2019-07-29 21:13:04 +00:00
|
|
|
|
2020-02-13 00:15:08 +00:00
|
|
|
# Disabled device is unavailable
|
|
|
|
device_1_copy = copy(DEVICE_1)
|
|
|
|
device_1_copy["disabled"] = True
|
2020-05-04 17:29:49 +00:00
|
|
|
event = {"meta": {"message": MESSAGE_DEVICE}, "data": [device_1_copy]}
|
2020-02-13 00:15:08 +00:00
|
|
|
controller.api.message_handler(event)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
|
|
|
assert device_1.state == STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
|
2020-04-16 22:08:53 +00:00
|
|
|
async def test_remove_clients(hass):
|
|
|
|
"""Test the remove_items function with some clients."""
|
|
|
|
controller = await setup_unifi_integration(
|
|
|
|
hass, clients_response=[CLIENT_1, CLIENT_2]
|
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2
|
2020-04-16 22:08:53 +00:00
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is not None
|
|
|
|
|
|
|
|
wired_client = hass.states.get("device_tracker.wired_client")
|
|
|
|
assert wired_client is not None
|
|
|
|
|
|
|
|
controller.api.websocket._data = {
|
|
|
|
"meta": {"message": MESSAGE_CLIENT_REMOVED},
|
|
|
|
"data": [CLIENT_1],
|
|
|
|
}
|
|
|
|
controller.api.session_handler(SIGNAL_DATA)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
|
2020-04-16 22:08:53 +00:00
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is None
|
|
|
|
|
|
|
|
wired_client = hass.states.get("device_tracker.wired_client")
|
|
|
|
assert wired_client is not None
|
|
|
|
|
|
|
|
|
2020-02-13 00:15:08 +00:00
|
|
|
async def test_controller_state_change(hass):
|
|
|
|
"""Verify entities state reflect on controller becoming unavailable."""
|
|
|
|
controller = await setup_unifi_integration(
|
|
|
|
hass, clients_response=[CLIENT_1], devices_response=[DEVICE_1],
|
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2
|
2020-02-13 00:15:08 +00:00
|
|
|
|
2020-01-31 19:23:25 +00:00
|
|
|
# Controller unavailable
|
|
|
|
controller.async_unifi_signalling_callback(
|
|
|
|
SIGNAL_CONNECTION_STATE, STATE_DISCONNECTED
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1.state == STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
|
|
|
assert device_1.state == STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
# Controller available
|
|
|
|
controller.async_unifi_signalling_callback(SIGNAL_CONNECTION_STATE, STATE_RUNNING)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
2020-02-13 00:15:08 +00:00
|
|
|
assert client_1.state == "not_home"
|
2020-01-31 19:23:25 +00:00
|
|
|
|
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
2020-02-13 00:15:08 +00:00
|
|
|
assert device_1.state == "not_home"
|
2020-01-31 19:23:25 +00:00
|
|
|
|
2020-02-13 00:15:08 +00:00
|
|
|
|
|
|
|
async def test_option_track_clients(hass):
|
|
|
|
"""Test the tracking of clients can be turned off."""
|
|
|
|
controller = await setup_unifi_integration(
|
|
|
|
hass, clients_response=[CLIENT_1, CLIENT_2], devices_response=[DEVICE_1],
|
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 3
|
2020-02-13 00:15:08 +00:00
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is not None
|
|
|
|
|
|
|
|
client_2 = hass.states.get("device_tracker.wired_client")
|
|
|
|
assert client_2 is not None
|
|
|
|
|
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
|
|
|
assert device_1 is not None
|
|
|
|
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
controller.config_entry, options={CONF_TRACK_CLIENTS: False},
|
|
|
|
)
|
2019-08-01 15:22:08 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-02-13 00:15:08 +00:00
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is None
|
|
|
|
|
|
|
|
client_2 = hass.states.get("device_tracker.wired_client")
|
|
|
|
assert client_2 is None
|
|
|
|
|
2019-08-01 15:22:08 +00:00
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
2020-02-13 00:15:08 +00:00
|
|
|
assert device_1 is not None
|
|
|
|
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
controller.config_entry, options={CONF_TRACK_CLIENTS: True},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is not None
|
|
|
|
|
|
|
|
client_2 = hass.states.get("device_tracker.wired_client")
|
|
|
|
assert client_2 is not None
|
|
|
|
|
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
|
|
|
assert device_1 is not None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_option_track_wired_clients(hass):
|
|
|
|
"""Test the tracking of wired clients can be turned off."""
|
|
|
|
controller = await setup_unifi_integration(
|
|
|
|
hass, clients_response=[CLIENT_1, CLIENT_2], devices_response=[DEVICE_1],
|
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 3
|
2020-02-13 00:15:08 +00:00
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is not None
|
|
|
|
|
|
|
|
client_2 = hass.states.get("device_tracker.wired_client")
|
|
|
|
assert client_2 is not None
|
|
|
|
|
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
|
|
|
assert device_1 is not None
|
2019-08-01 15:22:08 +00:00
|
|
|
|
2019-08-31 20:04:04 +00:00
|
|
|
hass.config_entries.async_update_entry(
|
2020-02-13 00:15:08 +00:00
|
|
|
controller.config_entry, options={CONF_TRACK_WIRED_CLIENTS: False},
|
2019-08-31 20:04:04 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2020-02-13 00:15:08 +00:00
|
|
|
|
2019-08-31 20:04:04 +00:00
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
2020-02-13 00:15:08 +00:00
|
|
|
assert client_1 is not None
|
|
|
|
|
2019-08-31 20:04:04 +00:00
|
|
|
client_2 = hass.states.get("device_tracker.wired_client")
|
|
|
|
assert client_2 is None
|
2020-02-13 00:15:08 +00:00
|
|
|
|
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
|
|
|
assert device_1 is not None
|
|
|
|
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
controller.config_entry, options={CONF_TRACK_WIRED_CLIENTS: True},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is not None
|
|
|
|
|
|
|
|
client_2 = hass.states.get("device_tracker.wired_client")
|
|
|
|
assert client_2 is not None
|
|
|
|
|
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
|
|
|
assert device_1 is not None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_option_track_devices(hass):
|
|
|
|
"""Test the tracking of devices can be turned off."""
|
|
|
|
controller = await setup_unifi_integration(
|
|
|
|
hass, clients_response=[CLIENT_1, CLIENT_2], devices_response=[DEVICE_1],
|
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 3
|
2020-02-13 00:15:08 +00:00
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is not None
|
|
|
|
|
|
|
|
client_2 = hass.states.get("device_tracker.wired_client")
|
|
|
|
assert client_2 is not None
|
|
|
|
|
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
|
|
|
assert device_1 is not None
|
|
|
|
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
controller.config_entry, options={CONF_TRACK_DEVICES: False},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is not None
|
|
|
|
|
|
|
|
client_2 = hass.states.get("device_tracker.wired_client")
|
|
|
|
assert client_2 is not None
|
|
|
|
|
2019-08-31 20:04:04 +00:00
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
|
|
|
assert device_1 is None
|
|
|
|
|
2020-02-13 00:15:08 +00:00
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
controller.config_entry, options={CONF_TRACK_DEVICES: True},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is not None
|
|
|
|
|
|
|
|
client_2 = hass.states.get("device_tracker.wired_client")
|
|
|
|
assert client_2 is not None
|
|
|
|
|
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
|
|
|
assert device_1 is not None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_option_ssid_filter(hass):
|
|
|
|
"""Test the SSID filter works."""
|
2020-04-17 06:39:01 +00:00
|
|
|
controller = await setup_unifi_integration(hass, clients_response=[CLIENT_3])
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
|
2020-04-17 06:39:01 +00:00
|
|
|
|
|
|
|
client_3 = hass.states.get("device_tracker.client_3")
|
|
|
|
assert client_3
|
|
|
|
|
|
|
|
# Set SSID filter
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
controller.config_entry, options={CONF_SSID_FILTER: ["ssid"]},
|
2020-02-13 00:15:08 +00:00
|
|
|
)
|
2020-04-17 06:39:01 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-02-13 00:15:08 +00:00
|
|
|
|
|
|
|
client_3 = hass.states.get("device_tracker.client_3")
|
2020-03-16 11:10:45 +00:00
|
|
|
assert not client_3
|
2020-02-13 00:15:08 +00:00
|
|
|
|
|
|
|
client_3_copy = copy(CLIENT_3)
|
|
|
|
client_3_copy["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
2020-05-04 17:29:49 +00:00
|
|
|
event = {"meta": {"message": MESSAGE_CLIENT}, "data": [client_3_copy]}
|
2020-02-13 00:15:08 +00:00
|
|
|
controller.api.message_handler(event)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# SSID filter active even though time stamp should mark as home
|
|
|
|
client_3 = hass.states.get("device_tracker.client_3")
|
2020-03-16 11:10:45 +00:00
|
|
|
assert not client_3
|
2020-02-13 00:15:08 +00:00
|
|
|
|
|
|
|
# Remove SSID filter
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
controller.config_entry, options={CONF_SSID_FILTER: []},
|
|
|
|
)
|
2020-05-04 17:29:49 +00:00
|
|
|
event = {"meta": {"message": MESSAGE_CLIENT}, "data": [client_3_copy]}
|
2020-02-13 00:15:08 +00:00
|
|
|
controller.api.message_handler(event)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_3 = hass.states.get("device_tracker.client_3")
|
|
|
|
assert client_3.state == "home"
|
|
|
|
|
2019-07-29 21:13:04 +00:00
|
|
|
|
2019-10-03 20:23:25 +00:00
|
|
|
async def test_wireless_client_go_wired_issue(hass):
|
2019-10-02 19:43:14 +00:00
|
|
|
"""Test the solution to catch wireless device go wired UniFi issue.
|
|
|
|
|
|
|
|
UniFi has a known issue that when a wireless device goes away it sometimes gets marked as wired.
|
|
|
|
"""
|
|
|
|
client_1_client = copy(CLIENT_1)
|
|
|
|
client_1_client["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
|
|
|
|
2020-01-03 18:23:17 +00:00
|
|
|
controller = await setup_unifi_integration(hass, clients_response=[client_1_client])
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
|
2019-10-02 19:43:14 +00:00
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is not None
|
|
|
|
assert client_1.state == "home"
|
2020-04-17 06:39:01 +00:00
|
|
|
assert client_1.attributes["is_wired"] is False
|
2019-10-02 19:43:14 +00:00
|
|
|
|
|
|
|
client_1_client["is_wired"] = True
|
|
|
|
client_1_client["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
2020-05-04 17:29:49 +00:00
|
|
|
event = {"meta": {"message": MESSAGE_CLIENT}, "data": [client_1_client]}
|
2020-01-31 19:23:25 +00:00
|
|
|
controller.api.message_handler(event)
|
2019-10-02 19:43:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
2019-12-10 19:05:18 +00:00
|
|
|
assert client_1.state == "home"
|
2020-04-17 06:39:01 +00:00
|
|
|
assert client_1.attributes["is_wired"] is False
|
2019-12-10 19:05:18 +00:00
|
|
|
|
|
|
|
with patch.object(
|
2020-04-23 14:48:24 +00:00
|
|
|
dt_util, "utcnow", return_value=(dt_util.utcnow() + timedelta(minutes=5)),
|
2019-12-10 19:05:18 +00:00
|
|
|
):
|
2020-05-04 17:29:49 +00:00
|
|
|
event = {"meta": {"message": MESSAGE_CLIENT}, "data": [client_1_client]}
|
2020-01-31 19:23:25 +00:00
|
|
|
controller.api.message_handler(event)
|
2019-12-10 19:05:18 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1.state == "not_home"
|
2020-04-17 06:39:01 +00:00
|
|
|
assert client_1.attributes["is_wired"] is False
|
|
|
|
|
|
|
|
client_1_client["is_wired"] = False
|
|
|
|
client_1_client["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
|
|
|
event = {"meta": {"message": "sta:sync"}, "data": [client_1_client]}
|
|
|
|
controller.api.message_handler(event)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1.state == "home"
|
|
|
|
assert client_1.attributes["is_wired"] is False
|
|
|
|
|
|
|
|
|
|
|
|
async def test_option_ignore_wired_bug(hass):
|
|
|
|
"""Test option to ignore wired bug."""
|
|
|
|
client_1_client = copy(CLIENT_1)
|
|
|
|
client_1_client["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
|
|
|
|
|
|
|
controller = await setup_unifi_integration(
|
|
|
|
hass, options={CONF_IGNORE_WIRED_BUG: True}, clients_response=[client_1_client]
|
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
|
2020-04-17 06:39:01 +00:00
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is not None
|
|
|
|
assert client_1.state == "home"
|
|
|
|
assert client_1.attributes["is_wired"] is False
|
|
|
|
|
|
|
|
client_1_client["is_wired"] = True
|
|
|
|
client_1_client["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
|
|
|
event = {"meta": {"message": "sta:sync"}, "data": [client_1_client]}
|
|
|
|
controller.api.message_handler(event)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1.state == "home"
|
|
|
|
assert client_1.attributes["is_wired"] is True
|
2019-10-02 19:43:14 +00:00
|
|
|
|
|
|
|
client_1_client["is_wired"] = False
|
|
|
|
client_1_client["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
2020-01-31 19:23:25 +00:00
|
|
|
event = {"meta": {"message": "sta:sync"}, "data": [client_1_client]}
|
|
|
|
controller.api.message_handler(event)
|
2019-10-02 19:43:14 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1.state == "home"
|
2020-04-17 06:39:01 +00:00
|
|
|
assert client_1.attributes["is_wired"] is False
|
2019-10-02 19:43:14 +00:00
|
|
|
|
|
|
|
|
2019-10-03 20:23:25 +00:00
|
|
|
async def test_restoring_client(hass):
|
2019-07-29 21:13:04 +00:00
|
|
|
"""Test the update_items function with some clients."""
|
2019-08-18 04:34:11 +00:00
|
|
|
config_entry = config_entries.ConfigEntry(
|
2019-10-03 20:23:25 +00:00
|
|
|
version=1,
|
2020-04-23 14:48:24 +00:00
|
|
|
domain=UNIFI_DOMAIN,
|
2019-10-03 20:23:25 +00:00
|
|
|
title="Mock Title",
|
|
|
|
data=ENTRY_CONFIG,
|
|
|
|
source="test",
|
|
|
|
connection_class=config_entries.CONN_CLASS_LOCAL_POLL,
|
2019-08-18 04:34:11 +00:00
|
|
|
system_options={},
|
2019-10-03 20:23:25 +00:00
|
|
|
options={},
|
|
|
|
entry_id=1,
|
2019-08-18 04:34:11 +00:00
|
|
|
)
|
|
|
|
|
2019-07-29 21:13:04 +00:00
|
|
|
registry = await entity_registry.async_get_registry(hass)
|
|
|
|
registry.async_get_or_create(
|
2020-04-23 14:48:24 +00:00
|
|
|
TRACKER_DOMAIN,
|
|
|
|
UNIFI_DOMAIN,
|
|
|
|
f'{CLIENT_1["mac"]}-site_id',
|
2019-07-31 19:25:30 +00:00
|
|
|
suggested_object_id=CLIENT_1["hostname"],
|
2019-08-18 04:34:11 +00:00
|
|
|
config_entry=config_entry,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-07-29 21:13:04 +00:00
|
|
|
registry.async_get_or_create(
|
2020-04-23 14:48:24 +00:00
|
|
|
TRACKER_DOMAIN,
|
|
|
|
UNIFI_DOMAIN,
|
|
|
|
f'{CLIENT_2["mac"]}-site_id',
|
2019-07-31 19:25:30 +00:00
|
|
|
suggested_object_id=CLIENT_2["hostname"],
|
2019-08-18 04:34:11 +00:00
|
|
|
config_entry=config_entry,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-07-29 21:13:04 +00:00
|
|
|
|
2019-10-03 20:23:25 +00:00
|
|
|
await setup_unifi_integration(
|
|
|
|
hass,
|
2020-03-12 10:56:50 +00:00
|
|
|
options={CONF_BLOCK_CLIENT: True},
|
2019-10-03 20:23:25 +00:00
|
|
|
clients_response=[CLIENT_2],
|
|
|
|
clients_all_response=[CLIENT_1],
|
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2
|
2019-07-29 21:13:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
device_1 = hass.states.get("device_tracker.client_1")
|
2019-07-29 21:13:04 +00:00
|
|
|
assert device_1 is not None
|
2019-08-02 08:13:00 +00:00
|
|
|
|
|
|
|
|
2019-10-03 20:23:25 +00:00
|
|
|
async def test_dont_track_clients(hass):
|
2020-01-31 16:33:00 +00:00
|
|
|
"""Test don't track clients config works."""
|
2019-10-03 20:23:25 +00:00
|
|
|
await setup_unifi_integration(
|
|
|
|
hass,
|
2020-04-23 14:48:24 +00:00
|
|
|
options={CONF_TRACK_CLIENTS: False},
|
2019-10-03 20:23:25 +00:00
|
|
|
clients_response=[CLIENT_1],
|
|
|
|
devices_response=[DEVICE_1],
|
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
|
2019-08-02 08:13:00 +00:00
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is None
|
|
|
|
|
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
|
|
|
assert device_1 is not None
|
|
|
|
assert device_1.state == "not_home"
|
|
|
|
|
|
|
|
|
2019-10-03 20:23:25 +00:00
|
|
|
async def test_dont_track_devices(hass):
|
2020-01-31 16:33:00 +00:00
|
|
|
"""Test don't track devices config works."""
|
2019-10-03 20:23:25 +00:00
|
|
|
await setup_unifi_integration(
|
|
|
|
hass,
|
2020-04-23 14:48:24 +00:00
|
|
|
options={CONF_TRACK_DEVICES: False},
|
2019-10-03 20:23:25 +00:00
|
|
|
clients_response=[CLIENT_1],
|
|
|
|
devices_response=[DEVICE_1],
|
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
|
2019-08-02 08:13:00 +00:00
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is not None
|
|
|
|
assert client_1.state == "not_home"
|
|
|
|
|
|
|
|
device_1 = hass.states.get("device_tracker.device_1")
|
|
|
|
assert device_1 is None
|
2019-08-02 21:51:06 +00:00
|
|
|
|
|
|
|
|
2019-10-03 20:23:25 +00:00
|
|
|
async def test_dont_track_wired_clients(hass):
|
2020-01-31 16:33:00 +00:00
|
|
|
"""Test don't track wired clients config works."""
|
2019-10-03 20:23:25 +00:00
|
|
|
await setup_unifi_integration(
|
|
|
|
hass,
|
2020-04-23 14:48:24 +00:00
|
|
|
options={CONF_TRACK_WIRED_CLIENTS: False},
|
2019-10-03 20:23:25 +00:00
|
|
|
clients_response=[CLIENT_1, CLIENT_2],
|
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
|
2019-08-02 21:51:06 +00:00
|
|
|
|
|
|
|
client_1 = hass.states.get("device_tracker.client_1")
|
|
|
|
assert client_1 is not None
|
|
|
|
assert client_1.state == "not_home"
|
|
|
|
|
|
|
|
client_2 = hass.states.get("device_tracker.client_2")
|
|
|
|
assert client_2 is None
|