From 95bcceddf76120c6eeaa120b51e67a68daeeaa1d Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Mon, 17 Feb 2020 23:10:44 +0100 Subject: [PATCH] Add an options flow to demo (#31920) To help test the frontend --- homeassistant/components/demo/config_flow.py | 81 ++++++++++++++++++++ homeassistant/components/demo/strings.json | 20 +++++ 2 files changed, 101 insertions(+) diff --git a/homeassistant/components/demo/config_flow.py b/homeassistant/components/demo/config_flow.py index e6b275920c8..1f3975d0241 100644 --- a/homeassistant/components/demo/config_flow.py +++ b/homeassistant/components/demo/config_flow.py @@ -1,16 +1,97 @@ """Config flow to configure demo component.""" +import voluptuous as vol from homeassistant import config_entries +from homeassistant.core import callback +import homeassistant.helpers.config_validation as cv # pylint: disable=unused-import from . import DOMAIN +CONF_STRING = "string" +CONF_BOOLEAN = "bool" +CONF_INT = "int" +CONF_SELECT = "select" +CONF_MULTISELECT = "multi" + class DemoConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Demo configuration flow.""" VERSION = 1 + @staticmethod + @callback + def async_get_options_flow(config_entry): + """Get the options flow for this handler.""" + return OptionsFlowHandler(config_entry) + async def async_step_import(self, import_info): """Set the config entry up from yaml.""" return self.async_create_entry(title="Demo", data={}) + + +class OptionsFlowHandler(config_entries.OptionsFlow): + """Handle options.""" + + def __init__(self, config_entry): + """Initialize options flow.""" + self.config_entry = config_entry + self.options = dict(config_entry.options) + + async def async_step_init(self, user_input=None): + """Manage the options.""" + return await self.async_step_options_1() + + async def async_step_options_1(self, user_input=None): + """Manage the options.""" + if user_input is not None: + self.options.update(user_input) + return await self.async_step_options_2() + + return self.async_show_form( + step_id="options_1", + data_schema=vol.Schema( + { + vol.Optional( + CONF_BOOLEAN, + default=self.config_entry.options.get(CONF_BOOLEAN, False), + ): bool, + vol.Optional( + CONF_INT, default=self.config_entry.options.get(CONF_INT, 10), + ): int, + } + ), + ) + + async def async_step_options_2(self, user_input=None): + """Manage the options 2.""" + if user_input is not None: + self.options.update(user_input) + return await self._update_options() + + return self.async_show_form( + step_id="options_2", + data_schema=vol.Schema( + { + vol.Optional( + CONF_STRING, + default=self.config_entry.options.get(CONF_STRING, "Default",), + ): str, + vol.Optional( + CONF_SELECT, + default=self.config_entry.options.get(CONF_SELECT, "default"), + ): vol.In(["default", "other"]), + vol.Optional( + CONF_MULTISELECT, + default=self.config_entry.options.get( + CONF_MULTISELECT, ["default"] + ), + ): cv.multi_select({"default": "Default", "other": "Other"}), + } + ), + ) + + async def _update_options(self): + """Update config entry options.""" + return self.async_create_entry(title="", data=self.options) diff --git a/homeassistant/components/demo/strings.json b/homeassistant/components/demo/strings.json index a2c0103280b..33f3b4229dc 100644 --- a/homeassistant/components/demo/strings.json +++ b/homeassistant/components/demo/strings.json @@ -1,5 +1,25 @@ { "config": { "title": "Demo" + }, + "options": { + "step": { + "init": { + "data": {} + }, + "options_1": { + "data": { + "bool": "Optional boolean", + "int": "Numeric input" + } + }, + "options_2": { + "data": { + "string": "String value", + "select": "Select an option", + "multi": "Multiselect" + } + } + } } }