mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Use websocket client to test device removal in Unifi (#116309)
* Use websocket client to test device removal from registry * Rename client to ws_client to avoid confusion with Unifi clients * Use remove_device helper
This commit is contained in:
parent
58c7a97149
commit
7a10959e58
@ -5,7 +5,6 @@ from unittest.mock import patch
|
|||||||
|
|
||||||
from aiounifi.models.message import MessageKey
|
from aiounifi.models.message import MessageKey
|
||||||
|
|
||||||
from homeassistant import loader
|
|
||||||
from homeassistant.components import unifi
|
from homeassistant.components import unifi
|
||||||
from homeassistant.components.unifi.const import (
|
from homeassistant.components.unifi.const import (
|
||||||
CONF_ALLOW_BANDWIDTH_SENSORS,
|
CONF_ALLOW_BANDWIDTH_SENSORS,
|
||||||
@ -23,6 +22,7 @@ from .test_hub import DEFAULT_CONFIG_ENTRY_ID, setup_unifi_integration
|
|||||||
|
|
||||||
from tests.common import flush_store
|
from tests.common import flush_store
|
||||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||||
|
from tests.typing import WebSocketGenerator
|
||||||
|
|
||||||
|
|
||||||
async def test_setup_with_no_config(hass: HomeAssistant) -> None:
|
async def test_setup_with_no_config(hass: HomeAssistant) -> None:
|
||||||
@ -121,6 +121,7 @@ async def test_remove_config_entry_device(
|
|||||||
aioclient_mock: AiohttpClientMocker,
|
aioclient_mock: AiohttpClientMocker,
|
||||||
device_registry: dr.DeviceRegistry,
|
device_registry: dr.DeviceRegistry,
|
||||||
mock_unifi_websocket,
|
mock_unifi_websocket,
|
||||||
|
hass_ws_client: WebSocketGenerator,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Verify removing a device manually."""
|
"""Verify removing a device manually."""
|
||||||
client_1 = {
|
client_1 = {
|
||||||
@ -173,31 +174,39 @@ async def test_remove_config_entry_device(
|
|||||||
devices_response=[device_1],
|
devices_response=[device_1],
|
||||||
)
|
)
|
||||||
|
|
||||||
integration = await loader.async_get_integration(hass, config_entry.domain)
|
assert await async_setup_component(hass, "config", {})
|
||||||
component = await integration.async_get_component()
|
ws_client = await hass_ws_client(hass)
|
||||||
|
|
||||||
# Remove a client
|
# Try to remove an active client from UI: not allowed
|
||||||
mock_unifi_websocket(message=MessageKey.CLIENT_REMOVED, data=[client_2])
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
# Try to remove an active client: not allowed
|
|
||||||
device_entry = device_registry.async_get_device(
|
device_entry = device_registry.async_get_device(
|
||||||
connections={(dr.CONNECTION_NETWORK_MAC, client_1["mac"])}
|
connections={(dr.CONNECTION_NETWORK_MAC, client_1["mac"])}
|
||||||
)
|
)
|
||||||
assert not await component.async_remove_config_entry_device(
|
response = await ws_client.remove_device(device_entry.id, config_entry.entry_id)
|
||||||
hass, config_entry, device_entry
|
assert not response["success"]
|
||||||
|
assert device_registry.async_get_device(
|
||||||
|
connections={(dr.CONNECTION_NETWORK_MAC, client_1["mac"])}
|
||||||
)
|
)
|
||||||
# Try to remove an active device: not allowed
|
|
||||||
|
# Try to remove an active device from UI: not allowed
|
||||||
device_entry = device_registry.async_get_device(
|
device_entry = device_registry.async_get_device(
|
||||||
connections={(dr.CONNECTION_NETWORK_MAC, device_1["mac"])}
|
connections={(dr.CONNECTION_NETWORK_MAC, device_1["mac"])}
|
||||||
)
|
)
|
||||||
assert not await component.async_remove_config_entry_device(
|
response = await ws_client.remove_device(device_entry.id, config_entry.entry_id)
|
||||||
hass, config_entry, device_entry
|
assert not response["success"]
|
||||||
|
assert device_registry.async_get_device(
|
||||||
|
connections={(dr.CONNECTION_NETWORK_MAC, device_1["mac"])}
|
||||||
)
|
)
|
||||||
# Try to remove an inactive client: allowed
|
|
||||||
|
# Remove a client from Unifi API
|
||||||
|
mock_unifi_websocket(message=MessageKey.CLIENT_REMOVED, data=[client_2])
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
# Try to remove an inactive client from UI: allowed
|
||||||
device_entry = device_registry.async_get_device(
|
device_entry = device_registry.async_get_device(
|
||||||
connections={(dr.CONNECTION_NETWORK_MAC, client_2["mac"])}
|
connections={(dr.CONNECTION_NETWORK_MAC, client_2["mac"])}
|
||||||
)
|
)
|
||||||
assert await component.async_remove_config_entry_device(
|
response = await ws_client.remove_device(device_entry.id, config_entry.entry_id)
|
||||||
hass, config_entry, device_entry
|
assert response["success"]
|
||||||
|
assert not device_registry.async_get_device(
|
||||||
|
connections={(dr.CONNECTION_NETWORK_MAC, client_2["mac"])}
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user