UniFi - Simplify config option of block clients to just a multi select drop down (#34514)

This commit is contained in:
Robert Svensson 2020-04-22 05:45:48 +02:00 committed by GitHub
parent a5b98b40ac
commit 193e9bec97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 69 deletions

View File

@ -34,7 +34,6 @@ from .const import (
from .controller import get_controller from .controller import get_controller
from .errors import AlreadyConfigured, AuthenticationRequired, CannotConnect from .errors import AlreadyConfigured, AuthenticationRequired, CannotConnect
CONF_NEW_CLIENT = "new_client"
DEFAULT_PORT = 8443 DEFAULT_PORT = 8443
DEFAULT_SITE_ID = "default" DEFAULT_SITE_ID = "default"
DEFAULT_VERIFY_SSL = False DEFAULT_VERIFY_SSL = False
@ -230,53 +229,27 @@ class UnifiOptionsFlowHandler(config_entries.OptionsFlow):
errors = {} errors = {}
if user_input is not None: if user_input is not None:
new_client = user_input.pop(CONF_NEW_CLIENT, None)
self.options.update(user_input) self.options.update(user_input)
if new_client:
if (
new_client in self.controller.api.clients
or new_client in self.controller.api.clients_all
):
self.options[CONF_BLOCK_CLIENT].append(new_client)
else:
errors["base"] = "unknown_client_mac"
else:
return await self.async_step_statistics_sensors() return await self.async_step_statistics_sensors()
clients_to_block = {} clients_to_block = {}
for mac in self.options[CONF_BLOCK_CLIENT]: for client in self.controller.api.clients.values():
clients_to_block[
name = None client.mac
] = f"{client.name or client.hostname} ({client.mac})"
for clients in [
self.controller.api.clients,
self.controller.api.clients_all,
]:
if mac in clients:
name = f"{clients[mac].name or clients[mac].hostname} ({mac})"
break
if not name:
name = mac
clients_to_block[mac] = name
return self.async_show_form( return self.async_show_form(
step_id="client_control", step_id="client_control",
data_schema=vol.Schema( data_schema=vol.Schema(
{ {
vol.Optional(
CONF_BLOCK_CLIENT, default=self.options[CONF_BLOCK_CLIENT]
): cv.multi_select(clients_to_block),
vol.Optional( vol.Optional(
CONF_POE_CLIENTS, CONF_POE_CLIENTS,
default=self.options.get(CONF_POE_CLIENTS, DEFAULT_POE_CLIENTS), default=self.options.get(CONF_POE_CLIENTS, DEFAULT_POE_CLIENTS),
): bool, ): bool,
vol.Optional(
CONF_BLOCK_CLIENT, default=self.options[CONF_BLOCK_CLIENT]
): cv.multi_select(clients_to_block),
vol.Optional(CONF_NEW_CLIENT): str,
} }
), ),
errors=errors, errors=errors,

View File

@ -41,7 +41,6 @@
"client_control": { "client_control": {
"data": { "data": {
"block_client": "Network access controlled clients", "block_client": "Network access controlled clients",
"new_client": "Add new client for network access control",
"poe_clients": "Allow POE control of clients" "poe_clients": "Allow POE control of clients"
}, },
"description": "Configure client controls\n\nCreate switches for serial numbers you want to control network access for.", "description": "Configure client controls\n\nCreate switches for serial numbers you want to control network access for.",

View File

@ -5,7 +5,6 @@ from asynctest import patch
from homeassistant import data_entry_flow from homeassistant import data_entry_flow
from homeassistant.components import unifi from homeassistant.components import unifi
from homeassistant.components.unifi import config_flow from homeassistant.components.unifi import config_flow
from homeassistant.components.unifi.config_flow import CONF_NEW_CLIENT
from homeassistant.components.unifi.const import ( from homeassistant.components.unifi.const import (
CONF_ALLOW_BANDWIDTH_SENSORS, CONF_ALLOW_BANDWIDTH_SENSORS,
CONF_BLOCK_CLIENT, CONF_BLOCK_CLIENT,
@ -293,38 +292,9 @@ async def test_option_flow(hass):
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "client_control" assert result["step_id"] == "client_control"
clients_to_block = hass.config_entries.options._progress[result["flow_id"]].options[
CONF_BLOCK_CLIENT
]
result = await hass.config_entries.options.async_configure( result = await hass.config_entries.options.async_configure(
result["flow_id"], result["flow_id"],
user_input={ user_input={CONF_BLOCK_CLIENT: [CLIENTS[0]["mac"]], CONF_POE_CLIENTS: False},
CONF_BLOCK_CLIENT: clients_to_block,
CONF_NEW_CLIENT: "00:00:00:00:00:01",
CONF_POE_CLIENTS: False,
},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "client_control"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_BLOCK_CLIENT: clients_to_block,
CONF_NEW_CLIENT: "00:00:00:00:00:02",
},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "client_control"
assert result["errors"] == {"base": "unknown_client_mac"}
clients_to_block = hass.config_entries.options._progress[result["flow_id"]].options[
CONF_BLOCK_CLIENT
]
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={CONF_BLOCK_CLIENT: clients_to_block},
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
@ -343,6 +313,6 @@ async def test_option_flow(hass):
CONF_DETECTION_TIME: 100, CONF_DETECTION_TIME: 100,
CONF_IGNORE_WIRED_BUG: False, CONF_IGNORE_WIRED_BUG: False,
CONF_POE_CLIENTS: False, CONF_POE_CLIENTS: False,
CONF_BLOCK_CLIENT: ["00:00:00:00:00:01"], CONF_BLOCK_CLIENT: [CLIENTS[0]["mac"]],
CONF_ALLOW_BANDWIDTH_SENSORS: True, CONF_ALLOW_BANDWIDTH_SENSORS: True,
} }