UniFi - Store controller in config_entry.entry_id (#34553)
* Store controller in config_entry.entry_id * Clean up imports
This commit is contained in:
1 parent
a0fbf9ba47
commit
c3689d7416
13 files changed
+190
-182
No files matched your search
@@ -7,8 +7,7 @@ from aiounifi.websocket import SIGNAL_DATA, STATE_DISCONNECTED, STATE_RUNNING
|
||||
from asynctest import patch
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import unifi
|
||||
import homeassistant.components.device_tracker as device_tracker
|
||||
from homeassistant.components.device_tracker import DOMAIN as TRACKER_DOMAIN
|
||||
from homeassistant.components.unifi.const import (
|
||||
CONF_BLOCK_CLIENT,
|
||||
CONF_IGNORE_WIRED_BUG,
|
||||
@@ -16,6 +15,7 @@ from homeassistant.components.unifi.const import (
|
||||
CONF_TRACK_CLIENTS,
|
||||
CONF_TRACK_DEVICES,
|
||||
CONF_TRACK_WIRED_CLIENTS,
|
||||
DOMAIN as UNIFI_DOMAIN,
|
||||
)
|
||||
from homeassistant.const import STATE_UNAVAILABLE
|
||||
from homeassistant.helpers import entity_registry
|
||||
@@ -99,18 +99,18 @@ async def test_platform_manually_configured(hass):
|
||||
"""Test that nothing happens when configuring unifi through device tracker platform."""
|
||||
assert (
|
||||
await async_setup_component(
|
||||
hass, device_tracker.DOMAIN, {device_tracker.DOMAIN: {"platform": "unifi"}}
|
||||
hass, TRACKER_DOMAIN, {TRACKER_DOMAIN: {"platform": UNIFI_DOMAIN}}
|
||||
)
|
||||
is False
|
||||
)
|
||||
assert unifi.DOMAIN not in hass.data
|
||||
assert UNIFI_DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_clients(hass):
|
||||
"""Test the update_clients function when no clients are found."""
|
||||
await setup_unifi_integration(hass)
|
||||
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 0
|
||||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_tracked_devices(hass):
|
||||
@@ -125,7 +125,7 @@ async def test_tracked_devices(hass):
|
||||
devices_response=[DEVICE_1, DEVICE_2],
|
||||
known_wireless_clients=(CLIENT_4["mac"],),
|
||||
)
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 5
|
||||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 5
|
||||
|
||||
client_1 = hass.states.get("device_tracker.client_1")
|
||||
assert client_1 is not None
|
||||
@@ -186,7 +186,7 @@ async def test_remove_clients(hass):
|
||||
controller = await setup_unifi_integration(
|
||||
hass, clients_response=[CLIENT_1, CLIENT_2]
|
||||
)
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 2
|
||||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2
|
||||
|
||||
client_1 = hass.states.get("device_tracker.client_1")
|
||||
assert client_1 is not None
|
||||
@@ -201,7 +201,7 @@ async def test_remove_clients(hass):
|
||||
controller.api.session_handler(SIGNAL_DATA)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 1
|
||||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
|
||||
|
||||
client_1 = hass.states.get("device_tracker.client_1")
|
||||
assert client_1 is None
|
||||
@@ -215,7 +215,7 @@ async def test_controller_state_change(hass):
|
||||
controller = await setup_unifi_integration(
|
||||
hass, clients_response=[CLIENT_1], devices_response=[DEVICE_1],
|
||||
)
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 2
|
||||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2
|
||||
|
||||
# Controller unavailable
|
||||
controller.async_unifi_signalling_callback(
|
||||
@@ -245,7 +245,7 @@ async def test_option_track_clients(hass):
|
||||
controller = await setup_unifi_integration(
|
||||
hass, clients_response=[CLIENT_1, CLIENT_2], devices_response=[DEVICE_1],
|
||||
)
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 3
|
||||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 3
|
||||
|
||||
client_1 = hass.states.get("device_tracker.client_1")
|
||||
assert client_1 is not None
|
||||
@@ -290,7 +290,7 @@ async def test_option_track_wired_clients(hass):
|
||||
controller = await setup_unifi_integration(
|
||||
hass, clients_response=[CLIENT_1, CLIENT_2], devices_response=[DEVICE_1],
|
||||
)
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 3
|
||||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 3
|
||||
|
||||
client_1 = hass.states.get("device_tracker.client_1")
|
||||
assert client_1 is not None
|
||||
@@ -335,7 +335,7 @@ async def test_option_track_devices(hass):
|
||||
controller = await setup_unifi_integration(
|
||||
hass, clients_response=[CLIENT_1, CLIENT_2], devices_response=[DEVICE_1],
|
||||
)
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 3
|
||||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 3
|
||||
|
||||
client_1 = hass.states.get("device_tracker.client_1")
|
||||
assert client_1 is not None
|
||||
@@ -378,7 +378,7 @@ async def test_option_track_devices(hass):
|
||||
async def test_option_ssid_filter(hass):
|
||||
"""Test the SSID filter works."""
|
||||
controller = await setup_unifi_integration(hass, clients_response=[CLIENT_3])
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 1
|
||||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
|
||||
|
||||
client_3 = hass.states.get("device_tracker.client_3")
|
||||
assert client_3
|
||||
@@ -423,7 +423,7 @@ async def test_wireless_client_go_wired_issue(hass):
|
||||
client_1_client["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
||||
|
||||
controller = await setup_unifi_integration(hass, clients_response=[client_1_client])
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 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
|
||||
@@ -441,9 +441,7 @@ async def test_wireless_client_go_wired_issue(hass):
|
||||
assert client_1.attributes["is_wired"] is False
|
||||
|
||||
with patch.object(
|
||||
unifi.device_tracker.dt_util,
|
||||
"utcnow",
|
||||
return_value=(dt_util.utcnow() + timedelta(minutes=5)),
|
||||
dt_util, "utcnow", return_value=(dt_util.utcnow() + timedelta(minutes=5)),
|
||||
):
|
||||
event = {"meta": {"message": "sta:sync"}, "data": [client_1_client]}
|
||||
controller.api.message_handler(event)
|
||||
@@ -472,7 +470,7 @@ async def test_option_ignore_wired_bug(hass):
|
||||
controller = await setup_unifi_integration(
|
||||
hass, options={CONF_IGNORE_WIRED_BUG: True}, clients_response=[client_1_client]
|
||||
)
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 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
|
||||
@@ -504,7 +502,7 @@ async def test_restoring_client(hass):
|
||||
"""Test the update_items function with some clients."""
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
version=1,
|
||||
domain=unifi.DOMAIN,
|
||||
domain=UNIFI_DOMAIN,
|
||||
title="Mock Title",
|
||||
data=ENTRY_CONFIG,
|
||||
source="test",
|
||||
@@ -516,16 +514,16 @@ async def test_restoring_client(hass):
|
||||
|
||||
registry = await entity_registry.async_get_registry(hass)
|
||||
registry.async_get_or_create(
|
||||
device_tracker.DOMAIN,
|
||||
unifi.DOMAIN,
|
||||
"{}-site_id".format(CLIENT_1["mac"]),
|
||||
TRACKER_DOMAIN,
|
||||
UNIFI_DOMAIN,
|
||||
f'{CLIENT_1["mac"]}-site_id',
|
||||
suggested_object_id=CLIENT_1["hostname"],
|
||||
config_entry=config_entry,
|
||||
)
|
||||
registry.async_get_or_create(
|
||||
device_tracker.DOMAIN,
|
||||
unifi.DOMAIN,
|
||||
"{}-site_id".format(CLIENT_2["mac"]),
|
||||
TRACKER_DOMAIN,
|
||||
UNIFI_DOMAIN,
|
||||
f'{CLIENT_2["mac"]}-site_id',
|
||||
suggested_object_id=CLIENT_2["hostname"],
|
||||
config_entry=config_entry,
|
||||
)
|
||||
@@ -536,7 +534,7 @@ async def test_restoring_client(hass):
|
||||
clients_response=[CLIENT_2],
|
||||
clients_all_response=[CLIENT_1],
|
||||
)
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 2
|
||||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2
|
||||
|
||||
device_1 = hass.states.get("device_tracker.client_1")
|
||||
assert device_1 is not None
|
||||
@@ -546,11 +544,11 @@ async def test_dont_track_clients(hass):
|
||||
"""Test don't track clients config works."""
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
options={unifi.controller.CONF_TRACK_CLIENTS: False},
|
||||
options={CONF_TRACK_CLIENTS: False},
|
||||
clients_response=[CLIENT_1],
|
||||
devices_response=[DEVICE_1],
|
||||
)
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 1
|
||||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1
|
||||
|
||||
client_1 = hass.states.get("device_tracker.client_1")
|
||||
assert client_1 is None
|
||||
@@ -564,11 +562,11 @@ async def test_dont_track_devices(hass):
|
||||
"""Test don't track devices config works."""
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
options={unifi.controller.CONF_TRACK_DEVICES: False},
|
||||
options={CONF_TRACK_DEVICES: False},
|
||||
clients_response=[CLIENT_1],
|
||||
devices_response=[DEVICE_1],
|
||||
)
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 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
|
||||
@@ -582,10 +580,10 @@ async def test_dont_track_wired_clients(hass):
|
||||
"""Test don't track wired clients config works."""
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
options={unifi.controller.CONF_TRACK_WIRED_CLIENTS: False},
|
||||
options={CONF_TRACK_WIRED_CLIENTS: False},
|
||||
clients_response=[CLIENT_1, CLIENT_2],
|
||||
)
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 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
|
||||
|
||||
Reference in new issue
Block a user