From fd5f6bf3106aa05cbcb94169ad0cdc3759534f9f Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Thu, 20 Jul 2023 11:53:38 -0700 Subject: [PATCH] Pass on lights that are not managed by AL, closes #638 (#639) * Pass on lights that are not managed by AL, closes #638 * Rename exception --- custom_components/adaptive_lighting/manifest.json | 2 +- custom_components/adaptive_lighting/switch.py | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/custom_components/adaptive_lighting/manifest.json b/custom_components/adaptive_lighting/manifest.json index 40131076..52f0554c 100644 --- a/custom_components/adaptive_lighting/manifest.json +++ b/custom_components/adaptive_lighting/manifest.json @@ -8,5 +8,5 @@ "iot_class": "calculated", "issue_tracker": "https://github.com/basnijholt/adaptive-lighting/issues", "requirements": ["ulid-transform"], - "version": "1.15.1" + "version": "1.15.2" } diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index edbc8214..4cbd6854 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -305,6 +305,10 @@ def _get_switches_with_lights( return switches +class NoSwitchFoundError(ValueError): + """No switches found for lights.""" + + def find_switch_for_lights( hass: HomeAssistant, lights: list[str], @@ -318,13 +322,13 @@ def find_switch_for_lights( if len(on_switches) == 1: # Of the multiple switches, only one is on return on_switches[0] - raise ValueError( + raise NoSwitchFoundError( f"find_switch_for_lights: Light(s) {lights} found in multiple switch configs" f" ({[s.entity_id for s in switches]}). You must pass a switch under" f" 'entity_id'." ) else: - raise ValueError( + raise NoSwitchFoundError( f"find_switch_for_lights: Light(s) {lights} not found in any switch's" f" configuration. You must either include the light(s) that is/are" f" in the integration config, or pass a switch under 'entity_id'." @@ -1840,7 +1844,12 @@ async def _service_interceptor_turn_on_handler( return entity_id = entity_ids[0] - adaptive_switch = find_switch_for_lights(self.hass, [entity_id]) + try: + adaptive_switch = find_switch_for_lights(self.hass, [entity_id]) + except NoSwitchFoundError: + # This might be a light that is not managed by this AL instance. + return + if entity_id not in adaptive_switch.lights: return