mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Refactor options flow in dnsip (#114058)
* Refactor options flow in dnsip * Mods
This commit is contained in:
parent
71a6653f60
commit
5e70faca5f
@ -14,7 +14,7 @@ from homeassistant.config_entries import (
|
|||||||
ConfigEntry,
|
ConfigEntry,
|
||||||
ConfigFlow,
|
ConfigFlow,
|
||||||
ConfigFlowResult,
|
ConfigFlowResult,
|
||||||
OptionsFlow,
|
OptionsFlowWithConfigEntry,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_NAME
|
||||||
from homeassistant.core import callback
|
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."""
|
"""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(
|
async def async_step_init(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Manage the options."""
|
"""Manage the options."""
|
||||||
errors = {}
|
errors = {}
|
||||||
if user_input is not None:
|
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(
|
validate = await async_validate_hostname(
|
||||||
self.entry.data[CONF_HOSTNAME],
|
self.config_entry.data[CONF_HOSTNAME],
|
||||||
user_input[CONF_RESOLVER],
|
resolver,
|
||||||
user_input[CONF_RESOLVER_IPV6],
|
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"
|
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"
|
errors[CONF_RESOLVER_IPV6] = "invalid_resolver"
|
||||||
else:
|
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(
|
schema = self.add_suggested_values_to_schema(
|
||||||
step_id="init",
|
vol.Schema(
|
||||||
data_schema=vol.Schema(
|
|
||||||
{
|
{
|
||||||
vol.Optional(
|
vol.Optional(CONF_RESOLVER): cv.string,
|
||||||
CONF_RESOLVER,
|
vol.Optional(CONF_RESOLVER_IPV6): cv.string,
|
||||||
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,
|
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
errors=errors,
|
self.config_entry.options,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return self.async_show_form(step_id="init", data_schema=schema, errors=errors)
|
||||||
|
@ -221,6 +221,61 @@ async def test_options_flow(hass: HomeAssistant) -> None:
|
|||||||
assert entry.state == config_entries.ConfigEntryState.LOADED
|
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(
|
@pytest.mark.parametrize(
|
||||||
"p_input",
|
"p_input",
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user