mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 15:47:12 +00:00
Add reconfiguration support for keenetic_ndms2 integration (#142191)
Co-authored-by: Franck Nijhof <frenck@frenck.nl> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
e63e6a6072
commit
275d390a6c
@ -8,7 +8,12 @@ from urllib.parse import urlparse
|
|||||||
from ndms2_client import Client, ConnectionException, InterfaceInfo, TelnetConnection
|
from ndms2_client import Client, ConnectionException, InterfaceInfo, TelnetConnection
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow
|
from homeassistant.config_entries import (
|
||||||
|
SOURCE_RECONFIGURE,
|
||||||
|
ConfigFlow,
|
||||||
|
ConfigFlowResult,
|
||||||
|
OptionsFlow,
|
||||||
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_PASSWORD,
|
CONF_PASSWORD,
|
||||||
@ -45,7 +50,7 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
|
||||||
host: str | bytes | None = None
|
_host: str | bytes | None = None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@callback
|
||||||
@ -61,8 +66,9 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
"""Handle a flow initialized by the user."""
|
"""Handle a flow initialized by the user."""
|
||||||
errors = {}
|
errors = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
host = self.host or user_input[CONF_HOST]
|
host = self._host or user_input[CONF_HOST]
|
||||||
self._async_abort_entries_match({CONF_HOST: host})
|
if self.source != SOURCE_RECONFIGURE:
|
||||||
|
self._async_abort_entries_match({CONF_HOST: host})
|
||||||
|
|
||||||
_client = Client(
|
_client = Client(
|
||||||
TelnetConnection(
|
TelnetConnection(
|
||||||
@ -81,12 +87,17 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
except ConnectionException:
|
except ConnectionException:
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
else:
|
else:
|
||||||
|
if self.source == SOURCE_RECONFIGURE:
|
||||||
|
return self.async_update_reload_and_abort(
|
||||||
|
self._get_reconfigure_entry(),
|
||||||
|
data={CONF_HOST: host, **user_input},
|
||||||
|
)
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=router_info.name, data={CONF_HOST: host, **user_input}
|
title=router_info.name, data={CONF_HOST: host, **user_input}
|
||||||
)
|
)
|
||||||
|
|
||||||
host_schema: VolDictType = (
|
host_schema: VolDictType = (
|
||||||
{vol.Required(CONF_HOST): str} if not self.host else {}
|
{vol.Required(CONF_HOST): str} if not self._host else {}
|
||||||
)
|
)
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
@ -102,6 +113,15 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def async_step_reconfigure(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> ConfigFlowResult:
|
||||||
|
"""Handle reconfiguration."""
|
||||||
|
existing_entry_data = dict(self._get_reconfigure_entry().data)
|
||||||
|
self._host = existing_entry_data[CONF_HOST]
|
||||||
|
|
||||||
|
return await self.async_step_user(user_input)
|
||||||
|
|
||||||
async def async_step_ssdp(
|
async def async_step_ssdp(
|
||||||
self, discovery_info: SsdpServiceInfo
|
self, discovery_info: SsdpServiceInfo
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
@ -124,7 +144,7 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
self._async_abort_entries_match({CONF_HOST: host})
|
self._async_abort_entries_match({CONF_HOST: host})
|
||||||
|
|
||||||
self.host = host
|
self._host = host
|
||||||
self.context["title_placeholders"] = {
|
self.context["title_placeholders"] = {
|
||||||
"name": friendly_name,
|
"name": friendly_name,
|
||||||
"host": host,
|
"host": host,
|
||||||
|
@ -21,7 +21,8 @@
|
|||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
|
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
|
||||||
"no_udn": "SSDP discovery info has no UDN",
|
"no_udn": "SSDP discovery info has no UDN",
|
||||||
"not_keenetic_ndms2": "Discovered device is not a Keenetic router"
|
"not_keenetic_ndms2": "Discovered device is not a Keenetic router",
|
||||||
|
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"options": {
|
"options": {
|
||||||
|
@ -25,6 +25,12 @@ MOCK_DATA = {
|
|||||||
CONF_PORT: 23,
|
CONF_PORT: 23,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MOCK_RECONFIGURE = {
|
||||||
|
CONF_USERNAME: "user1",
|
||||||
|
CONF_PASSWORD: "pass1",
|
||||||
|
CONF_PORT: 123,
|
||||||
|
}
|
||||||
|
|
||||||
MOCK_OPTIONS = {
|
MOCK_OPTIONS = {
|
||||||
CONF_SCAN_INTERVAL: 15,
|
CONF_SCAN_INTERVAL: 15,
|
||||||
const.CONF_CONSIDER_HOME: 150,
|
const.CONF_CONSIDER_HOME: 150,
|
||||||
|
@ -19,7 +19,14 @@ from homeassistant.helpers.service_info.ssdp import (
|
|||||||
ATTR_UPNP_UDN,
|
ATTR_UPNP_UDN,
|
||||||
)
|
)
|
||||||
|
|
||||||
from . import MOCK_DATA, MOCK_NAME, MOCK_OPTIONS, MOCK_SSDP_DISCOVERY_INFO
|
from . import (
|
||||||
|
MOCK_DATA,
|
||||||
|
MOCK_IP,
|
||||||
|
MOCK_NAME,
|
||||||
|
MOCK_OPTIONS,
|
||||||
|
MOCK_RECONFIGURE,
|
||||||
|
MOCK_SSDP_DISCOVERY_INFO,
|
||||||
|
)
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
@ -75,6 +82,34 @@ async def test_flow_works(hass: HomeAssistant, connect) -> None:
|
|||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reconfigure(hass: HomeAssistant, connect) -> None:
|
||||||
|
"""Test reconfigure flow."""
|
||||||
|
entry = MockConfigEntry(domain=keenetic.DOMAIN, data=MOCK_DATA)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
result = await entry.start_reconfigure_flow(hass)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "user"
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.keenetic_ndms2.async_setup_entry", return_value=True
|
||||||
|
) as mock_setup_entry:
|
||||||
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
user_input=MOCK_RECONFIGURE,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result2["type"] is FlowResultType.ABORT
|
||||||
|
assert result2["reason"] == "reconfigure_successful"
|
||||||
|
assert entry.data == {
|
||||||
|
CONF_HOST: MOCK_IP,
|
||||||
|
**MOCK_RECONFIGURE,
|
||||||
|
}
|
||||||
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
async def test_options(hass: HomeAssistant) -> None:
|
async def test_options(hass: HomeAssistant) -> None:
|
||||||
"""Test updating options."""
|
"""Test updating options."""
|
||||||
entry = MockConfigEntry(domain=keenetic.DOMAIN, data=MOCK_DATA)
|
entry = MockConfigEntry(domain=keenetic.DOMAIN, data=MOCK_DATA)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user