Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reconfigure flow to niko_home_control #133993

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions homeassistant/components/niko_home_control/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@

MINOR_VERSION = 2

async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle reconfiguration of the integration."""
errors: dict[str, str] = {}
if user_input is not None:
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
error = await test_connection(user_input[CONF_HOST])
if not error:
return self.async_update_reload_and_abort(

Check warning on line 47 in homeassistant/components/niko_home_control/config_flow.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/niko_home_control/config_flow.py#L44-L47

Added lines #L44 - L47 were not covered by tests
self._get_reconfigure_entry(),
data_updates=user_input,
)
errors["base"] = error

Check warning on line 51 in homeassistant/components/niko_home_control/config_flow.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/niko_home_control/config_flow.py#L51

Added line #L51 was not covered by tests

return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
)

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
Expand Down
80 changes: 80 additions & 0 deletions tests/components/niko_home_control/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,83 @@ async def test_duplicate_import_entry(

assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"


async def test_duplicate_reconfigure_entry(
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_config_entry: MockConfigEntry
) -> None:
"""Test uniqueness."""

mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
result = await mock_config_entry.start_reconfigure_flow(hass)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "192.168.0.123"}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"


async def test_reconfigure_setup(
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_config_entry: MockConfigEntry
) -> None:
"""Test the reconfigure flow."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()

result = await mock_config_entry.start_reconfigure_flow(hass)

assert result["type"] is FlowResultType.FORM
assert set(result["data_schema"].schema) == {CONF_HOST}


async def test_reconfigure(
hass: HomeAssistant,
mock_niko_home_control_connection: AsyncMock,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the reconfigure flow."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()

result = await mock_config_entry.start_reconfigure_flow(hass)

assert result["type"] is FlowResultType.FORM
assert set(result["data_schema"].schema) == {CONF_HOST}

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "192.168.0.122"},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY


async def test_reconfigure_cannot_connect(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the cannot connect error."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()

result = await mock_config_entry.start_reconfigure_flow(hass)

assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}

with patch(
"homeassistant.components.niko_home_control.config_flow.NHCController.connect",
side_effect=Exception,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "192.168.0.122"},
)
assert result["errors"] == {"base": "cannot_connect"}