Use serial number for AirVisal Pro config entry unique ID (#84902)
* Use serial number for AirVisal Pro config entry unique ID * Code review
This commit is contained in:
parent
fdf2f8a2ea
commit
34b5928707
4 changed files with 45 additions and 23 deletions
|
@ -2,6 +2,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
|
from dataclasses import dataclass, field
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from pyairvisual.node import (
|
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]:
|
@dataclass
|
||||||
"""Validate an IP address/password combo (and return any errors as appropriate)."""
|
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)
|
node = NodeSamba(ip_address, password)
|
||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await node.async_connect()
|
await node.async_connect()
|
||||||
|
measurements = await node.async_get_latest_measurements()
|
||||||
except InvalidAuthenticationError as err:
|
except InvalidAuthenticationError as err:
|
||||||
LOGGER.error("Invalid password for Pro at IP address %s: %s", ip_address, err)
|
LOGGER.error("Invalid password for Pro at IP address %s: %s", ip_address, err)
|
||||||
errors["base"] = "invalid_auth"
|
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
|
except Exception as err: # pylint: disable=broad-except
|
||||||
LOGGER.exception("Unknown error while connecting to %s: %s", ip_address, err)
|
LOGGER.exception("Unknown error while connecting to %s: %s", ip_address, err)
|
||||||
errors["base"] = "unknown"
|
errors["base"] = "unknown"
|
||||||
|
else:
|
||||||
|
return ValidationResult(serial_number=measurements["serial_number"])
|
||||||
finally:
|
finally:
|
||||||
await node.async_disconnect()
|
await node.async_disconnect()
|
||||||
|
|
||||||
return errors
|
return ValidationResult(errors=errors)
|
||||||
|
|
||||||
|
|
||||||
class AirVisualProFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
class AirVisualProFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
|
@ -89,11 +103,15 @@ class AirVisualProFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
|
|
||||||
assert self._reauth_entry
|
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]
|
self._reauth_entry.data[CONF_IP_ADDRESS], user_input[CONF_PASSWORD]
|
||||||
):
|
)
|
||||||
|
|
||||||
|
if validation_result.errors:
|
||||||
return self.async_show_form(
|
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(
|
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]
|
ip_address = user_input[CONF_IP_ADDRESS]
|
||||||
|
|
||||||
await self.async_set_unique_id(ip_address)
|
validation_result = await async_validate_credentials(
|
||||||
self._abort_if_unique_id_configured()
|
|
||||||
|
|
||||||
if errors := await async_validate_credentials(
|
|
||||||
ip_address, user_input[CONF_PASSWORD]
|
ip_address, user_input[CONF_PASSWORD]
|
||||||
):
|
|
||||||
return self.async_show_form(
|
|
||||||
step_id="user", data_schema=STEP_USER_SCHEMA, errors=errors
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if validation_result.errors:
|
||||||
|
return self.async_show_form(
|
||||||
|
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)
|
return self.async_create_entry(title=ip_address, data=user_input)
|
||||||
|
|
|
@ -12,9 +12,9 @@ from tests.common import MockConfigEntry, load_fixture
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="config_entry")
|
@pytest.fixture(name="config_entry")
|
||||||
def config_entry_fixture(hass, config, unique_id):
|
def config_entry_fixture(hass, config):
|
||||||
"""Define a config entry fixture."""
|
"""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)
|
entry.add_to_hass(hass)
|
||||||
return entry
|
return entry
|
||||||
|
|
||||||
|
@ -69,9 +69,3 @@ async def setup_airvisual_pro_fixture(hass, config, pro):
|
||||||
assert await async_setup_component(hass, DOMAIN, config)
|
assert await async_setup_component(hass, DOMAIN, config)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="unique_id")
|
|
||||||
def unique_id_fixture(hass):
|
|
||||||
"""Define a config entry unique ID fixture."""
|
|
||||||
return "192.168.1.101"
|
|
||||||
|
|
|
@ -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."""
|
"""Test that errors are shown when duplicates are added."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
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["type"] == data_entry_flow.FlowResultType.ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
|
@ -17,7 +17,7 @@ async def test_entry_diagnostics(hass, config_entry, hass_client, setup_airvisua
|
||||||
"pref_disable_new_entities": False,
|
"pref_disable_new_entities": False,
|
||||||
"pref_disable_polling": False,
|
"pref_disable_polling": False,
|
||||||
"source": "user",
|
"source": "user",
|
||||||
"unique_id": "192.168.1.101",
|
"unique_id": "XXXXXXX",
|
||||||
"disabled_by": None,
|
"disabled_by": None,
|
||||||
},
|
},
|
||||||
"data": {
|
"data": {
|
||||||
|
|
Loading…
Add table
Reference in a new issue