From 72e7beeb76e238fe5cf35f5b7a1d6f17425e5349 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Fri, 1 May 2020 07:33:22 +0200 Subject: [PATCH] UniFi - Add simple options flow (#34990) --- homeassistant/components/unifi/config_flow.py | 38 ++++++++++++++++++- homeassistant/components/unifi/strings.json | 8 ++++ .../components/unifi/translations/en.json | 8 ++++ tests/components/unifi/test_config_flow.py | 36 ++++++++++++++++-- 4 files changed, 86 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/unifi/config_flow.py b/homeassistant/components/unifi/config_flow.py index f42acf54e9d..72ba593bae6 100644 --- a/homeassistant/components/unifi/config_flow.py +++ b/homeassistant/components/unifi/config_flow.py @@ -175,7 +175,43 @@ class UnifiOptionsFlowHandler(config_entries.OptionsFlow): """Manage the UniFi options.""" self.controller = self.hass.data[UNIFI_DOMAIN][self.config_entry.entry_id] self.options[CONF_BLOCK_CLIENT] = self.controller.option_block_clients - return await self.async_step_device_tracker() + + if self.show_advanced_options: + return await self.async_step_device_tracker() + + return await self.async_step_simple_options() + + async def async_step_simple_options(self, user_input=None): + """For simple Jack.""" + if user_input is not None: + self.options.update(user_input) + return await self._update_options() + + clients_to_block = {} + + for client in self.controller.api.clients.values(): + clients_to_block[ + client.mac + ] = f"{client.name or client.hostname} ({client.mac})" + + return self.async_show_form( + step_id="simple_options", + data_schema=vol.Schema( + { + vol.Optional( + CONF_TRACK_CLIENTS, + default=self.controller.option_track_clients, + ): bool, + vol.Optional( + CONF_TRACK_DEVICES, + default=self.controller.option_track_devices, + ): bool, + vol.Optional( + CONF_BLOCK_CLIENT, default=self.options[CONF_BLOCK_CLIENT] + ): cv.multi_select(clients_to_block), + } + ), + ) async def async_step_device_tracker(self, user_input=None): """Manage the device tracker options.""" diff --git a/homeassistant/components/unifi/strings.json b/homeassistant/components/unifi/strings.json index 6c142d371c9..da1d6200ed5 100644 --- a/homeassistant/components/unifi/strings.json +++ b/homeassistant/components/unifi/strings.json @@ -46,6 +46,14 @@ "description": "Configure client controls\n\nCreate switches for serial numbers you want to control network access for.", "title": "UniFi options 2/3" }, + "simple_options": { + "data": { + "track_clients": "[%key:component::unifi::options::step::device_tracker::data::track_clients%]", + "track_devices": "[%key:component::unifi::options::step::device_tracker::data::track_devices%]", + "block_client": "[%key:component::unifi::options::step::client_control::data::block_client%]" + }, + "description": "Configure UniFi integration" + }, "statistics_sensors": { "data": { "allow_bandwidth_sensors": "Bandwidth usage sensors for network clients" diff --git a/homeassistant/components/unifi/translations/en.json b/homeassistant/components/unifi/translations/en.json index 618c393b7aa..fd7096686e1 100644 --- a/homeassistant/components/unifi/translations/en.json +++ b/homeassistant/components/unifi/translations/en.json @@ -46,6 +46,14 @@ "description": "Configure device tracking", "title": "UniFi options 1/3" }, + "simple_options": { + "data": { + "track_clients": "[%key:component::unifi::options::step::device_tracker::data::track_clients%]", + "track_devices": "[%key:component::unifi::options::step::device_tracker::data::track_devices%]", + "block_client": "[%key:component::unifi::options::step::client_control::data::block_client%]" + }, + "description": "Configure UniFi integration" + }, "statistics_sensors": { "data": { "allow_bandwidth_sensors": "Bandwidth usage sensors for network clients" diff --git a/tests/components/unifi/test_config_flow.py b/tests/components/unifi/test_config_flow.py index ae738ba8a64..489ae25a60c 100644 --- a/tests/components/unifi/test_config_flow.py +++ b/tests/components/unifi/test_config_flow.py @@ -264,14 +264,14 @@ async def test_flow_fails_unknown_problem(hass, aioclient_mock): assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT -async def test_option_flow(hass): - """Test config flow options.""" +async def test_advanced_option_flow(hass): + """Test advanced config flow options.""" controller = await setup_unifi_integration( hass, clients_response=CLIENTS, wlans_response=WLANS ) result = await hass.config_entries.options.async_init( - controller.config_entry.entry_id + controller.config_entry.entry_id, context={"show_advanced_options": True} ) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM @@ -315,3 +315,33 @@ async def test_option_flow(hass): CONF_BLOCK_CLIENT: [CLIENTS[0]["mac"]], CONF_ALLOW_BANDWIDTH_SENSORS: True, } + + +async def test_simple_option_flow(hass): + """Test simple config flow options.""" + controller = await setup_unifi_integration( + hass, clients_response=CLIENTS, wlans_response=WLANS + ) + + result = await hass.config_entries.options.async_init( + controller.config_entry.entry_id, context={"show_advanced_options": False} + ) + + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["step_id"] == "simple_options" + + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={ + CONF_TRACK_CLIENTS: False, + CONF_TRACK_DEVICES: False, + CONF_BLOCK_CLIENT: [CLIENTS[0]["mac"]], + }, + ) + + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["data"] == { + CONF_TRACK_CLIENTS: False, + CONF_TRACK_DEVICES: False, + CONF_BLOCK_CLIENT: [CLIENTS[0]["mac"]], + }