1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 17:49:37 +01:00

Add reconfigure flow to Saunum integration (#157128)

Co-authored-by: Joostlek <joostlek@outlook.com>
This commit is contained in:
mettolen
2025-11-25 16:10:10 +02:00
committed by GitHub
parent c2219aadb1
commit e496fb2227
5 changed files with 147 additions and 10 deletions
+15 -6
View File
@@ -8,7 +8,7 @@ from typing import Any
from pysaunum import SaunumClient, SaunumException
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.config_entries import SOURCE_USER, ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST
from homeassistant.helpers import config_validation as cv
@@ -46,14 +46,18 @@ class LeilSaunaConfigFlow(ConfigFlow, domain=DOMAIN):
VERSION = 1
MINOR_VERSION = 1
async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle reconfiguration of the integration."""
return await self.async_step_user(user_input)
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input is not None:
# Check for duplicate configuration
self._async_abort_entries_match(user_input)
try:
@@ -64,9 +68,14 @@ class LeilSaunaConfigFlow(ConfigFlow, domain=DOMAIN):
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
return self.async_create_entry(
title="Saunum",
data=user_input,
if self.source == SOURCE_USER:
return self.async_create_entry(
title="Saunum",
data=user_input,
)
return self.async_update_reload_and_abort(
self._get_reconfigure_entry(),
data_updates=user_input,
)
return self.async_show_form(
@@ -45,7 +45,9 @@ rules:
discovery:
status: exempt
comment: Device uses generic Espressif hardware with no unique identifying information (MAC OUI or hostname) that would distinguish it from other Espressif-based devices on the network.
discovery-update-info: todo
discovery-update-info:
status: exempt
comment: Device cannot be discovered and the Modbus TCP API does not provide MAC address or other unique network identifiers needed to update connection information.
docs-data-update: done
docs-examples: todo
docs-known-limitations: done
@@ -62,7 +64,7 @@ rules:
entity-translations: done
exception-translations: done
icon-translations: todo
reconfiguration-flow: todo
reconfiguration-flow: done
repair-issues:
status: exempt
comment: This integration doesn't have any cases where raising an issue is needed.
+11 -1
View File
@@ -1,13 +1,23 @@
{
"config": {
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"step": {
"reconfigure": {
"data": {
"host": "[%key:common::config_flow::data::ip%]"
},
"data_description": {
"host": "[%key:component::saunum::config::step::user::data_description::host%]"
},
"description": "[%key:component::saunum::config::step::user::description%]"
},
"user": {
"data": {
"host": "[%key:common::config_flow::data::ip%]"
+10
View File
@@ -94,3 +94,13 @@ async def init_integration(
await hass.async_block_till_done()
return mock_config_entry
@pytest.fixture
def mock_setup_entry() -> Generator[MagicMock]:
"""Mock Saunum setup entry."""
with patch(
"homeassistant.components.saunum.async_setup_entry", autospec=True
) as mock_setup_entry:
mock_setup_entry.return_value = True
yield mock_setup_entry
+107 -1
View File
@@ -2,6 +2,8 @@
from __future__ import annotations
from unittest.mock import AsyncMock
from pysaunum import SaunumConnectionError, SaunumException
import pytest
@@ -14,10 +16,11 @@ from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
TEST_USER_INPUT = {CONF_HOST: "192.168.1.100"}
TEST_RECONFIGURE_INPUT = {CONF_HOST: "192.168.1.200"}
@pytest.mark.usefixtures("mock_saunum_client")
async def test_full_flow(hass: HomeAssistant) -> None:
async def test_full_flow(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
"""Test full flow."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
@@ -49,6 +52,7 @@ async def test_form_errors(
mock_saunum_client,
side_effect: Exception,
error_base: str,
mock_setup_entry: AsyncMock,
) -> None:
"""Test error handling and recovery."""
mock_saunum_client.connect.side_effect = side_effect
@@ -96,3 +100,105 @@ async def test_form_duplicate(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
@pytest.mark.usefixtures("mock_saunum_client")
@pytest.mark.parametrize("user_input", [TEST_RECONFIGURE_INPUT, TEST_USER_INPUT])
async def test_reconfigure_flow(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
user_input: dict[str, str],
mock_setup_entry: AsyncMock,
) -> None:
"""Test reconfigure flow."""
mock_config_entry.add_to_hass(hass)
result = await mock_config_entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input,
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert mock_config_entry.data == user_input
@pytest.mark.parametrize(
("side_effect", "error_base"),
[
(SaunumConnectionError("Connection failed"), "cannot_connect"),
(SaunumException("Read error"), "cannot_connect"),
(Exception("Unexpected error"), "unknown"),
],
)
async def test_reconfigure_errors(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_saunum_client,
side_effect: Exception,
error_base: str,
mock_setup_entry: AsyncMock,
) -> None:
"""Test reconfigure flow error handling."""
mock_config_entry.add_to_hass(hass)
mock_saunum_client.connect.side_effect = side_effect
result = await mock_config_entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
TEST_RECONFIGURE_INPUT,
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": error_base}
# Test recovery - clear the error and try again
mock_saunum_client.connect.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
TEST_RECONFIGURE_INPUT,
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert mock_config_entry.data == TEST_RECONFIGURE_INPUT
@pytest.mark.usefixtures("mock_saunum_client")
async def test_reconfigure_to_existing_host(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_setup_entry: AsyncMock
) -> None:
"""Test reconfigure flow aborts when changing to a host used by another entry."""
mock_config_entry.add_to_hass(hass)
# Create a second entry with a different host
second_entry = MockConfigEntry(
domain=DOMAIN,
data=TEST_RECONFIGURE_INPUT,
title="Saunum 2",
)
second_entry.add_to_hass(hass)
result = await mock_config_entry.start_reconfigure_flow(hass)
# Try to reconfigure first entry to use the same host as second entry
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
TEST_RECONFIGURE_INPUT, # Same host as second_entry
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
# Verify the original entry was not changed
assert mock_config_entry.data == TEST_USER_INPUT