Raise ConfigEntryError when serial number is missing in huawei_lte (#185045)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
G Johansson
2026-10-08 22:28:06 +02:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 17274410ab
commit 7d9d3bc994
3 changed files with 76 additions and 12 deletions
+13 -12
View File
@@ -36,7 +36,11 @@ from homeassistant.const import (
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.exceptions import (
ConfigEntryAuthFailed,
ConfigEntryError,
ConfigEntryNotReady,
)
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
@@ -327,18 +331,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: HuaweiLteConfigEntry) ->
)
else:
await hass.async_add_executor_job(router.cleanup)
msg = (
"Could not resolve serial number to use as unique id for router at %s"
", setup failed"
raise ConfigEntryError(
translation_domain=DOMAIN,
translation_key=(
"serial_number_not_found"
if entry.data.get(CONF_PASSWORD)
else "serial_number_not_found_unauthenticated"
),
translation_placeholders={"url": url},
)
if not entry.data.get(CONF_PASSWORD):
msg += (
". Try setting up credentials for the router for one startup, "
"unauthenticated mode can be enabled after that in integration "
"settings"
)
_LOGGER.error(msg, url)
return False
# Store reference to router
entry.runtime_data = router
@@ -382,6 +382,12 @@
"exceptions": {
"send_message_failed": {
"message": "Failed to send SMS to {targets}: {error}"
},
"serial_number_not_found": {
"message": "Could not resolve serial number to use as unique ID for router at {url}"
},
"serial_number_not_found_unauthenticated": {
"message": "Could not resolve serial number to use as unique ID for router at {url}. Try setting up credentials for the router for one startup, unauthenticated mode can be enabled after that in integration settings"
}
},
"options": {
+57
View File
@@ -0,0 +1,57 @@
"""Tests for the Huawei LTE integration setup."""
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
from homeassistant.components.huawei_lte.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME
from homeassistant.core import HomeAssistant
from . import magic_client
from tests.common import MockConfigEntry
@pytest.mark.parametrize(
("data", "reason"),
[
pytest.param(
{CONF_URL: "http://huawei-lte"},
"Could not resolve serial number to use as unique ID for router at"
" http://huawei-lte. Try setting up credentials for the router for one"
" startup, unauthenticated mode can be enabled after that in integration"
" settings",
id="unauthenticated",
),
pytest.param(
{
CONF_URL: "http://huawei-lte",
CONF_USERNAME: "admin",
CONF_PASSWORD: "password",
},
"Could not resolve serial number to use as unique ID for router at"
" http://huawei-lte",
id="authenticated",
),
],
)
async def test_setup_serial_number_not_found(
hass: HomeAssistant, data: dict[str, Any], reason: str
) -> None:
"""Test setup fails when the serial number can't be resolved."""
entry = MockConfigEntry(domain=DOMAIN, data=data)
entry.add_to_hass(hass)
client = magic_client()
client.device.information.return_value = {}
with (
patch("homeassistant.components.huawei_lte.Connection", MagicMock()),
patch("homeassistant.components.huawei_lte.Client", return_value=client),
):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.SETUP_ERROR
assert entry.reason == reason