From 5e70faca5f738150b7aec45acc43eafe841fe701 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Mon, 25 Mar 2024 17:38:02 +0100 Subject: [PATCH] Refactor options flow in dnsip (#114058) * Refactor options flow in dnsip * Mods --- homeassistant/components/dnsip/config_flow.py | 52 +++++++++--------- tests/components/dnsip/test_config_flow.py | 55 +++++++++++++++++++ 2 files changed, 81 insertions(+), 26 deletions(-) diff --git a/homeassistant/components/dnsip/config_flow.py b/homeassistant/components/dnsip/config_flow.py index 8dc62a8343d..f07971d5db5 100644 --- a/homeassistant/components/dnsip/config_flow.py +++ b/homeassistant/components/dnsip/config_flow.py @@ -14,7 +14,7 @@ from homeassistant.config_entries import ( ConfigEntry, ConfigFlow, ConfigFlowResult, - OptionsFlow, + OptionsFlowWithConfigEntry, ) from homeassistant.const import CONF_NAME from homeassistant.core import callback @@ -146,47 +146,47 @@ class DnsIPConfigFlow(ConfigFlow, domain=DOMAIN): ) -class DnsIPOptionsFlowHandler(OptionsFlow): +class DnsIPOptionsFlowHandler(OptionsFlowWithConfigEntry): """Handle a option config flow for dnsip integration.""" - def __init__(self, entry: ConfigEntry) -> None: - """Initialize options flow.""" - self.entry = entry - async def async_step_init( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Manage the options.""" errors = {} if user_input is not None: + resolver = user_input.get(CONF_RESOLVER, DEFAULT_RESOLVER) + resolver_ipv6 = user_input.get(CONF_RESOLVER_IPV6, DEFAULT_RESOLVER_IPV6) validate = await async_validate_hostname( - self.entry.data[CONF_HOSTNAME], - user_input[CONF_RESOLVER], - user_input[CONF_RESOLVER_IPV6], + self.config_entry.data[CONF_HOSTNAME], + resolver, + resolver_ipv6, ) - if validate[CONF_IPV4] is False and self.entry.data[CONF_IPV4] is True: + if ( + validate[CONF_IPV4] is False + and self.config_entry.data[CONF_IPV4] is True + ): errors[CONF_RESOLVER] = "invalid_resolver" - elif validate[CONF_IPV6] is False and self.entry.data[CONF_IPV6] is True: + elif ( + validate[CONF_IPV6] is False + and self.config_entry.data[CONF_IPV6] is True + ): errors[CONF_RESOLVER_IPV6] = "invalid_resolver" else: - return self.async_create_entry(title=self.entry.title, data=user_input) + return self.async_create_entry( + title=self.config_entry.title, + data={CONF_RESOLVER: resolver, CONF_RESOLVER_IPV6: resolver_ipv6}, + ) - return self.async_show_form( - step_id="init", - data_schema=vol.Schema( + schema = self.add_suggested_values_to_schema( + vol.Schema( { - vol.Optional( - CONF_RESOLVER, - default=self.entry.options.get(CONF_RESOLVER, DEFAULT_RESOLVER), - ): cv.string, - vol.Optional( - CONF_RESOLVER_IPV6, - default=self.entry.options.get( - CONF_RESOLVER_IPV6, DEFAULT_RESOLVER_IPV6 - ), - ): cv.string, + vol.Optional(CONF_RESOLVER): cv.string, + vol.Optional(CONF_RESOLVER_IPV6): cv.string, } ), - errors=errors, + self.config_entry.options, ) + + return self.async_show_form(step_id="init", data_schema=schema, errors=errors) diff --git a/tests/components/dnsip/test_config_flow.py b/tests/components/dnsip/test_config_flow.py index 7575efe220d..eb254908dd3 100644 --- a/tests/components/dnsip/test_config_flow.py +++ b/tests/components/dnsip/test_config_flow.py @@ -221,6 +221,61 @@ async def test_options_flow(hass: HomeAssistant) -> None: assert entry.state == config_entries.ConfigEntryState.LOADED +async def test_options_flow_empty_return(hass: HomeAssistant) -> None: + """Test options config flow with empty return from user.""" + entry = MockConfigEntry( + domain=DOMAIN, + unique_id="12345", + data={ + CONF_HOSTNAME: "home-assistant.io", + CONF_NAME: "home-assistant.io", + CONF_IPV4: True, + CONF_IPV6: False, + }, + options={ + CONF_RESOLVER: "8.8.8.8", + CONF_RESOLVER_IPV6: "2620:119:53::1", + }, + ) + entry.add_to_hass(hass) + + with patch( + "homeassistant.components.dnsip.config_flow.aiodns.DNSResolver", + return_value=RetrieveDNS(), + ): + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + result = await hass.config_entries.options.async_init(entry.entry_id) + + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "init" + + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={}, + ) + await hass.async_block_till_done() + + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["data"] == { + "resolver": "208.67.222.222", + "resolver_ipv6": "2620:119:53::53", + } + + entry = hass.config_entries.async_get_entry(entry.entry_id) + assert entry.data == { + "hostname": "home-assistant.io", + "ipv4": True, + "ipv6": False, + "name": "home-assistant.io", + } + assert entry.options == { + "resolver": "208.67.222.222", + "resolver_ipv6": "2620:119:53::53", + } + + @pytest.mark.parametrize( "p_input", [