From ae4a7c97f45a0dc1ca254cfdb4b0a2e40d1255b6 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Thu, 8 Oct 2026 22:18:47 +0200 Subject: [PATCH] Raise ConfigEntryError instead of returning False in hue setup (#185053) Co-authored-by: Claude Opus 5.5 --- homeassistant/components/hue/__init__.py | 13 +++++- homeassistant/components/hue/strings.json | 8 ++++ tests/components/hue/test_init.py | 48 +++++++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/hue/__init__.py b/homeassistant/components/hue/__init__.py index a840ced7d57c..15f4b0bb5066 100644 --- a/homeassistant/components/hue/__init__.py +++ b/homeassistant/components/hue/__init__.py @@ -3,7 +3,9 @@ from aiohue.util import normalize_bridge_id from homeassistant.config_entries import SOURCE_IGNORE +from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant, callback +from homeassistant.exceptions import ConfigEntryError from homeassistant.helpers import config_validation as cv, device_registry as dr from homeassistant.helpers.typing import ConfigType @@ -59,7 +61,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: HueConfigEntry) -> bool: # setup the bridge instance bridge = HueBridge(hass, entry) if not await bridge.async_initialize_bridge(): - return False + raise ConfigEntryError( + translation_domain=DOMAIN, + translation_key="bridge_initialization_failed", + translation_placeholders={"host": entry.data[CONF_HOST]}, + ) api = bridge.api @@ -95,7 +101,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: HueConfigEntry) -> bool: # There is another entry that already has the right unique # ID. Delete this entry hass.async_create_task(hass.config_entries.async_remove(entry.entry_id)) - return False + raise ConfigEntryError( + translation_domain=DOMAIN, + translation_key="duplicate_entry", + ) # v1 bridges already register their device before platform forwarding, so # light/sensor entities can resolve it as their via_device parent; only diff --git a/homeassistant/components/hue/strings.json b/homeassistant/components/hue/strings.json index 9d5f82352c5a..61125ef28179 100644 --- a/homeassistant/components/hue/strings.json +++ b/homeassistant/components/hue/strings.json @@ -158,6 +158,14 @@ } } }, + "exceptions": { + "bridge_initialization_failed": { + "message": "Failed to set up the Hue bridge at {host}" + }, + "duplicate_entry": { + "message": "Another entry already exists for this Hue bridge, this entry will be removed" + } + }, "issues": { "deprecated_effect_none": { "description": "A light was turned on with the deprecated effect `None`. This has been replaced with `off`. Please update any automations, scenes, or scripts that use this effect.", diff --git a/tests/components/hue/test_init.py b/tests/components/hue/test_init.py index fcd19a0d0335..1fcc6cd5c422 100644 --- a/tests/components/hue/test_init.py +++ b/tests/components/hue/test_init.py @@ -152,6 +152,54 @@ async def test_fixing_unique_id_other_correct( assert hass.config_entries.async_entries() == [correct_entry] +async def test_fixing_unique_id_other_correct_setup_error( + hass: HomeAssistant, mock_bridge_setup +) -> None: + """Test setup error reason when another entry has the correct ID.""" + MockConfigEntry( + domain=hue.DOMAIN, + data={"host": "0.0.0.0", "api_version": 2}, + unique_id="mock-id", + minor_version=2, + ).add_to_hass(hass) + entry = MockConfigEntry( + domain=hue.DOMAIN, + data={"host": "0.0.0.0", "api_version": 2}, + unique_id="invalid-id", + minor_version=2, + ) + entry.add_to_hass(hass) + + with patch.object(hass.config_entries, "async_remove", AsyncMock()) as mock_remove: + assert await async_setup_component(hass, hue.DOMAIN, {}) is True + await hass.async_block_till_done() + + assert entry.state is config_entries.ConfigEntryState.SETUP_ERROR + assert entry.reason == ( + "Another entry already exists for this Hue bridge, this entry will be removed" + ) + mock_remove.assert_awaited_once_with(entry.entry_id) + + +async def test_bridge_initialization_failed( + hass: HomeAssistant, mock_bridge_setup +) -> None: + """Test setup fails when the bridge can't be initialized.""" + mock_bridge_setup.async_initialize_bridge.return_value = False + entry = MockConfigEntry( + domain=hue.DOMAIN, + data={"host": "0.0.0.0", "api_version": 2}, + minor_version=2, + ) + entry.add_to_hass(hass) + + assert await async_setup_component(hass, hue.DOMAIN, {}) is True + await hass.async_block_till_done() + + assert entry.state is config_entries.ConfigEntryState.SETUP_ERROR + assert entry.reason == "Failed to set up the Hue bridge at 0.0.0.0" + + async def test_security_vuln_check(hass: HomeAssistant) -> None: """Test that we report security vulnerabilities.""" entry = MockConfigEntry(