"mode": "box", CONF_UNIT_OF_MEASUREMENT: "decimals", } }), vol.Required(CONF_UNIT_PREFIX, default="none"): selector.selector({"select": { "options": UNIT_PREFIXES }}), vol.Required(CONF_UNIT_TIME, default=TIME_HOURS): selector.selector({"select": { "options": TIME_UNITS }}), }) CONFIG_FLOW: dict[str, HelperFlowFormStep | HelperFlowMenuStep] = { "user": HelperFlowFormStep(CONFIG_SCHEMA) } OPTIONS_FLOW: dict[str, HelperFlowFormStep | HelperFlowMenuStep] = { "init": HelperFlowFormStep(OPTIONS_SCHEMA) } class ConfigFlowHandler(HelperConfigFlowHandler, domain=DOMAIN): """Handle a config or options flow for Integration.""" config_flow = CONFIG_FLOW options_flow = OPTIONS_FLOW def async_config_entry_title(self, options: Mapping[str, Any]) -> str: """Return config entry title."""
selector.selector({"number": { "mode": "box" }}), }) CONFIG_SCHEMA = vol.Schema({ vol.Required(CONF_NAME): selector.selector({"text": {}}), vol.Required(CONF_ENTITY_ID): selector.selector({"entity": { "domain": "sensor" }}), }).extend(OPTIONS_SCHEMA.schema) CONFIG_FLOW: dict[str, HelperFlowFormStep | HelperFlowMenuStep] = { "user": HelperFlowFormStep(CONFIG_SCHEMA, validate_user_input=_validate_mode) } OPTIONS_FLOW: dict[str, HelperFlowFormStep | HelperFlowMenuStep] = { "init": HelperFlowFormStep(OPTIONS_SCHEMA, validate_user_input=_validate_mode) } class ConfigFlowHandler(HelperConfigFlowHandler, domain=DOMAIN): """Handle a config or options flow for Threshold.""" config_flow = CONFIG_FLOW options_flow = OPTIONS_FLOW def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
def set_group_type(group_type: str) -> Callable[[dict[str, Any]], dict[str, Any]]: """Set group type.""" @callback def _set_group_type(user_input: dict[str, Any]) -> dict[str, Any]: """Add group type to user input.""" return {"group_type": group_type, **user_input} return _set_group_type CONFIG_FLOW: dict[str, HelperFlowFormStep | HelperFlowMenuStep] = { "user": HelperFlowMenuStep(GROUP_TYPES), "binary_sensor": HelperFlowFormStep( BINARY_SENSOR_CONFIG_SCHEMA, set_group_type("binary_sensor") ), "cover": HelperFlowFormStep( basic_group_config_schema("cover"), set_group_type("cover") ), "fan": HelperFlowFormStep(basic_group_config_schema("fan"), set_group_type("fan")), "light": HelperFlowFormStep( basic_group_config_schema("light"), set_group_type("light") ), "media_player": HelperFlowFormStep( basic_group_config_schema("media_player"), set_group_type("media_player") ), } OPTIONS_FLOW: dict[str, HelperFlowFormStep | HelperFlowMenuStep] = {
HelperFlowFormStep( vol.Schema({ vol.Required(CONF_ENTITY_ID): selector.selector({"entity": { "domain": Platform.SWITCH }}), vol.Required(CONF_TARGET_DOMAIN): selector.selector({ "select": { "options": [ { "value": Platform.COVER, "label": "Cover" }, { "value": Platform.FAN, "label": "Fan" }, { "value": Platform.LIGHT, "label": "Light" }, { "value": Platform.LOCK, "label": "Lock" }, { "value": Platform.SIREN, "label": "Siren" }, ] } }), }))
async def test_config_flow_advanced_option( hass: HomeAssistant, manager: data_entry_flow.FlowManager, marker ): """Test handling of advanced options in config flow.""" manager.hass = hass CONFIG_SCHEMA = vol.Schema( { marker("option1"): str, marker("advanced_no_default", description={"advanced": True}): str, marker( "advanced_default", default="a very reasonable default", description={"advanced": True}, ): str, } ) CONFIG_FLOW: dict[str, HelperFlowFormStep | HelperFlowMenuStep] = { "init": HelperFlowFormStep(CONFIG_SCHEMA) } @manager.mock_reg_handler("test") class TestFlow(HelperConfigFlowHandler): config_flow = CONFIG_FLOW # Start flow in basic mode result = await manager.async_init("test") assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert list(result["data_schema"].schema.keys()) == ["option1"] result = await manager.async_configure(result["flow_id"], {"option1": "blabla"}) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["data"] == {} assert result["options"] == { "advanced_default": "a very reasonable default", "option1": "blabla", } for option in result["options"]: # Make sure we didn't get the Optional or Required instance as key assert isinstance(option, str) # Start flow in advanced mode result = await manager.async_init("test", context={"show_advanced_options": True}) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert list(result["data_schema"].schema.keys()) == [ "option1", "advanced_no_default", "advanced_default", ] result = await manager.async_configure( result["flow_id"], {"advanced_no_default": "abc123", "option1": "blabla"} ) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["data"] == {} assert result["options"] == { "advanced_default": "a very reasonable default", "advanced_no_default": "abc123", "option1": "blabla", } for option in result["options"]: # Make sure we didn't get the Optional or Required instance as key assert isinstance(option, str) # Start flow in advanced mode result = await manager.async_init("test", context={"show_advanced_options": True}) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert list(result["data_schema"].schema.keys()) == [ "option1", "advanced_no_default", "advanced_default", ] result = await manager.async_configure( result["flow_id"], { "advanced_default": "not default", "advanced_no_default": "abc123", "option1": "blabla", }, ) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["data"] == {} assert result["options"] == { "advanced_default": "not default", "advanced_no_default": "abc123", "option1": "blabla", } for option in result["options"]: # Make sure we didn't get the Optional or Required instance as key assert isinstance(option, str)
async def test_options_flow_advanced_option( hass: HomeAssistant, manager: data_entry_flow.FlowManager, marker ): """Test handling of advanced options in options flow.""" manager.hass = hass OPTIONS_SCHEMA = vol.Schema( { marker("option1"): str, marker("advanced_no_default", description={"advanced": True}): str, marker( "advanced_default", default="a very reasonable default", description={"advanced": True}, ): str, } ) OPTIONS_FLOW: dict[str, HelperFlowFormStep | HelperFlowMenuStep] = { "init": HelperFlowFormStep(OPTIONS_SCHEMA) } class TestFlow(HelperConfigFlowHandler, domain="test"): config_flow = {} options_flow = OPTIONS_FLOW config_entry = MockConfigEntry( data={}, domain="test", options={ "option1": "blabla", "advanced_no_default": "abc123", "advanced_default": "not default", }, ) config_entry.add_to_hass(hass) # Start flow in basic mode result = await hass.config_entries.options.async_init(config_entry.entry_id) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert list(result["data_schema"].schema.keys()) == ["option1"] result = await hass.config_entries.options.async_configure( result["flow_id"], {"option1": "blublu"} ) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["data"] == { "advanced_default": "not default", "advanced_no_default": "abc123", "option1": "blublu", } for option in result["data"]: # Make sure we didn't get the Optional or Required instance as key assert isinstance(option, str) # Start flow in advanced mode result = await hass.config_entries.options.async_init( config_entry.entry_id, context={"show_advanced_options": True} ) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert list(result["data_schema"].schema.keys()) == [ "option1", "advanced_no_default", "advanced_default", ] result = await hass.config_entries.options.async_configure( result["flow_id"], {"advanced_no_default": "def456", "option1": "blabla"} ) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["data"] == { "advanced_default": "a very reasonable default", "advanced_no_default": "def456", "option1": "blabla", } for option in result["data"]: # Make sure we didn't get the Optional or Required instance as key assert isinstance(option, str) # Start flow in advanced mode result = await hass.config_entries.options.async_init( config_entry.entry_id, context={"show_advanced_options": True} ) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert list(result["data_schema"].schema.keys()) == [ "option1", "advanced_no_default", "advanced_default", ] result = await hass.config_entries.options.async_configure( result["flow_id"], { "advanced_default": "also not default", "advanced_no_default": "abc123", "option1": "blabla", }, ) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["data"] == { "advanced_default": "also not default", "advanced_no_default": "abc123", "option1": "blabla", } for option in result["data"]: # Make sure we didn't get the Optional or Required instance as key assert isinstance(option, str)