Clean up Fritz options flow (#109111)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
Simone Chemelli 2024-01-30 13:19:40 +01:00 committed by GitHub
parent a8915b85a4
commit f7909ee34a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 28 deletions

View File

@ -17,7 +17,12 @@ from homeassistant.components.device_tracker import (
CONF_CONSIDER_HOME, CONF_CONSIDER_HOME,
DEFAULT_CONSIDER_HOME, DEFAULT_CONSIDER_HOME,
) )
from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
OptionsFlow,
OptionsFlowWithConfigEntry,
)
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
@ -289,12 +294,8 @@ class FritzBoxToolsFlowHandler(ConfigFlow, domain=DOMAIN):
return self.async_abort(reason="reauth_successful") return self.async_abort(reason="reauth_successful")
class FritzBoxToolsOptionsFlowHandler(OptionsFlow): class FritzBoxToolsOptionsFlowHandler(OptionsFlowWithConfigEntry):
"""Handle a option flow.""" """Handle an options flow."""
def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_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
@ -308,13 +309,13 @@ class FritzBoxToolsOptionsFlowHandler(OptionsFlow):
{ {
vol.Optional( vol.Optional(
CONF_CONSIDER_HOME, CONF_CONSIDER_HOME,
default=self.config_entry.options.get( default=self.options.get(
CONF_CONSIDER_HOME, DEFAULT_CONSIDER_HOME.total_seconds() CONF_CONSIDER_HOME, DEFAULT_CONSIDER_HOME.total_seconds()
), ),
): vol.All(vol.Coerce(int), vol.Clamp(min=0, max=900)), ): vol.All(vol.Coerce(int), vol.Clamp(min=0, max=900)),
vol.Optional( vol.Optional(
CONF_OLD_DISCOVERY, CONF_OLD_DISCOVERY,
default=self.config_entry.options.get( default=self.options.get(
CONF_OLD_DISCOVERY, DEFAULT_CONF_OLD_DISCOVERY CONF_OLD_DISCOVERY, DEFAULT_CONF_OLD_DISCOVERY
), ),
): bool, ): bool,

View File

@ -9,11 +9,13 @@ from fritzconnection.core.exceptions import (
) )
import pytest import pytest
from homeassistant import data_entry_flow
from homeassistant.components.device_tracker import ( from homeassistant.components.device_tracker import (
CONF_CONSIDER_HOME, CONF_CONSIDER_HOME,
DEFAULT_CONSIDER_HOME, DEFAULT_CONSIDER_HOME,
) )
from homeassistant.components.fritz.const import ( from homeassistant.components.fritz.const import (
CONF_OLD_DISCOVERY,
DOMAIN, DOMAIN,
ERROR_AUTH_INVALID, ERROR_AUTH_INVALID,
ERROR_CANNOT_CONNECT, ERROR_CANNOT_CONNECT,
@ -453,28 +455,24 @@ async def test_ssdp_exception(hass: HomeAssistant, mock_get_source_ip) -> None:
assert result["step_id"] == "confirm" assert result["step_id"] == "confirm"
async def test_options_flow( async def test_options_flow(hass: HomeAssistant) -> None:
hass: HomeAssistant, fc_class_mock, mock_get_source_ip
) -> None:
"""Test options flow.""" """Test options flow."""
mock_config = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) mock_config = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA)
mock_config.add_to_hass(hass) mock_config.add_to_hass(hass)
with patch( result = await hass.config_entries.options.async_init(mock_config.entry_id)
"homeassistant.components.fritz.config_flow.FritzConnection", await hass.async_block_till_done()
side_effect=fc_class_mock, result = await hass.config_entries.options.async_configure(
), patch("homeassistant.components.fritz.common.FritzBoxTools"): result["flow_id"],
result = await hass.config_entries.options.async_init(mock_config.entry_id) user_input={
assert result["type"] == FlowResultType.FORM CONF_CONSIDER_HOME: 37,
assert result["step_id"] == "init" },
)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(mock_config.entry_id) assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
result = await hass.config_entries.options.async_configure( assert result["data"] == {
result["flow_id"], CONF_OLD_DISCOVERY: False,
user_input={ CONF_CONSIDER_HOME: 37,
CONF_CONSIDER_HOME: 37, }
},
)
assert result["type"] == FlowResultType.CREATE_ENTRY
assert mock_config.options[CONF_CONSIDER_HOME] == 37