From 6ea4ca57df567067db2dcbe616a2cf0a08ddaf93 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Thu, 8 Oct 2026 22:27:10 +0200 Subject: [PATCH] Raise ConfigEntryError when hub is not primary in hunterdouglas_powerview (#185066) Co-authored-by: Claude Opus 5.5 --- .../hunterdouglas_powerview/__init__.py | 17 +++++----- .../hunterdouglas_powerview/strings.json | 5 +++ .../hunterdouglas_powerview/test_init.py | 32 +++++++++++++++++++ 3 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 tests/components/hunterdouglas_powerview/test_init.py diff --git a/homeassistant/components/hunterdouglas_powerview/__init__.py b/homeassistant/components/hunterdouglas_powerview/__init__.py index 22d686922aca..703e3899e726 100644 --- a/homeassistant/components/hunterdouglas_powerview/__init__.py +++ b/homeassistant/components/hunterdouglas_powerview/__init__.py @@ -11,7 +11,7 @@ from aiopvapi.shades import Shades from homeassistant.const import CONF_API_VERSION, CONF_HOST, Platform from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady from homeassistant.helpers import device_registry as dr, entity_registry as er from .const import DOMAIN, HUB_EXCEPTIONS, MANUFACTURER @@ -55,14 +55,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: PowerviewConfigEntry) -> if hub.role != "Primary": # this should be caught in config_flow, but account for a hub changing roles # this will only happen manually by a user - _LOGGER.error( - "%s (%s) is performing role of %s Hub. " - "Only the Primary Hub can manage shades", - hub.name, - hub.hub_address, - hub.role, + raise ConfigEntryError( + translation_domain=DOMAIN, + translation_key="not_primary_hub", + translation_placeholders={ + "name": hub.name, + "address": hub.hub_address, + "role": hub.role, + }, ) - return False # manual registration of the hub device_registry = dr.async_get(hass) diff --git a/homeassistant/components/hunterdouglas_powerview/strings.json b/homeassistant/components/hunterdouglas_powerview/strings.json index a56b179cdcf0..9314d567d748 100644 --- a/homeassistant/components/hunterdouglas_powerview/strings.json +++ b/homeassistant/components/hunterdouglas_powerview/strings.json @@ -62,5 +62,10 @@ "name": "[%key:component::sensor::entity_component::signal_strength::name%]" } } + }, + "exceptions": { + "not_primary_hub": { + "message": "{name} ({address}) is performing role of {role} Hub. Only the Primary Hub can manage shades" + } } } diff --git a/tests/components/hunterdouglas_powerview/test_init.py b/tests/components/hunterdouglas_powerview/test_init.py new file mode 100644 index 000000000000..cd9dbf58c163 --- /dev/null +++ b/tests/components/hunterdouglas_powerview/test_init.py @@ -0,0 +1,32 @@ +"""Tests for the Hunter Douglas PowerView integration setup.""" + +from unittest.mock import AsyncMock, MagicMock, patch + +from homeassistant.components.hunterdouglas_powerview.const import DOMAIN +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant + +from .const import MOCK_MAC + +from tests.common import MockConfigEntry + + +async def test_setup_not_primary_hub(hass: HomeAssistant) -> None: + """Test setup fails when the hub is not the primary hub.""" + entry = MockConfigEntry(domain=DOMAIN, data={"host": "1.2.3.4"}, unique_id=MOCK_MAC) + entry.add_to_hass(hass) + hub = MagicMock(hub_address="1.2.3.4", role="Secondary") + hub.name = "PowerView Hub" + + with patch( + "homeassistant.components.hunterdouglas_powerview.async_connect_hub", + AsyncMock(return_value=MagicMock(hub=hub)), + ): + 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 == ( + "PowerView Hub (1.2.3.4) is performing role of Secondary Hub. Only the" + " Primary Hub can manage shades" + )