Add reconfigure flow to Qube heat pump (#184685)

This commit is contained in:
Mattie
2026-10-07 13:58:34 +02:00
committed by GitHub
parent 3f86fd94dd
commit c5b3e9fc74
4 changed files with 179 additions and 7 deletions
@@ -8,10 +8,13 @@ from python_qube_heatpump import QubeClient, async_get_device_info, parse_device
from homeassistant.components import zeroconf
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.helpers.selector import TextSelector
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
from .const import DEFAULT_PORT, DOMAIN, MDNS_LOOKUP_TIMEOUT
HOST_SCHEMA = probatio.Schema({probatio.Required(CONF_HOST): TextSelector()})
async def _async_validate_device(host: str) -> str | None:
"""Connect and verify the device is a Qube; return an error key on failure."""
@@ -60,12 +63,9 @@ class QubeConfigFlow(ConfigFlow, domain=DOMAIN):
self._abort_if_unique_id_configured(updates={CONF_HOST: host})
return self._async_create_qube_entry(host)
schema = probatio.Schema(
{
probatio.Required(CONF_HOST): str,
}
return self.async_show_form(
step_id="user", data_schema=HOST_SCHEMA, errors=errors
)
return self.async_show_form(step_id="user", data_schema=schema, errors=errors)
@override
async def async_step_zeroconf(
@@ -114,6 +114,40 @@ class QubeConfigFlow(ConfigFlow, domain=DOMAIN):
errors=errors,
)
async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Change the host of an existing entry."""
entry = self._get_reconfigure_entry()
errors: dict[str, str] = {}
if user_input is not None:
host = user_input[CONF_HOST]
self._async_abort_entries_match({CONF_HOST: host})
if error := await _async_validate_device(host):
errors["base"] = error
else:
# Refuse a different controller when both uuids are known;
# without mDNS the Modbus check above is all we can verify
if entry.unique_id is not None:
aiozc = await zeroconf.async_get_async_instance(self.hass)
if device := await async_get_device_info(
host, aiozc, timeout=MDNS_LOOKUP_TIMEOUT
):
await self.async_set_unique_id(device.uuid)
self._abort_if_unique_id_mismatch()
return self.async_update_reload_and_abort(
entry, data_updates={CONF_HOST: host}
)
return self.async_show_form(
step_id="reconfigure",
data_schema=self.add_suggested_values_to_schema(HOST_SCHEMA, entry.data),
errors=errors,
)
def _async_create_qube_entry(self, host: str) -> ConfigFlowResult:
"""Create the config entry for a verified Qube."""
return self.async_create_entry(
@@ -68,7 +68,7 @@ rules:
entity-translations: done
exception-translations: done
icon-translations: done
reconfiguration-flow: todo
reconfiguration-flow: done
repair-issues:
status: exempt
comment: |
@@ -2,7 +2,8 @@
"config": {
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"not_qube_device": "The discovered device is not a Qube heat pump."
"not_qube_device": "The discovered device is not a Qube heat pump.",
"unique_id_mismatch": "The device at this address is a different Qube heat pump. Enter the address of the heat pump that was set up before."
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
@@ -10,6 +11,15 @@
},
"flow_title": "Qube heat pump ({host})",
"step": {
"reconfigure": {
"data": {
"host": "[%key:common::config_flow::data::host%]"
},
"data_description": {
"host": "[%key:component::hr_energy_qube::config::step::user::data_description::host%]"
},
"description": "Enter the new IP address or hostname of your Qube heat pump."
},
"user": {
"data": {
"host": "[%key:common::config_flow::data::host%]"
@@ -305,3 +305,131 @@ async def test_zeroconf_not_qube(hass: HomeAssistant) -> None:
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "not_qube_device"
@pytest.mark.parametrize(
("unique_id", "device_info"),
[
pytest.param(DEVICE_INFO.uuid, DEVICE_INFO, id="same_controller"),
pytest.param(DEVICE_INFO.uuid, None, id="no_mdns"),
pytest.param(None, DEVICE_INFO, id="no_unique_id"),
],
)
async def test_reconfigure(
hass: HomeAssistant,
mock_qube_client: MagicMock,
mock_device_info: AsyncMock,
unique_id: str | None,
device_info: QubeDeviceInfo | None,
) -> None:
"""Test changing the host of an existing entry."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_HOST: "192.168.5.100", CONF_PORT: 502},
unique_id=unique_id,
)
entry.add_to_hass(hass)
mock_device_info.return_value = device_info
result = await entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "192.168.5.208"}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert entry.data == {CONF_HOST: "192.168.5.208", CONF_PORT: 502}
assert entry.unique_id == unique_id
async def test_reconfigure_other_controller(
hass: HomeAssistant,
mock_qube_client: MagicMock,
mock_device_info: AsyncMock,
) -> None:
"""Test reconfiguring to a different Qube is refused."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_HOST: "192.168.5.100", CONF_PORT: 502},
unique_id="0001000000000001",
)
entry.add_to_hass(hass)
mock_device_info.return_value = DEVICE_INFO
result = await entry.start_reconfigure_flow(hass)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "192.168.5.208"}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unique_id_mismatch"
assert entry.data == {CONF_HOST: "192.168.5.100", CONF_PORT: 502}
async def test_reconfigure_host_of_other_entry(
hass: HomeAssistant,
mock_qube_client: MagicMock,
) -> None:
"""Test reconfiguring to the host of another entry is refused."""
other_entry = MockConfigEntry(
domain=DOMAIN, data={CONF_HOST: "192.168.5.208", CONF_PORT: 502}
)
other_entry.add_to_hass(hass)
entry = MockConfigEntry(
domain=DOMAIN, data={CONF_HOST: "192.168.5.100", CONF_PORT: 502}
)
entry.add_to_hass(hass)
result = await entry.start_reconfigure_flow(hass)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "192.168.5.208"}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert entry.data == {CONF_HOST: "192.168.5.100", CONF_PORT: 502}
@pytest.mark.parametrize(
("connect_result", "version_result", "error"),
[
pytest.param(False, "2.15", "cannot_connect", id="cannot_connect"),
pytest.param(True, None, "not_qube_device", id="not_qube_device"),
],
)
async def test_reconfigure_errors(
hass: HomeAssistant,
mock_qube_client: MagicMock,
connect_result: bool,
version_result: str | None,
error: str,
) -> None:
"""Test reconfigure reports Modbus errors and can be retried."""
entry = MockConfigEntry(
domain=DOMAIN, data={CONF_HOST: "192.168.5.100", CONF_PORT: 502}
)
entry.add_to_hass(hass)
mock_qube_client.connect = AsyncMock(return_value=connect_result)
mock_qube_client.async_get_software_version = AsyncMock(return_value=version_result)
result = await entry.start_reconfigure_flow(hass)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "192.168.5.208"}
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": error}
mock_qube_client.connect = AsyncMock(return_value=True)
mock_qube_client.async_get_software_version = AsyncMock(return_value="2.15")
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "192.168.5.208"}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert entry.data[CONF_HOST] == "192.168.5.208"