Refactor options flow in dnsip (#114058)

* Refactor options flow in dnsip

* Mods
This commit is contained in:
G Johansson 2024-03-25 17:38:02 +01:00 committed by GitHub
parent 71a6653f60
commit 5e70faca5f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 81 additions and 26 deletions

View file

@ -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)

View file

@ -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",
[