diff --git a/homeassistant/components/huawei_lte/__init__.py b/homeassistant/components/huawei_lte/__init__.py index 3564f716f27a..cc8edfd7c8c0 100644 --- a/homeassistant/components/huawei_lte/__init__.py +++ b/homeassistant/components/huawei_lte/__init__.py @@ -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 diff --git a/homeassistant/components/huawei_lte/strings.json b/homeassistant/components/huawei_lte/strings.json index e8309ac302a8..1ceaa78396a2 100644 --- a/homeassistant/components/huawei_lte/strings.json +++ b/homeassistant/components/huawei_lte/strings.json @@ -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": { diff --git a/tests/components/huawei_lte/test_init.py b/tests/components/huawei_lte/test_init.py new file mode 100644 index 000000000000..7d588a6b3dd3 --- /dev/null +++ b/tests/components/huawei_lte/test_init.py @@ -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