Raise ConfigEntryError instead of returning False in hue setup (#185053)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
G Johansson
2026-10-08 22:18:47 +02:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 49c3deb2e8
commit ae4a7c97f4
3 changed files with 67 additions and 2 deletions
+11 -2
View File
@@ -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
@@ -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.",
+48
View File
@@ -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(