Skip to content

Commit

Permalink
Support reconfigure flow in Nettigo Air Monitor integration (#117318)
Browse files Browse the repository at this point in the history
* Add reconfigure flow

* Fix input

* Add tests

---------

Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com>
  • Loading branch information
bieniu and bieniu committed May 13, 2024
1 parent c3f95a4 commit 0d09226
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 3 deletions.
47 changes: 46 additions & 1 deletion homeassistant/components/nam/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from collections.abc import Mapping
from dataclasses import dataclass
import logging
from typing import Any
from typing import TYPE_CHECKING, Any

from aiohttp.client_exceptions import ClientConnectorError
from nettigo_air_monitor import (
Expand Down Expand Up @@ -227,3 +227,48 @@ async def async_step_reauth_confirm(
data_schema=AUTH_SCHEMA,
errors=errors,
)

async def async_step_reconfigure(
self, _: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle a reconfiguration flow initialized by the user."""
entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])

if TYPE_CHECKING:
assert entry is not None

self.host = entry.data[CONF_HOST]
self.entry = entry

return await self.async_step_reconfigure_confirm()

async def async_step_reconfigure_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle a reconfiguration flow initialized by the user."""
errors = {}

if user_input is not None:
try:
config = await async_get_config(self.hass, user_input[CONF_HOST])
except (ApiError, ClientConnectorError, TimeoutError):
errors["base"] = "cannot_connect"
else:
if format_mac(config.mac_address) != self.entry.unique_id:
return self.async_abort(reason="another_device")

data = {**self.entry.data, CONF_HOST: user_input[CONF_HOST]}
self.hass.config_entries.async_update_entry(self.entry, data=data)
await self.hass.config_entries.async_reload(self.entry.entry_id)
return self.async_abort(reason="reconfigure_successful")

return self.async_show_form(
step_id="reconfigure_confirm",
data_schema=vol.Schema(
{
vol.Required(CONF_HOST, default=self.host): str,
}
),
description_placeholders={"device_name": self.entry.title},
errors=errors,
)
12 changes: 11 additions & 1 deletion homeassistant/components/nam/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
},
"confirm_discovery": {
"description": "Do you want to set up Nettigo Air Monitor at {host}?"
},
"reconfigure_confirm": {
"description": "Update configuration for {device_name}.",
"data": {
"host": "[%key:common::config_flow::data::host%]"
},
"data_description": {
"host": "[%key:component::nam::config::step::user::data_description::host%]"
}
}
},
"error": {
Expand All @@ -38,7 +47,8 @@
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"device_unsupported": "The device is unsupported.",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"reauth_unsuccessful": "Re-authentication was unsuccessful, please remove the integration and set it up again."
"reauth_unsuccessful": "Re-authentication was unsuccessful, please remove the integration and set it up again.",
"another_device": "The IP address/hostname of another Nettigo Air Monitor was used."
}
},
"entity": {
Expand Down
166 changes: 165 additions & 1 deletion tests/components/nam/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@

from homeassistant.components import zeroconf
from homeassistant.components.nam.const import DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER, SOURCE_ZEROCONF
from homeassistant.config_entries import (
SOURCE_REAUTH,
SOURCE_RECONFIGURE,
SOURCE_USER,
SOURCE_ZEROCONF,
)
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType

Expand Down Expand Up @@ -437,3 +443,161 @@ async def test_zeroconf_errors(hass: HomeAssistant, error) -> None:

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


async def test_reconfigure_successful(hass: HomeAssistant) -> None:
"""Test starting a reconfigure flow."""
entry = MockConfigEntry(
domain=DOMAIN,
title="10.10.2.3",
unique_id="aa:bb:cc:dd:ee:ff",
data={
CONF_HOST: "10.10.2.3",
CONF_USERNAME: "fake_username",
CONF_PASSWORD: "fake_password",
},
)
entry.add_to_hass(hass)

result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": SOURCE_RECONFIGURE,
"entry_id": entry.entry_id,
},
data=entry.data,
)

assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"

with (
patch(
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
return_value=DEVICE_CONFIG_AUTH,
),
patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
return_value="aa:bb:cc:dd:ee:ff",
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_HOST: "10.10.10.10"},
)

assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert entry.data == {
CONF_HOST: "10.10.10.10",
CONF_USERNAME: "fake_username",
CONF_PASSWORD: "fake_password",
}


async def test_reconfigure_not_successful(hass: HomeAssistant) -> None:
"""Test starting a reconfigure flow but no connection found."""
entry = MockConfigEntry(
domain=DOMAIN,
title="10.10.2.3",
unique_id="aa:bb:cc:dd:ee:ff",
data={
CONF_HOST: "10.10.2.3",
CONF_USERNAME: "fake_username",
CONF_PASSWORD: "fake_password",
},
)
entry.add_to_hass(hass)

result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": SOURCE_RECONFIGURE,
"entry_id": entry.entry_id,
},
data=entry.data,
)

assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"

with patch(
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
side_effect=ApiError("API Error"),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_HOST: "10.10.10.10"},
)

assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"
assert result["errors"] == {"base": "cannot_connect"}

with (
patch(
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
return_value=DEVICE_CONFIG_AUTH,
),
patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
return_value="aa:bb:cc:dd:ee:ff",
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_HOST: "10.10.10.10"},
)

assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert entry.data == {
CONF_HOST: "10.10.10.10",
CONF_USERNAME: "fake_username",
CONF_PASSWORD: "fake_password",
}


async def test_reconfigure_not_the_same_device(hass: HomeAssistant) -> None:
"""Test starting the reconfiguration process, but with a different printer."""
entry = MockConfigEntry(
domain=DOMAIN,
title="10.10.2.3",
unique_id="11:22:33:44:55:66",
data={
CONF_HOST: "10.10.2.3",
CONF_USERNAME: "fake_username",
CONF_PASSWORD: "fake_password",
},
)
entry.add_to_hass(hass)

result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": SOURCE_RECONFIGURE,
"entry_id": entry.entry_id,
},
data=entry.data,
)

assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"

with (
patch(
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
return_value=DEVICE_CONFIG_AUTH,
),
patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
return_value="aa:bb:cc:dd:ee:ff",
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_HOST: "10.10.10.10"},
)

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

0 comments on commit 0d09226

Please sign in to comment.