diff --git a/homeassistant/components/airvisual_pro/config_flow.py b/homeassistant/components/airvisual_pro/config_flow.py index 7cf03009932..23da39150c5 100644 --- a/homeassistant/components/airvisual_pro/config_flow.py +++ b/homeassistant/components/airvisual_pro/config_flow.py @@ -2,6 +2,7 @@ from __future__ import annotations from collections.abc import Mapping +from dataclasses import dataclass, field from typing import Any from pyairvisual.node import ( @@ -33,13 +34,24 @@ STEP_USER_SCHEMA = vol.Schema( ) -async def async_validate_credentials(ip_address: str, password: str) -> dict[str, Any]: - """Validate an IP address/password combo (and return any errors as appropriate).""" +@dataclass +class ValidationResult: + """Define a validation result.""" + + serial_number: str | None = None + errors: dict[str, Any] = field(default_factory=dict) + + +async def async_validate_credentials( + ip_address: str, password: str +) -> ValidationResult: + """Validate an IP address/password combo.""" node = NodeSamba(ip_address, password) errors = {} try: await node.async_connect() + measurements = await node.async_get_latest_measurements() except InvalidAuthenticationError as err: LOGGER.error("Invalid password for Pro at IP address %s: %s", ip_address, err) errors["base"] = "invalid_auth" @@ -52,10 +64,12 @@ async def async_validate_credentials(ip_address: str, password: str) -> dict[str except Exception as err: # pylint: disable=broad-except LOGGER.exception("Unknown error while connecting to %s: %s", ip_address, err) errors["base"] = "unknown" + else: + return ValidationResult(serial_number=measurements["serial_number"]) finally: await node.async_disconnect() - return errors + return ValidationResult(errors=errors) class AirVisualProFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): @@ -89,11 +103,15 @@ class AirVisualProFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): assert self._reauth_entry - if errors := await async_validate_credentials( + validation_result = await async_validate_credentials( self._reauth_entry.data[CONF_IP_ADDRESS], user_input[CONF_PASSWORD] - ): + ) + + if validation_result.errors: return self.async_show_form( - step_id="reauth_confirm", data_schema=STEP_REAUTH_SCHEMA, errors=errors + step_id="reauth_confirm", + data_schema=STEP_REAUTH_SCHEMA, + errors=validation_result.errors, ) self.hass.config_entries.async_update_entry( @@ -113,14 +131,18 @@ class AirVisualProFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): ip_address = user_input[CONF_IP_ADDRESS] - await self.async_set_unique_id(ip_address) - self._abort_if_unique_id_configured() - - if errors := await async_validate_credentials( + validation_result = await async_validate_credentials( ip_address, user_input[CONF_PASSWORD] - ): + ) + + if validation_result.errors: return self.async_show_form( - step_id="user", data_schema=STEP_USER_SCHEMA, errors=errors + step_id="user", + data_schema=STEP_USER_SCHEMA, + errors=validation_result.errors, ) + await self.async_set_unique_id(validation_result.serial_number) + self._abort_if_unique_id_configured() + return self.async_create_entry(title=ip_address, data=user_input) diff --git a/tests/components/airvisual_pro/conftest.py b/tests/components/airvisual_pro/conftest.py index c5851e940de..5846a988688 100644 --- a/tests/components/airvisual_pro/conftest.py +++ b/tests/components/airvisual_pro/conftest.py @@ -12,9 +12,9 @@ from tests.common import MockConfigEntry, load_fixture @pytest.fixture(name="config_entry") -def config_entry_fixture(hass, config, unique_id): +def config_entry_fixture(hass, config): """Define a config entry fixture.""" - entry = MockConfigEntry(domain=DOMAIN, unique_id=unique_id, data=config) + entry = MockConfigEntry(domain=DOMAIN, unique_id="XXXXXXX", data=config) entry.add_to_hass(hass) return entry @@ -69,9 +69,3 @@ async def setup_airvisual_pro_fixture(hass, config, pro): assert await async_setup_component(hass, DOMAIN, config) await hass.async_block_till_done() yield - - -@pytest.fixture(name="unique_id") -def unique_id_fixture(hass): - """Define a config entry unique ID fixture.""" - return "192.168.1.101" diff --git a/tests/components/airvisual_pro/test_config_flow.py b/tests/components/airvisual_pro/test_config_flow.py index 32c4e14ba76..61cd43f058d 100644 --- a/tests/components/airvisual_pro/test_config_flow.py +++ b/tests/components/airvisual_pro/test_config_flow.py @@ -52,10 +52,16 @@ async def test_create_entry( } -async def test_duplicate_error(hass, config, config_entry): +async def test_duplicate_error(hass, config, config_entry, setup_airvisual_pro): """Test that errors are shown when duplicates are added.""" result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER}, data=config + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] == data_entry_flow.FlowResultType.FORM + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input=config ) assert result["type"] == data_entry_flow.FlowResultType.ABORT assert result["reason"] == "already_configured" diff --git a/tests/components/airvisual_pro/test_diagnostics.py b/tests/components/airvisual_pro/test_diagnostics.py index ab780b90704..5847dd7ed88 100644 --- a/tests/components/airvisual_pro/test_diagnostics.py +++ b/tests/components/airvisual_pro/test_diagnostics.py @@ -17,7 +17,7 @@ async def test_entry_diagnostics(hass, config_entry, hass_client, setup_airvisua "pref_disable_new_entities": False, "pref_disable_polling": False, "source": "user", - "unique_id": "192.168.1.101", + "unique_id": "XXXXXXX", "disabled_by": None, }, "data": {