Add reconfiguration flow for Powerfox Local integration (#164002)

This commit is contained in:
Klaas Schoute
2026-02-25 01:05:30 +01:00
committed by GitHub
parent 7ae0380b33
commit 249e6c2f3d
4 changed files with 135 additions and 7 deletions

View File

@@ -8,7 +8,12 @@ from typing import Any
from powerfox import PowerfoxAuthenticationError, PowerfoxConnectionError, PowerfoxLocal
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.config_entries import (
SOURCE_RECONFIGURE,
SOURCE_USER,
ConfigFlow,
ConfigFlowResult,
)
from homeassistant.const import CONF_API_KEY, CONF_HOST
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
@@ -54,7 +59,15 @@ class PowerfoxLocalConfigFlow(ConfigFlow, domain=DOMAIN):
except PowerfoxConnectionError:
errors["base"] = "cannot_connect"
else:
return self._async_create_entry()
if self.source == SOURCE_USER:
return self._async_create_entry()
return self.async_update_reload_and_abort(
self._get_reconfigure_entry(),
data={
CONF_HOST: self._host,
CONF_API_KEY: self._api_key,
},
)
return self.async_show_form(
step_id="user",
@@ -130,6 +143,12 @@ class PowerfoxLocalConfigFlow(ConfigFlow, domain=DOMAIN):
errors=errors,
)
async def async_step_reconfigure(
self, user_input: Mapping[str, Any]
) -> ConfigFlowResult:
"""Handle reconfiguration."""
return await self.async_step_user()
def _async_create_entry(self) -> ConfigFlowResult:
"""Create a config entry."""
return self.async_create_entry(
@@ -149,5 +168,8 @@ class PowerfoxLocalConfigFlow(ConfigFlow, domain=DOMAIN):
)
await client.value()
await self.async_set_unique_id(self._device_id)
self._abort_if_unique_id_configured(updates={CONF_HOST: self._host})
await self.async_set_unique_id(self._device_id, raise_on_progress=False)
if self.source == SOURCE_RECONFIGURE:
self._abort_if_unique_id_mismatch()
else:
self._abort_if_unique_id_configured(updates={CONF_HOST: self._host})

View File

@@ -74,7 +74,7 @@ rules:
status: exempt
comment: |
There is no need for icon translations.
reconfiguration-flow: todo
reconfiguration-flow: done
repair-issues:
status: exempt
comment: |

View File

@@ -3,7 +3,9 @@
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]",
"unique_id_mismatch": "Please ensure you reconfigure against the same device."
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",

View File

@@ -1,8 +1,9 @@
"""Test the Powerfox Local config flow."""
from datetime import UTC, datetime
from unittest.mock import AsyncMock
from powerfox import PowerfoxAuthenticationError, PowerfoxConnectionError
from powerfox import LocalResponse, PowerfoxAuthenticationError, PowerfoxConnectionError
import pytest
from homeassistant.components.powerfox_local.const import DOMAIN
@@ -251,3 +252,106 @@ async def test_step_reauth_exceptions(
assert result.get("reason") == "reauth_successful"
assert mock_config_entry.data[CONF_API_KEY] == "new-api-key"
async def test_reconfigure_flow(
hass: HomeAssistant,
mock_powerfox_local_client: AsyncMock,
mock_config_entry: MockConfigEntry,
mock_setup_entry: AsyncMock,
) -> None:
"""Test reconfiguration flow."""
mock_config_entry.add_to_hass(hass)
result = await mock_config_entry.start_reconfigure_flow(hass)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_HOST: "192.168.1.200", CONF_API_KEY: MOCK_API_KEY},
)
assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "reconfigure_successful"
assert mock_config_entry.data[CONF_HOST] == "192.168.1.200"
assert mock_config_entry.data[CONF_API_KEY] == MOCK_API_KEY
@pytest.mark.parametrize(
("exception", "error"),
[
(PowerfoxConnectionError, "cannot_connect"),
(PowerfoxAuthenticationError, "invalid_auth"),
],
)
async def test_reconfigure_flow_exceptions(
hass: HomeAssistant,
mock_powerfox_local_client: AsyncMock,
mock_config_entry: MockConfigEntry,
mock_setup_entry: AsyncMock,
exception: Exception,
error: str,
) -> None:
"""Test exceptions during reconfiguration flow."""
mock_powerfox_local_client.value.side_effect = exception
mock_config_entry.add_to_hass(hass)
result = await mock_config_entry.start_reconfigure_flow(hass)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_HOST: "192.168.1.200", CONF_API_KEY: MOCK_API_KEY},
)
assert result.get("type") is FlowResultType.FORM
assert result.get("errors") == {"base": error}
# Recover from error
mock_powerfox_local_client.value.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_HOST: "192.168.1.200", CONF_API_KEY: MOCK_API_KEY},
)
assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "reconfigure_successful"
assert mock_config_entry.data[CONF_HOST] == "192.168.1.200"
async def test_reconfigure_flow_unique_id_mismatch(
hass: HomeAssistant,
mock_powerfox_local_client: AsyncMock,
mock_config_entry: MockConfigEntry,
mock_setup_entry: AsyncMock,
) -> None:
"""Test reconfiguration aborts on unique ID mismatch."""
mock_config_entry.add_to_hass(hass)
result = await mock_config_entry.start_reconfigure_flow(hass)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "user"
# Return response with different API key (which serves as device_id)
mock_powerfox_local_client.value.return_value = LocalResponse(
timestamp=datetime(2026, 2, 25, 10, 48, 51, tzinfo=UTC),
power=111,
energy_usage=1111111,
energy_return=111111,
energy_usage_high_tariff=111111,
energy_usage_low_tariff=111111,
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_HOST: "192.168.1.200", CONF_API_KEY: "different_api_key"},
)
assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "unique_id_mismatch"