Raise ConfigEntryError on login failure in nuheat (#185101)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
G Johansson
2026-10-08 23:02:26 +02:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 0077665d71
commit e21baff6df
3 changed files with 50 additions and 12 deletions
+11 -10
View File
@@ -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)
+6 -1
View File
@@ -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"
}
}
}
+33 -1
View File
@@ -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"