diff --git a/homeassistant/components/pooldose/config_flow.py b/homeassistant/components/pooldose/config_flow.py index e4bf114a936..36cd93b7515 100644 --- a/homeassistant/components/pooldose/config_flow.py +++ b/homeassistant/components/pooldose/config_flow.py @@ -12,6 +12,7 @@ import voluptuous as vol from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo from .const import DOMAIN @@ -25,10 +26,77 @@ SCHEMA_DEVICE = vol.Schema( class PooldoseConfigFlow(ConfigFlow, domain=DOMAIN): - """Handle a config flow for Seko Pooldose.""" + """Config flow for the Pooldose integration including DHCP discovery.""" VERSION = 1 + def __init__(self) -> None: + """Initialize the config flow and store the discovered IP address.""" + super().__init__() + self._discovered_ip: str | None = None + + async def _validate_host( + self, host: str + ) -> tuple[str | None, dict[str, str] | None, dict[str, str] | None]: + """Validate the host and return (serial_number, api_versions, errors).""" + client = PooldoseClient(host) + client_status = await client.connect() + if client_status == RequestStatus.HOST_UNREACHABLE: + return None, None, {"base": "cannot_connect"} + if client_status == RequestStatus.PARAMS_FETCH_FAILED: + return None, None, {"base": "params_fetch_failed"} + if client_status != RequestStatus.SUCCESS: + return None, None, {"base": "cannot_connect"} + + api_status, api_versions = client.check_apiversion_supported() + if api_status == RequestStatus.NO_DATA: + return None, None, {"base": "api_not_set"} + if api_status == RequestStatus.API_VERSION_UNSUPPORTED: + return None, api_versions, {"base": "api_not_supported"} + + device_info = client.device_info + if not device_info: + return None, None, {"base": "no_device_info"} + serial_number = device_info.get("SERIAL_NUMBER") + if not serial_number: + return None, None, {"base": "no_serial_number"} + + return serial_number, None, None + + async def async_step_dhcp( + self, discovery_info: DhcpServiceInfo + ) -> ConfigFlowResult: + """Handle DHCP discovery: validate device and update IP if needed.""" + serial_number, _, _ = await self._validate_host(discovery_info.ip) + if not serial_number: + return self.async_abort(reason="no_serial_number") + + await self.async_set_unique_id(serial_number) + + # Conditionally update IP and abort if entry exists + self._abort_if_unique_id_configured(updates={CONF_HOST: discovery_info.ip}) + + # Continue with new device flow + self._discovered_ip = discovery_info.ip + return self.async_show_form( + step_id="dhcp_confirm", + description_placeholders={ + "ip": discovery_info.ip, + "mac": discovery_info.macaddress, + "name": f"PoolDose {serial_number}", + }, + ) + + async def async_step_dhcp_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Create the entry after the confirmation dialog.""" + discovered_ip = self._discovered_ip + return self.async_create_entry( + title=f"PoolDose {self.unique_id}", + data={CONF_HOST: discovered_ip}, + ) + async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: @@ -40,58 +108,16 @@ class PooldoseConfigFlow(ConfigFlow, domain=DOMAIN): ) host = user_input[CONF_HOST] - client = PooldoseClient(host) - client_status = await client.connect() - if client_status == RequestStatus.HOST_UNREACHABLE: + serial_number, api_versions, errors = await self._validate_host(host) + if errors: return self.async_show_form( step_id="user", data_schema=SCHEMA_DEVICE, - errors={"base": "cannot_connect"}, - ) - if client_status == RequestStatus.PARAMS_FETCH_FAILED: - return self.async_show_form( - step_id="user", - data_schema=SCHEMA_DEVICE, - errors={"base": "params_fetch_failed"}, - ) - if client_status != RequestStatus.SUCCESS: - return self.async_show_form( - step_id="user", - data_schema=SCHEMA_DEVICE, - errors={"base": "cannot_connect"}, - ) - - api_status, api_versions = client.check_apiversion_supported() - if api_status == RequestStatus.NO_DATA: - return self.async_show_form( - step_id="user", - data_schema=SCHEMA_DEVICE, - errors={"base": "api_not_set"}, - ) - if api_status == RequestStatus.API_VERSION_UNSUPPORTED: - return self.async_show_form( - step_id="user", - data_schema=SCHEMA_DEVICE, - errors={"base": "api_not_supported"}, + errors=errors, description_placeholders=api_versions, ) - device_info = client.device_info - if not device_info: - return self.async_show_form( - step_id="user", - data_schema=SCHEMA_DEVICE, - errors={"base": "no_device_info"}, - ) - serial_number = device_info.get("SERIAL_NUMBER") - if not serial_number: - return self.async_show_form( - step_id="user", - data_schema=SCHEMA_DEVICE, - errors={"base": "no_serial_number"}, - ) - - await self.async_set_unique_id(serial_number) + await self.async_set_unique_id(serial_number, raise_on_progress=False) self._abort_if_unique_id_configured() return self.async_create_entry( title=f"PoolDose {serial_number}", diff --git a/homeassistant/components/pooldose/manifest.json b/homeassistant/components/pooldose/manifest.json index 8bcbb18737c..5328edce108 100644 --- a/homeassistant/components/pooldose/manifest.json +++ b/homeassistant/components/pooldose/manifest.json @@ -3,6 +3,11 @@ "name": "SEKO PoolDose", "codeowners": ["@lmaertin"], "config_flow": true, + "dhcp": [ + { + "hostname": "kommspot" + } + ], "documentation": "https://www.home-assistant.io/integrations/pooldose", "iot_class": "local_polling", "quality_scale": "bronze", diff --git a/homeassistant/components/pooldose/quality_scale.yaml b/homeassistant/components/pooldose/quality_scale.yaml index dc3c2221d73..3c685e8c511 100644 --- a/homeassistant/components/pooldose/quality_scale.yaml +++ b/homeassistant/components/pooldose/quality_scale.yaml @@ -44,12 +44,8 @@ rules: # Gold devices: done diagnostics: todo - discovery-update-info: - status: todo - comment: DHCP discovery is possible - discovery: - status: todo - comment: DHCP discovery is possible + discovery-update-info: done + discovery: done docs-data-update: done docs-examples: todo docs-known-limitations: todo diff --git a/homeassistant/components/pooldose/strings.json b/homeassistant/components/pooldose/strings.json index 1a9dbbf106f..59e2ee7a950 100644 --- a/homeassistant/components/pooldose/strings.json +++ b/homeassistant/components/pooldose/strings.json @@ -10,6 +10,10 @@ "data_description": { "host": "IP address or hostname of your device" } + }, + "dhcp_confirm": { + "title": "Confirm DHCP discovered PoolDose device", + "description": "A PoolDose device was found on your network at {ip} with MAC address {mac}.\n\nDo you want to add {name} to Home Assistant?" } }, "error": { @@ -22,7 +26,10 @@ "unknown": "[%key:common::config_flow::error::unknown%]" }, "abort": { - "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" + "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", + "no_device_info": "Unable to retrieve device information", + "no_serial_number": "No serial number found on the device" } }, "entity": { diff --git a/homeassistant/generated/dhcp.py b/homeassistant/generated/dhcp.py index ab95b106551..e744f42b541 100644 --- a/homeassistant/generated/dhcp.py +++ b/homeassistant/generated/dhcp.py @@ -563,6 +563,10 @@ DHCP: Final[list[dict[str, str | bool]]] = [ "domain": "playstation_network", "macaddress": "84E657*", }, + { + "domain": "pooldose", + "hostname": "kommspot", + }, { "domain": "powerwall", "hostname": "1118431-*", diff --git a/tests/components/pooldose/fixtures/deviceinfo.json b/tests/components/pooldose/fixtures/deviceinfo.json index 528be8757e6..69ac3ba0a0a 100644 --- a/tests/components/pooldose/fixtures/deviceinfo.json +++ b/tests/components/pooldose/fixtures/deviceinfo.json @@ -10,6 +10,6 @@ "SW_VERSION": "2.10", "API_VERSION": "v1/", "FW_CODE": "539187", - "MAC": "AA:BB:CC:DD:EE:FF", + "MAC": "", "IP": "192.168.1.100" } diff --git a/tests/components/pooldose/test_config_flow.py b/tests/components/pooldose/test_config_flow.py index 6229526dd9a..777f2843bba 100644 --- a/tests/components/pooldose/test_config_flow.py +++ b/tests/components/pooldose/test_config_flow.py @@ -6,10 +6,11 @@ from unittest.mock import AsyncMock import pytest from homeassistant.components.pooldose.const import DOMAIN -from homeassistant.config_entries import SOURCE_USER +from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType +from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo from .conftest import RequestStatus @@ -237,3 +238,120 @@ async def test_duplicate_entry_aborts( ) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_configured" + + +async def test_dhcp_flow(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: + """Test the full DHCP config flow.""" + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_DHCP}, + data=DhcpServiceInfo( + ip="192.168.0.123", hostname="kommspot", macaddress="a4e57caabbcc" + ), + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "dhcp_confirm" + + result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "PoolDose TEST123456789" + assert result["data"] == {CONF_HOST: "192.168.0.123"} + assert result["result"].unique_id == "TEST123456789" + + +async def test_dhcp_no_serial_number( + hass: HomeAssistant, mock_pooldose_client: AsyncMock, mock_setup_entry: AsyncMock +) -> None: + """Test that the DHCP flow aborts if no serial number is found.""" + mock_pooldose_client.device_info = {"NAME": "Pool Device", "MODEL": "POOL DOSE"} + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_DHCP}, + data=DhcpServiceInfo( + ip="192.168.0.123", hostname="kommspot", macaddress="a4e57caabbcc" + ), + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "no_serial_number" + + +@pytest.mark.parametrize( + ("client_status"), + [ + (RequestStatus.HOST_UNREACHABLE), + (RequestStatus.PARAMS_FETCH_FAILED), + (RequestStatus.UNKNOWN_ERROR), + ], +) +async def test_dhcp_connection_errors( + hass: HomeAssistant, + mock_pooldose_client: AsyncMock, + mock_setup_entry: AsyncMock, + client_status: str, +) -> None: + """Test that the DHCP flow aborts on connection errors.""" + mock_pooldose_client.connect.return_value = client_status + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_DHCP}, + data=DhcpServiceInfo( + ip="192.168.0.123", hostname="kommspot", macaddress="a4e57caabbcc" + ), + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "no_serial_number" + + +@pytest.mark.parametrize( + "api_status", + [ + RequestStatus.NO_DATA, + RequestStatus.API_VERSION_UNSUPPORTED, + ], +) +async def test_dhcp_api_errors( + hass: HomeAssistant, + mock_pooldose_client: AsyncMock, + mock_setup_entry: AsyncMock, + api_status: str, +) -> None: + """Test that the DHCP flow aborts on API errors.""" + mock_pooldose_client.check_apiversion_supported.return_value = (api_status, {}) + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_DHCP}, + data=DhcpServiceInfo( + ip="192.168.0.123", hostname="kommspot", macaddress="a4e57caabbcc" + ), + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "no_serial_number" + + +async def test_dhcp_updates_host( + hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_setup_entry: AsyncMock +) -> None: + """Test that DHCP discovery updates the host if it has changed.""" + mock_config_entry.add_to_hass(hass) + + # Verify initial host IP + assert mock_config_entry.data[CONF_HOST] == "192.168.1.100" + + # Simulate DHCP discovery event with different IP + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_DHCP}, + data=DhcpServiceInfo( + ip="192.168.0.123", hostname="kommspot", macaddress="a4e57caabbcc" + ), + ) + + # Verify flow aborts as device is already configured + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + assert mock_config_entry.data[CONF_HOST] == "192.168.0.123"