Make device tracker entities work better (#63328)
Co-authored-by: Franck Nijhof <git@frenck.dev> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
d4310f0d70
commit
2b4bb49eb7
32 changed files with 546 additions and 357 deletions
|
@ -19,7 +19,7 @@ from homeassistant.const import (
|
|||
STATE_HOME,
|
||||
STATE_NOT_HOME,
|
||||
)
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.util import slugify
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
|
@ -41,6 +41,9 @@ MOCK_BYTES_TOTAL = [60000000000, 50000000000]
|
|||
MOCK_CURRENT_TRANSFER_RATES = [20000000, 10000000]
|
||||
MOCK_LOAD_AVG = [1.1, 1.2, 1.3]
|
||||
MOCK_TEMPERATURES = {"2.4GHz": 40, "5.0GHz": 0, "CPU": 71.2}
|
||||
MOCK_MAC_1 = "a1:b1:c1:d1:e1:f1"
|
||||
MOCK_MAC_2 = "a2:b2:c2:d2:e2:f2"
|
||||
MOCK_MAC_3 = "a3:b3:c3:d3:e3:f3"
|
||||
|
||||
SENSOR_NAMES = [
|
||||
"Devices Connected",
|
||||
|
@ -61,8 +64,8 @@ SENSOR_NAMES = [
|
|||
def mock_devices_fixture():
|
||||
"""Mock a list of devices."""
|
||||
return {
|
||||
"a1:b1:c1:d1:e1:f1": Device("a1:b1:c1:d1:e1:f1", "192.168.1.2", "Test"),
|
||||
"a2:b2:c2:d2:e2:f2": Device("a2:b2:c2:d2:e2:f2", "192.168.1.3", "TestTwo"),
|
||||
MOCK_MAC_1: Device(MOCK_MAC_1, "192.168.1.2", "Test"),
|
||||
MOCK_MAC_2: Device(MOCK_MAC_2, "192.168.1.3", "TestTwo"),
|
||||
}
|
||||
|
||||
|
||||
|
@ -74,6 +77,26 @@ def mock_available_temps_list():
|
|||
return [True, False]
|
||||
|
||||
|
||||
@pytest.fixture(name="create_device_registry_devices")
|
||||
def create_device_registry_devices_fixture(hass):
|
||||
"""Create device registry devices so the device tracker entities are enabled."""
|
||||
dev_reg = dr.async_get(hass)
|
||||
config_entry = MockConfigEntry(domain="something_else")
|
||||
|
||||
for idx, device in enumerate(
|
||||
(
|
||||
MOCK_MAC_1,
|
||||
MOCK_MAC_2,
|
||||
MOCK_MAC_3,
|
||||
)
|
||||
):
|
||||
dev_reg.async_get_or_create(
|
||||
name=f"Device {idx}",
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, device)},
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(name="connect")
|
||||
def mock_controller_connect(mock_devices, mock_available_temps):
|
||||
"""Mock a successful connection."""
|
||||
|
@ -109,7 +132,13 @@ def mock_controller_connect(mock_devices, mock_available_temps):
|
|||
yield service_mock
|
||||
|
||||
|
||||
async def test_sensors(hass, connect, mock_devices, mock_available_temps):
|
||||
async def test_sensors(
|
||||
hass,
|
||||
connect,
|
||||
mock_devices,
|
||||
mock_available_temps,
|
||||
create_device_registry_devices,
|
||||
):
|
||||
"""Test creating an AsusWRT sensor."""
|
||||
entity_reg = er.async_get(hass)
|
||||
|
||||
|
@ -161,10 +190,8 @@ async def test_sensors(hass, connect, mock_devices, mock_available_temps):
|
|||
assert not hass.states.get(f"{sensor_prefix}_cpu_temperature")
|
||||
|
||||
# add one device and remove another
|
||||
mock_devices.pop("a1:b1:c1:d1:e1:f1")
|
||||
mock_devices["a3:b3:c3:d3:e3:f3"] = Device(
|
||||
"a3:b3:c3:d3:e3:f3", "192.168.1.4", "TestThree"
|
||||
)
|
||||
mock_devices.pop(MOCK_MAC_1)
|
||||
mock_devices[MOCK_MAC_3] = Device(MOCK_MAC_3, "192.168.1.4", "TestThree")
|
||||
|
||||
async_fire_time_changed(hass, utcnow() + timedelta(seconds=30))
|
||||
await hass.async_block_till_done()
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
"""Test Device Tracker config entry things."""
|
||||
from homeassistant.components.device_tracker import config_entry
|
||||
from homeassistant.components.device_tracker import DOMAIN, config_entry as ce
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
def test_tracker_entity():
|
||||
"""Test tracker entity."""
|
||||
|
||||
class TestEntry(config_entry.TrackerEntity):
|
||||
class TestEntry(ce.TrackerEntity):
|
||||
"""Mock tracker class."""
|
||||
|
||||
should_poll = False
|
||||
|
@ -17,3 +20,111 @@ def test_tracker_entity():
|
|||
instance.should_poll = True
|
||||
|
||||
assert not instance.force_update
|
||||
|
||||
|
||||
async def test_cleanup_legacy(hass, enable_custom_integrations):
|
||||
"""Test we clean up devices created by old device tracker."""
|
||||
dev_reg = dr.async_get(hass)
|
||||
ent_reg = er.async_get(hass)
|
||||
config_entry = MockConfigEntry(domain="test")
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
device1 = dev_reg.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id, identifiers={(DOMAIN, "device1")}
|
||||
)
|
||||
device2 = dev_reg.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id, identifiers={(DOMAIN, "device2")}
|
||||
)
|
||||
device3 = dev_reg.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id, identifiers={(DOMAIN, "device3")}
|
||||
)
|
||||
|
||||
# Device with light + device tracker entity
|
||||
entity1a = ent_reg.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"entity1a-unique",
|
||||
config_entry=config_entry,
|
||||
device_id=device1.id,
|
||||
)
|
||||
entity1b = ent_reg.async_get_or_create(
|
||||
"light",
|
||||
"test",
|
||||
"entity1b-unique",
|
||||
config_entry=config_entry,
|
||||
device_id=device1.id,
|
||||
)
|
||||
# Just device tracker entity
|
||||
entity2a = ent_reg.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"entity2a-unique",
|
||||
config_entry=config_entry,
|
||||
device_id=device2.id,
|
||||
)
|
||||
# Device with no device tracker entities
|
||||
entity3a = ent_reg.async_get_or_create(
|
||||
"light",
|
||||
"test",
|
||||
"entity3a-unique",
|
||||
config_entry=config_entry,
|
||||
device_id=device3.id,
|
||||
)
|
||||
# Device tracker but no device
|
||||
entity4a = ent_reg.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"entity4a-unique",
|
||||
config_entry=config_entry,
|
||||
)
|
||||
# Completely different entity
|
||||
entity5a = ent_reg.async_get_or_create(
|
||||
"light",
|
||||
"test",
|
||||
"entity4a-unique",
|
||||
config_entry=config_entry,
|
||||
)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, DOMAIN)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
for entity in (entity1a, entity1b, entity3a, entity4a, entity5a):
|
||||
assert ent_reg.async_get(entity.entity_id) is not None
|
||||
|
||||
# We've removed device so device ID cleared
|
||||
assert ent_reg.async_get(entity2a.entity_id).device_id is None
|
||||
# Removed because only had device tracker entity
|
||||
assert dev_reg.async_get(device2.id) is None
|
||||
|
||||
|
||||
async def test_register_mac(hass):
|
||||
"""Test registering a mac."""
|
||||
dev_reg = dr.async_get(hass)
|
||||
ent_reg = er.async_get(hass)
|
||||
|
||||
config_entry = MockConfigEntry(domain="test")
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
mac1 = "12:34:56:AB:CD:EF"
|
||||
|
||||
entity_entry_1 = ent_reg.async_get_or_create(
|
||||
"device_tracker",
|
||||
"test",
|
||||
mac1 + "yo1",
|
||||
original_name="name 1",
|
||||
config_entry=config_entry,
|
||||
disabled_by=er.RegistryEntryDisabler.INTEGRATION,
|
||||
)
|
||||
|
||||
ce._async_register_mac(hass, "test", mac1, mac1 + "yo1")
|
||||
|
||||
dev_reg.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, mac1)},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_entry_1 = ent_reg.async_get(entity_entry_1.entity_id)
|
||||
|
||||
assert entity_entry_1.disabled_by is None
|
||||
|
|
|
@ -14,25 +14,33 @@ from homeassistant.components.device_tracker.const import (
|
|||
SOURCE_TYPE_ROUTER,
|
||||
)
|
||||
from homeassistant.const import ATTR_BATTERY_LEVEL, STATE_HOME, STATE_NOT_HOME
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_scanner_entity_device_tracker(hass, enable_custom_integrations):
|
||||
"""Test ScannerEntity based device tracker."""
|
||||
# Make device tied to other integration so device tracker entities get enabled
|
||||
dr.async_get(hass).async_get_or_create(
|
||||
name="Device from other integration",
|
||||
config_entry_id=MockConfigEntry().entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "ad:de:ef:be:ed:fe")},
|
||||
)
|
||||
|
||||
config_entry = MockConfigEntry(domain="test")
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, DOMAIN)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_id = "device_tracker.unnamed_device"
|
||||
entity_id = "device_tracker.test_ad_de_ef_be_ed_fe"
|
||||
entity_state = hass.states.get(entity_id)
|
||||
assert entity_state.attributes == {
|
||||
ATTR_SOURCE_TYPE: SOURCE_TYPE_ROUTER,
|
||||
ATTR_BATTERY_LEVEL: 100,
|
||||
ATTR_IP: "0.0.0.0",
|
||||
ATTR_MAC: "ad:de:ef:be:ed:fe:",
|
||||
ATTR_MAC: "ad:de:ef:be:ed:fe",
|
||||
ATTR_HOST_NAME: "test.hostname.org",
|
||||
}
|
||||
assert entity_state.state == STATE_NOT_HOME
|
||||
|
|
|
@ -3,6 +3,8 @@ from unittest.mock import AsyncMock, patch
|
|||
|
||||
import pytest
|
||||
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from .const import (
|
||||
DATA_CALL_GET_CALLS_LOG,
|
||||
DATA_CONNECTION_GET_STATUS,
|
||||
|
@ -12,6 +14,8 @@ from .const import (
|
|||
WIFI_GET_GLOBAL_CONFIG,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_path():
|
||||
|
@ -20,8 +24,30 @@ def mock_path():
|
|||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_device_registry_devices(hass):
|
||||
"""Create device registry devices so the device tracker entities are enabled."""
|
||||
dev_reg = dr.async_get(hass)
|
||||
config_entry = MockConfigEntry(domain="something_else")
|
||||
|
||||
for idx, device in enumerate(
|
||||
(
|
||||
"68:A3:78:00:00:00",
|
||||
"8C:97:EA:00:00:00",
|
||||
"DE:00:B0:00:00:00",
|
||||
"DC:00:B0:00:00:00",
|
||||
"5E:65:55:00:00:00",
|
||||
)
|
||||
):
|
||||
dev_reg.async_get_or_create(
|
||||
name=f"Device {idx}",
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, device)},
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(name="router")
|
||||
def mock_router():
|
||||
def mock_router(mock_device_registry_devices):
|
||||
"""Mock a successful connection."""
|
||||
with patch("homeassistant.components.freebox.router.Freepybox") as service_mock:
|
||||
instance = service_mock.return_value
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
"""The tests for the Mikrotik device tracker platform."""
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import mikrotik
|
||||
import homeassistant.components.device_tracker as device_tracker
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
|
@ -15,6 +17,25 @@ from tests.common import MockConfigEntry, patch
|
|||
DEFAULT_DETECTION_TIME = timedelta(seconds=300)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_device_registry_devices(hass):
|
||||
"""Create device registry devices so the device tracker entities are enabled."""
|
||||
dev_reg = dr.async_get(hass)
|
||||
config_entry = MockConfigEntry(domain="something_else")
|
||||
|
||||
for idx, device in enumerate(
|
||||
(
|
||||
"00:00:00:00:00:01",
|
||||
"00:00:00:00:00:02",
|
||||
)
|
||||
):
|
||||
dev_reg.async_get_or_create(
|
||||
name=f"Device {idx}",
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, device)},
|
||||
)
|
||||
|
||||
|
||||
def mock_command(self, cmd, params=None):
|
||||
"""Mock the Mikrotik command method."""
|
||||
if cmd == mikrotik.const.MIKROTIK_SERVICES[mikrotik.const.IS_WIRELESS]:
|
||||
|
@ -39,7 +60,9 @@ async def test_platform_manually_configured(hass):
|
|||
assert mikrotik.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_device_trackers(hass, legacy_patchable_time):
|
||||
async def test_device_trackers(
|
||||
hass, legacy_patchable_time, mock_device_registry_devices
|
||||
):
|
||||
"""Test device_trackers created by mikrotik."""
|
||||
|
||||
# test devices are added from wireless list only
|
||||
|
|
|
@ -16,6 +16,7 @@ from homeassistant.components.ruckus_unleashed.const import (
|
|||
API_VERSION,
|
||||
)
|
||||
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
@ -68,6 +69,13 @@ def mock_config_entry() -> MockConfigEntry:
|
|||
async def init_integration(hass) -> MockConfigEntry:
|
||||
"""Set up the Ruckus Unleashed integration in Home Assistant."""
|
||||
entry = mock_config_entry()
|
||||
entry.add_to_hass(hass)
|
||||
# Make device tied to other integration so device tracker entities get enabled
|
||||
dr.async_get(hass).async_get_or_create(
|
||||
name="Device from other integration",
|
||||
config_entry_id=MockConfigEntry().entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, TEST_CLIENT[API_MAC])},
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.components.ruckus_unleashed.Ruckus.connect",
|
||||
return_value=None,
|
||||
|
@ -86,7 +94,6 @@ async def init_integration(hass) -> MockConfigEntry:
|
|||
TEST_CLIENT[API_MAC]: TEST_CLIENT,
|
||||
},
|
||||
):
|
||||
entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
|
|
@ -3,10 +3,8 @@ from datetime import timedelta
|
|||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.ruckus_unleashed import API_MAC, DOMAIN
|
||||
from homeassistant.components.ruckus_unleashed.const import API_AP, API_ID, API_NAME
|
||||
from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_UNAVAILABLE
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.util import utcnow
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
@ -112,24 +110,3 @@ async def test_restoring_clients(hass):
|
|||
device = hass.states.get(TEST_CLIENT_ENTITY_ID)
|
||||
assert device is not None
|
||||
assert device.state == STATE_NOT_HOME
|
||||
|
||||
|
||||
async def test_client_device_setup(hass):
|
||||
"""Test a client device is created."""
|
||||
await init_integration(hass)
|
||||
|
||||
router_info = DEFAULT_AP_INFO[API_AP][API_ID]["1"]
|
||||
|
||||
device_registry = dr.async_get(hass)
|
||||
client_device = device_registry.async_get_device(
|
||||
identifiers={},
|
||||
connections={(CONNECTION_NETWORK_MAC, TEST_CLIENT[API_MAC])},
|
||||
)
|
||||
router_device = device_registry.async_get_device(
|
||||
identifiers={(CONNECTION_NETWORK_MAC, router_info[API_MAC])},
|
||||
connections={(CONNECTION_NETWORK_MAC, router_info[API_MAC])},
|
||||
)
|
||||
|
||||
assert client_device
|
||||
assert client_device.name == TEST_CLIENT[API_NAME]
|
||||
assert client_device.via_device_id == router_device.id
|
||||
|
|
|
@ -6,6 +6,10 @@ from unittest.mock import patch
|
|||
from aiounifi.websocket import SIGNAL_CONNECTION_STATE, SIGNAL_DATA
|
||||
import pytest
|
||||
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_unifi_websocket():
|
||||
|
@ -34,3 +38,27 @@ def mock_discovery():
|
|||
return_value=None,
|
||||
) as mock:
|
||||
yield mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_device_registry(hass):
|
||||
"""Mock device registry."""
|
||||
dev_reg = dr.async_get(hass)
|
||||
config_entry = MockConfigEntry(domain="something_else")
|
||||
|
||||
for idx, device in enumerate(
|
||||
(
|
||||
"00:00:00:00:00:01",
|
||||
"00:00:00:00:00:02",
|
||||
"00:00:00:00:00:03",
|
||||
"00:00:00:00:00:04",
|
||||
"00:00:00:00:00:05",
|
||||
"00:00:00:00:01:01",
|
||||
"00:00:00:00:02:02",
|
||||
)
|
||||
):
|
||||
dev_reg.async_get_or_create(
|
||||
name=f"Device {idx}",
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, device)},
|
||||
)
|
||||
|
|
|
@ -346,7 +346,9 @@ async def test_reset_fails(hass, aioclient_mock):
|
|||
assert result is False
|
||||
|
||||
|
||||
async def test_connection_state_signalling(hass, aioclient_mock, mock_unifi_websocket):
|
||||
async def test_connection_state_signalling(
|
||||
hass, aioclient_mock, mock_unifi_websocket, mock_device_registry
|
||||
):
|
||||
"""Verify connection statesignalling and connection state are working."""
|
||||
client = {
|
||||
"hostname": "client",
|
||||
|
|
|
@ -38,7 +38,9 @@ async def test_no_entities(hass, aioclient_mock):
|
|||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_tracked_wireless_clients(hass, aioclient_mock, mock_unifi_websocket):
|
||||
async def test_tracked_wireless_clients(
|
||||
hass, aioclient_mock, mock_unifi_websocket, mock_device_registry
|
||||
):
|
||||
"""Verify tracking of wireless clients."""
|
||||
client = {
|
||||
"ap_mac": "00:00:00:00:02:01",
|
||||
|
@ -157,7 +159,9 @@ async def test_tracked_wireless_clients(hass, aioclient_mock, mock_unifi_websock
|
|||
assert hass.states.get("device_tracker.client").state == STATE_HOME
|
||||
|
||||
|
||||
async def test_tracked_clients(hass, aioclient_mock, mock_unifi_websocket):
|
||||
async def test_tracked_clients(
|
||||
hass, aioclient_mock, mock_unifi_websocket, mock_device_registry
|
||||
):
|
||||
"""Test the update_items function with some clients."""
|
||||
client_1 = {
|
||||
"ap_mac": "00:00:00:00:02:01",
|
||||
|
@ -234,7 +238,9 @@ async def test_tracked_clients(hass, aioclient_mock, mock_unifi_websocket):
|
|||
assert hass.states.get("device_tracker.client_1").state == STATE_HOME
|
||||
|
||||
|
||||
async def test_tracked_devices(hass, aioclient_mock, mock_unifi_websocket):
|
||||
async def test_tracked_devices(
|
||||
hass, aioclient_mock, mock_unifi_websocket, mock_device_registry
|
||||
):
|
||||
"""Test the update_items function with some devices."""
|
||||
device_1 = {
|
||||
"board_rev": 3,
|
||||
|
@ -321,45 +327,10 @@ async def test_tracked_devices(hass, aioclient_mock, mock_unifi_websocket):
|
|||
assert hass.states.get("device_tracker.device_1").state == STATE_UNAVAILABLE
|
||||
assert hass.states.get("device_tracker.device_2").state == STATE_HOME
|
||||
|
||||
# Update device registry when device is upgraded
|
||||
|
||||
event = {
|
||||
"_id": "5eae7fe02ab79c00f9d38960",
|
||||
"datetime": "2020-05-09T20:06:37Z",
|
||||
"key": "EVT_SW_Upgraded",
|
||||
"msg": f'Switch[{device_2["mac"]}] was upgraded from "{device_2["version"]}" to "4.3.13.11253"',
|
||||
"subsystem": "lan",
|
||||
"sw": device_2["mac"],
|
||||
"sw_name": device_2["name"],
|
||||
"time": 1589054797635,
|
||||
"version_from": {device_2["version"]},
|
||||
"version_to": "4.3.13.11253",
|
||||
}
|
||||
|
||||
device_2["version"] = event["version_to"]
|
||||
mock_unifi_websocket(
|
||||
data={
|
||||
"meta": {"message": MESSAGE_DEVICE},
|
||||
"data": [device_2],
|
||||
}
|
||||
)
|
||||
mock_unifi_websocket(
|
||||
data={
|
||||
"meta": {"message": MESSAGE_EVENT},
|
||||
"data": [event],
|
||||
}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device registry has been updated
|
||||
entity_registry = er.async_get(hass)
|
||||
entry = entity_registry.async_get("device_tracker.device_2")
|
||||
device_registry = dr.async_get(hass)
|
||||
device = device_registry.async_get(entry.device_id)
|
||||
assert device.sw_version == event["version_to"]
|
||||
|
||||
|
||||
async def test_remove_clients(hass, aioclient_mock, mock_unifi_websocket):
|
||||
async def test_remove_clients(
|
||||
hass, aioclient_mock, mock_unifi_websocket, mock_device_registry
|
||||
):
|
||||
"""Test the remove_items function with some clients."""
|
||||
client_1 = {
|
||||
"essid": "ssid",
|
||||
|
@ -399,7 +370,7 @@ async def test_remove_clients(hass, aioclient_mock, mock_unifi_websocket):
|
|||
|
||||
|
||||
async def test_remove_client_but_keep_device_entry(
|
||||
hass, aioclient_mock, mock_unifi_websocket
|
||||
hass, aioclient_mock, mock_unifi_websocket, mock_device_registry
|
||||
):
|
||||
"""Test that unifi entity base remove config entry id from a multi integration device registry entry."""
|
||||
client_1 = {
|
||||
|
@ -424,7 +395,7 @@ async def test_remove_client_but_keep_device_entry(
|
|||
"unique_id",
|
||||
device_id=device_entry.id,
|
||||
)
|
||||
assert len(device_entry.config_entries) == 2
|
||||
assert len(device_entry.config_entries) == 3
|
||||
|
||||
mock_unifi_websocket(
|
||||
data={
|
||||
|
@ -438,10 +409,12 @@ async def test_remove_client_but_keep_device_entry(
|
|||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 0
|
||||
|
||||
device_entry = device_registry.async_get(other_entity.device_id)
|
||||
assert len(device_entry.config_entries) == 1
|
||||
assert len(device_entry.config_entries) == 2
|
||||
|
||||
|
||||
async def test_controller_state_change(hass, aioclient_mock, mock_unifi_websocket):
|
||||
async def test_controller_state_change(
|
||||
hass, aioclient_mock, mock_unifi_websocket, mock_device_registry
|
||||
):
|
||||
"""Verify entities state reflect on controller becoming unavailable."""
|
||||
client = {
|
||||
"essid": "ssid",
|
||||
|
@ -495,7 +468,7 @@ async def test_controller_state_change(hass, aioclient_mock, mock_unifi_websocke
|
|||
|
||||
|
||||
async def test_controller_state_change_client_to_listen_on_all_state_changes(
|
||||
hass, aioclient_mock, mock_unifi_websocket
|
||||
hass, aioclient_mock, mock_unifi_websocket, mock_device_registry
|
||||
):
|
||||
"""Verify entities state reflect on controller becoming unavailable."""
|
||||
client = {
|
||||
|
@ -579,7 +552,7 @@ async def test_controller_state_change_client_to_listen_on_all_state_changes(
|
|||
assert hass.states.get("device_tracker.client").state == STATE_HOME
|
||||
|
||||
|
||||
async def test_option_track_clients(hass, aioclient_mock):
|
||||
async def test_option_track_clients(hass, aioclient_mock, mock_device_registry):
|
||||
"""Test the tracking of clients can be turned off."""
|
||||
wireless_client = {
|
||||
"essid": "ssid",
|
||||
|
@ -645,7 +618,7 @@ async def test_option_track_clients(hass, aioclient_mock):
|
|||
assert hass.states.get("device_tracker.device")
|
||||
|
||||
|
||||
async def test_option_track_wired_clients(hass, aioclient_mock):
|
||||
async def test_option_track_wired_clients(hass, aioclient_mock, mock_device_registry):
|
||||
"""Test the tracking of wired clients can be turned off."""
|
||||
wireless_client = {
|
||||
"essid": "ssid",
|
||||
|
@ -711,7 +684,7 @@ async def test_option_track_wired_clients(hass, aioclient_mock):
|
|||
assert hass.states.get("device_tracker.device")
|
||||
|
||||
|
||||
async def test_option_track_devices(hass, aioclient_mock):
|
||||
async def test_option_track_devices(hass, aioclient_mock, mock_device_registry):
|
||||
"""Test the tracking of devices can be turned off."""
|
||||
client = {
|
||||
"hostname": "client",
|
||||
|
@ -764,7 +737,9 @@ async def test_option_track_devices(hass, aioclient_mock):
|
|||
assert hass.states.get("device_tracker.device")
|
||||
|
||||
|
||||
async def test_option_ssid_filter(hass, aioclient_mock, mock_unifi_websocket):
|
||||
async def test_option_ssid_filter(
|
||||
hass, aioclient_mock, mock_unifi_websocket, mock_device_registry
|
||||
):
|
||||
"""Test the SSID filter works.
|
||||
|
||||
Client will travel from a supported SSID to an unsupported ssid.
|
||||
|
@ -896,7 +871,7 @@ async def test_option_ssid_filter(hass, aioclient_mock, mock_unifi_websocket):
|
|||
|
||||
|
||||
async def test_wireless_client_go_wired_issue(
|
||||
hass, aioclient_mock, mock_unifi_websocket
|
||||
hass, aioclient_mock, mock_unifi_websocket, mock_device_registry
|
||||
):
|
||||
"""Test the solution to catch wireless device go wired UniFi issue.
|
||||
|
||||
|
@ -979,7 +954,9 @@ async def test_wireless_client_go_wired_issue(
|
|||
assert client_state.attributes["is_wired"] is False
|
||||
|
||||
|
||||
async def test_option_ignore_wired_bug(hass, aioclient_mock, mock_unifi_websocket):
|
||||
async def test_option_ignore_wired_bug(
|
||||
hass, aioclient_mock, mock_unifi_websocket, mock_device_registry
|
||||
):
|
||||
"""Test option to ignore wired bug."""
|
||||
client = {
|
||||
"ap_mac": "00:00:00:00:02:01",
|
||||
|
@ -1061,7 +1038,7 @@ async def test_option_ignore_wired_bug(hass, aioclient_mock, mock_unifi_websocke
|
|||
assert client_state.attributes["is_wired"] is False
|
||||
|
||||
|
||||
async def test_restoring_client(hass, aioclient_mock):
|
||||
async def test_restoring_client(hass, aioclient_mock, mock_device_registry):
|
||||
"""Verify clients are restored from clients_all if they ever was registered to entity registry."""
|
||||
client = {
|
||||
"hostname": "client",
|
||||
|
@ -1115,7 +1092,7 @@ async def test_restoring_client(hass, aioclient_mock):
|
|||
assert not hass.states.get("device_tracker.not_restored")
|
||||
|
||||
|
||||
async def test_dont_track_clients(hass, aioclient_mock):
|
||||
async def test_dont_track_clients(hass, aioclient_mock, mock_device_registry):
|
||||
"""Test don't track clients config works."""
|
||||
wireless_client = {
|
||||
"essid": "ssid",
|
||||
|
@ -1175,7 +1152,7 @@ async def test_dont_track_clients(hass, aioclient_mock):
|
|||
assert hass.states.get("device_tracker.device")
|
||||
|
||||
|
||||
async def test_dont_track_devices(hass, aioclient_mock):
|
||||
async def test_dont_track_devices(hass, aioclient_mock, mock_device_registry):
|
||||
"""Test don't track devices config works."""
|
||||
client = {
|
||||
"hostname": "client",
|
||||
|
@ -1224,7 +1201,7 @@ async def test_dont_track_devices(hass, aioclient_mock):
|
|||
assert hass.states.get("device_tracker.device")
|
||||
|
||||
|
||||
async def test_dont_track_wired_clients(hass, aioclient_mock):
|
||||
async def test_dont_track_wired_clients(hass, aioclient_mock, mock_device_registry):
|
||||
"""Test don't track wired clients config works."""
|
||||
wireless_client = {
|
||||
"essid": "ssid",
|
||||
|
|
|
@ -5,7 +5,6 @@ from unittest.mock import patch
|
|||
from aiounifi.controller import MESSAGE_CLIENT_REMOVED, MESSAGE_EVENT
|
||||
|
||||
from homeassistant import config_entries, core
|
||||
from homeassistant.components.device_tracker import DOMAIN as TRACKER_DOMAIN
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.components.unifi.const import (
|
||||
CONF_BLOCK_CLIENT,
|
||||
|
@ -784,8 +783,6 @@ async def test_ignore_multiple_poe_clients_on_same_port(hass, aioclient_mock):
|
|||
devices_response=[DEVICE_1],
|
||||
)
|
||||
|
||||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 3
|
||||
|
||||
switch_1 = hass.states.get("switch.poe_client_1")
|
||||
switch_2 = hass.states.get("switch.poe_client_2")
|
||||
assert switch_1 is None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue