From 8ec62305f010f68ae84353fd7c7a85c16d5c6e21 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Thu, 1 Oct 2020 10:09:06 +0200 Subject: [PATCH] exception --- custom_components/adaptive_lighting/switch.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 1a8a3eed..c6f9fe37 100755 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -40,7 +40,7 @@ from homeassistant.const import ( SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET, ) -from homeassistant.core import Context, Event, ServiceCall +from homeassistant.core import Context, Event, ServiceCall, callback from homeassistant.helpers import entity_platform import homeassistant.helpers.config_validation as cv from homeassistant.helpers.event import ( @@ -393,7 +393,8 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): self._state = False self._remove_listeners() - async def _update_attrs(self): + @callback + def _update_attrs(self): """Update Adaptive Values.""" # Setting all values because this method takes <0.5ms to execute. self._percent = self._calc_percent() @@ -418,7 +419,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): force: bool = False, context: Optional[Context] = None, ): - await self._update_attrs() + self._update_attrs() if self._only_once and not force: return await self._adapt_lights(lights or self._lights, transition, context) @@ -464,7 +465,15 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): # Check whether order is correct events = sorted(events, key=lambda x: x[1]) events_names, _ = zip(*events) - assert events_names in _ALLOWED_ORDERS, events_names + if events_names not in _ALLOWED_ORDERS: + msg = ( + f"{self._name}: The sun events {events_names} are not in the expected" + " order. The Adaptive Lighting integration will not work!" + " This might happen if your sunrise/sunset offset is too large or" + " your manually set sunrise/sunset time is past/before noon/midnight." + ) + _LOGGER.error(msg) + raise ValueError(msg) return events