From e21baff6dfe88907ea8bfc21464db9c955530880 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Thu, 8 Oct 2026 23:02:26 +0200 Subject: [PATCH] Raise ConfigEntryError on login failure in nuheat (#185101) Co-authored-by: Claude Opus 5.5 --- homeassistant/components/nuheat/__init__.py | 21 ++++++------ homeassistant/components/nuheat/strings.json | 7 +++- tests/components/nuheat/test_init.py | 34 +++++++++++++++++++- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/nuheat/__init__.py b/homeassistant/components/nuheat/__init__.py index 7566e145d6c5..32cbeccd717e 100644 --- a/homeassistant/components/nuheat/__init__.py +++ b/homeassistant/components/nuheat/__init__.py @@ -1,20 +1,17 @@ """Support for NuHeat thermostats.""" from http import HTTPStatus -import logging import nuheat import requests from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady -from .const import CONF_SERIAL_NUMBER, PLATFORMS +from .const import CONF_SERIAL_NUMBER, DOMAIN, PLATFORMS from .coordinator import NuHeatConfigEntry, NuHeatCoordinator -_LOGGER = logging.getLogger(__name__) - def _get_thermostat(api: nuheat.NuHeat, serial_number: str) -> nuheat.NuHeatThermostat: """Authenticate and create the thermostat object.""" @@ -45,12 +42,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: NuHeatConfigEntry) -> bo < ex.response.status_code < HTTPStatus.INTERNAL_SERVER_ERROR ): - _LOGGER.error("Failed to login to nuheat: %s", ex) - return False + raise ConfigEntryError( + translation_domain=DOMAIN, + translation_key="login_failed", + ) from ex raise ConfigEntryNotReady from ex - except Exception as ex: # noqa: BLE001 - _LOGGER.error("Failed to login to nuheat: %s", ex) - return False + except Exception as ex: + raise ConfigEntryError( + translation_domain=DOMAIN, + translation_key="login_failed", + ) from ex entry.runtime_data = NuHeatCoordinator(hass, entry, thermostat) diff --git a/homeassistant/components/nuheat/strings.json b/homeassistant/components/nuheat/strings.json index 9cf3761d9396..3a9ecce61dfb 100644 --- a/homeassistant/components/nuheat/strings.json +++ b/homeassistant/components/nuheat/strings.json @@ -16,9 +16,14 @@ "serial_number": "Serial number of the thermostat.", "username": "[%key:common::config_flow::data::username%]" }, - "description": "You will need to obtain your thermostat\u2019s numeric serial number or ID by logging into {nuheat_url} and selecting your thermostat(s).", + "description": "You will need to obtain your thermostat’s numeric serial number or ID by logging into {nuheat_url} and selecting your thermostat(s).", "title": "Connect to the NuHeat" } } + }, + "exceptions": { + "login_failed": { + "message": "Failed to log in to NuHeat" + } } } diff --git a/tests/components/nuheat/test_init.py b/tests/components/nuheat/test_init.py index 15829935dabf..ebfedb51b531 100644 --- a/tests/components/nuheat/test_init.py +++ b/tests/components/nuheat/test_init.py @@ -1,8 +1,12 @@ """NuHeat component tests.""" -from unittest.mock import patch +from unittest.mock import MagicMock, patch + +import pytest +import requests from homeassistant.components.nuheat.const import DOMAIN +from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from .mocks import MOCK_CONFIG_ENTRY, _get_mock_nuheat @@ -27,3 +31,31 @@ async def test_init_success(hass: HomeAssistant) -> None: config_entry.add_to_hass(hass) assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() + + +@pytest.mark.parametrize( + "exception", + [ + pytest.param( + requests.exceptions.HTTPError(response=MagicMock(status_code=401)), + id="unauthorized", + ), + pytest.param(Exception("Boom"), id="unknown"), + ], +) +async def test_init_login_failed(hass: HomeAssistant, exception: Exception) -> None: + """Test setup fails when logging in fails.""" + mock_nuheat = _get_mock_nuheat() + mock_nuheat.authenticate.side_effect = exception + + with patch( + "homeassistant.components.nuheat.nuheat.NuHeat", + return_value=mock_nuheat, + ): + config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG_ENTRY) + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.SETUP_ERROR + assert config_entry.reason == "Failed to log in to NuHeat"