mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Pooldose: Add Dhcp discovery (#152253)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Joostlek <joostlek@outlook.com>
This commit is contained in:
@@ -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}",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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": {
|
||||
|
||||
4
homeassistant/generated/dhcp.py
generated
4
homeassistant/generated/dhcp.py
generated
@@ -563,6 +563,10 @@ DHCP: Final[list[dict[str, str | bool]]] = [
|
||||
"domain": "playstation_network",
|
||||
"macaddress": "84E657*",
|
||||
},
|
||||
{
|
||||
"domain": "pooldose",
|
||||
"hostname": "kommspot",
|
||||
},
|
||||
{
|
||||
"domain": "powerwall",
|
||||
"hostname": "1118431-*",
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user