mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
UniFi - Store controller in config_entry.entry_id (#34553)
* Store controller in config_entry.entry_id * Clean up imports
This commit is contained in:
@@ -5,13 +5,15 @@ from aiounifi.controller import MESSAGE_CLIENT_REMOVED
|
||||
from aiounifi.websocket import SIGNAL_DATA
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import unifi
|
||||
import homeassistant.components.switch as switch
|
||||
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,
|
||||
CONF_TRACK_CLIENTS,
|
||||
CONF_TRACK_DEVICES,
|
||||
DOMAIN as UNIFI_DOMAIN,
|
||||
)
|
||||
from homeassistant.components.unifi.switch import POE_SWITCH
|
||||
from homeassistant.helpers import entity_registry
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
@@ -200,11 +202,11 @@ async def test_platform_manually_configured(hass):
|
||||
"""Test that we do not discover anything or try to set up a controller."""
|
||||
assert (
|
||||
await async_setup_component(
|
||||
hass, switch.DOMAIN, {switch.DOMAIN: {"platform": "unifi"}}
|
||||
hass, SWITCH_DOMAIN, {SWITCH_DOMAIN: {"platform": UNIFI_DOMAIN}}
|
||||
)
|
||||
is True
|
||||
)
|
||||
assert unifi.DOMAIN not in hass.data
|
||||
assert UNIFI_DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_clients(hass):
|
||||
@@ -214,7 +216,7 @@ async def test_no_clients(hass):
|
||||
)
|
||||
|
||||
assert len(controller.mock_requests) == 4
|
||||
assert len(hass.states.async_entity_ids("switch")) == 0
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_controller_not_client(hass):
|
||||
@@ -227,7 +229,7 @@ async def test_controller_not_client(hass):
|
||||
)
|
||||
|
||||
assert len(controller.mock_requests) == 4
|
||||
assert len(hass.states.async_entity_ids("switch")) == 0
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
cloudkey = hass.states.get("switch.cloud_key")
|
||||
assert cloudkey is None
|
||||
|
||||
@@ -245,7 +247,7 @@ async def test_not_admin(hass):
|
||||
)
|
||||
|
||||
assert len(controller.mock_requests) == 4
|
||||
assert len(hass.states.async_entity_ids("switch")) == 0
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_switches(hass):
|
||||
@@ -263,13 +265,13 @@ async def test_switches(hass):
|
||||
)
|
||||
|
||||
assert len(controller.mock_requests) == 4
|
||||
assert len(hass.states.async_entity_ids("switch")) == 3
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 3
|
||||
|
||||
switch_1 = hass.states.get("switch.poe_client_1")
|
||||
assert switch_1 is not None
|
||||
assert switch_1.state == "on"
|
||||
assert switch_1.attributes["power"] == "2.56"
|
||||
assert switch_1.attributes["switch"] == "00:00:00:00:01:01"
|
||||
assert switch_1.attributes[SWITCH_DOMAIN] == "00:00:00:00:01:01"
|
||||
assert switch_1.attributes["port"] == 1
|
||||
assert switch_1.attributes["poe_mode"] == "auto"
|
||||
|
||||
@@ -285,7 +287,7 @@ async def test_switches(hass):
|
||||
assert unblocked.state == "on"
|
||||
|
||||
await hass.services.async_call(
|
||||
"switch", "turn_off", {"entity_id": "switch.block_client_1"}, blocking=True
|
||||
SWITCH_DOMAIN, "turn_off", {"entity_id": "switch.block_client_1"}, blocking=True
|
||||
)
|
||||
assert len(controller.mock_requests) == 5
|
||||
assert controller.mock_requests[4] == {
|
||||
@@ -295,7 +297,7 @@ async def test_switches(hass):
|
||||
}
|
||||
|
||||
await hass.services.async_call(
|
||||
"switch", "turn_on", {"entity_id": "switch.block_client_1"}, blocking=True
|
||||
SWITCH_DOMAIN, "turn_on", {"entity_id": "switch.block_client_1"}, blocking=True
|
||||
)
|
||||
assert len(controller.mock_requests) == 6
|
||||
assert controller.mock_requests[5] == {
|
||||
@@ -313,7 +315,7 @@ async def test_remove_switches(hass):
|
||||
clients_response=[CLIENT_1, UNBLOCKED],
|
||||
devices_response=[DEVICE_1],
|
||||
)
|
||||
assert len(hass.states.async_entity_ids("switch")) == 2
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
||||
|
||||
poe_switch = hass.states.get("switch.poe_client_1")
|
||||
assert poe_switch is not None
|
||||
@@ -328,7 +330,7 @@ async def test_remove_switches(hass):
|
||||
controller.api.session_handler(SIGNAL_DATA)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_entity_ids("switch")) == 0
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
poe_switch = hass.states.get("switch.poe_client_1")
|
||||
assert poe_switch is None
|
||||
@@ -349,7 +351,7 @@ async def test_new_client_discovered_on_block_control(hass):
|
||||
)
|
||||
|
||||
assert len(controller.mock_requests) == 4
|
||||
assert len(hass.states.async_entity_ids("switch")) == 0
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
blocked = hass.states.get("switch.block_client_1")
|
||||
assert blocked is None
|
||||
@@ -361,7 +363,7 @@ async def test_new_client_discovered_on_block_control(hass):
|
||||
controller.api.session_handler("data")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_entity_ids("switch")) == 1
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
blocked = hass.states.get("switch.block_client_1")
|
||||
assert blocked is not None
|
||||
|
||||
@@ -373,7 +375,7 @@ async def test_option_block_clients(hass):
|
||||
options={CONF_BLOCK_CLIENT: [BLOCKED["mac"]]},
|
||||
clients_all_response=[BLOCKED, UNBLOCKED],
|
||||
)
|
||||
assert len(hass.states.async_entity_ids("switch")) == 1
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
|
||||
# Add a second switch
|
||||
hass.config_entries.async_update_entry(
|
||||
@@ -381,28 +383,28 @@ async def test_option_block_clients(hass):
|
||||
options={CONF_BLOCK_CLIENT: [BLOCKED["mac"], UNBLOCKED["mac"]]},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids("switch")) == 2
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
||||
|
||||
# Remove the second switch again
|
||||
hass.config_entries.async_update_entry(
|
||||
controller.config_entry, options={CONF_BLOCK_CLIENT: [BLOCKED["mac"]]},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids("switch")) == 1
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
|
||||
# Enable one and remove another one
|
||||
hass.config_entries.async_update_entry(
|
||||
controller.config_entry, options={CONF_BLOCK_CLIENT: [UNBLOCKED["mac"]]},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids("switch")) == 1
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
|
||||
# Remove one
|
||||
hass.config_entries.async_update_entry(
|
||||
controller.config_entry, options={CONF_BLOCK_CLIENT: []},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids("switch")) == 0
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_new_client_discovered_on_poe_control(hass):
|
||||
@@ -415,7 +417,7 @@ async def test_new_client_discovered_on_poe_control(hass):
|
||||
)
|
||||
|
||||
assert len(controller.mock_requests) == 4
|
||||
assert len(hass.states.async_entity_ids("switch")) == 1
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
|
||||
controller.api.websocket._data = {
|
||||
"meta": {"message": "sta:sync"},
|
||||
@@ -425,10 +427,10 @@ async def test_new_client_discovered_on_poe_control(hass):
|
||||
|
||||
# Calling a service will trigger the updates to run
|
||||
await hass.services.async_call(
|
||||
"switch", "turn_off", {"entity_id": "switch.poe_client_1"}, blocking=True
|
||||
SWITCH_DOMAIN, "turn_off", {"entity_id": "switch.poe_client_1"}, blocking=True
|
||||
)
|
||||
assert len(controller.mock_requests) == 5
|
||||
assert len(hass.states.async_entity_ids("switch")) == 2
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
||||
assert controller.mock_requests[4] == {
|
||||
"json": {
|
||||
"port_overrides": [{"port_idx": 1, "portconf_id": "1a1", "poe_mode": "off"}]
|
||||
@@ -438,7 +440,7 @@ async def test_new_client_discovered_on_poe_control(hass):
|
||||
}
|
||||
|
||||
await hass.services.async_call(
|
||||
"switch", "turn_on", {"entity_id": "switch.poe_client_1"}, blocking=True
|
||||
SWITCH_DOMAIN, "turn_on", {"entity_id": "switch.poe_client_1"}, blocking=True
|
||||
)
|
||||
assert len(controller.mock_requests) == 6
|
||||
assert controller.mock_requests[4] == {
|
||||
@@ -467,7 +469,7 @@ async def test_ignore_multiple_poe_clients_on_same_port(hass):
|
||||
)
|
||||
|
||||
assert len(controller.mock_requests) == 4
|
||||
assert len(hass.states.async_entity_ids("device_tracker")) == 3
|
||||
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")
|
||||
@@ -479,7 +481,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",
|
||||
@@ -491,16 +493,16 @@ async def test_restoring_client(hass):
|
||||
|
||||
registry = await entity_registry.async_get_registry(hass)
|
||||
registry.async_get_or_create(
|
||||
switch.DOMAIN,
|
||||
unifi.DOMAIN,
|
||||
"poe-{}".format(CLIENT_1["mac"]),
|
||||
SWITCH_DOMAIN,
|
||||
UNIFI_DOMAIN,
|
||||
f'{POE_SWITCH}-{CLIENT_1["mac"]}',
|
||||
suggested_object_id=CLIENT_1["hostname"],
|
||||
config_entry=config_entry,
|
||||
)
|
||||
registry.async_get_or_create(
|
||||
switch.DOMAIN,
|
||||
unifi.DOMAIN,
|
||||
"poe-{}".format(CLIENT_2["mac"]),
|
||||
SWITCH_DOMAIN,
|
||||
UNIFI_DOMAIN,
|
||||
f'{POE_SWITCH}-{CLIENT_2["mac"]}',
|
||||
suggested_object_id=CLIENT_2["hostname"],
|
||||
config_entry=config_entry,
|
||||
)
|
||||
@@ -518,7 +520,7 @@ async def test_restoring_client(hass):
|
||||
)
|
||||
|
||||
assert len(controller.mock_requests) == 4
|
||||
assert len(hass.states.async_entity_ids("switch")) == 2
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
||||
|
||||
device_1 = hass.states.get("switch.client_1")
|
||||
assert device_1 is not None
|
||||
|
||||
Reference in New Issue
Block a user