From fee90250eef694d62e9fbafbc1b07d53741e9e82 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sat, 22 Jul 2023 12:34:20 -0700 Subject: [PATCH] Better logging and prevent KeyError in state_changed_event_listener (#650) * Better logging * Remove useless and erroneous logging statement * Bump to 1.16.3 --- .../adaptive_lighting/manifest.json | 2 +- custom_components/adaptive_lighting/switch.py | 24 +++++++------------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/custom_components/adaptive_lighting/manifest.json b/custom_components/adaptive_lighting/manifest.json index 5e121064..fcf644cd 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.16.2" + "version": "1.16.3" } diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 0e0da07f..fbf6c79c 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -1212,10 +1212,8 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): async def _execute_adaptation_calls(self, data: AdaptationData): """Executes a sequence of adaptation service calls for the given service datas.""" - index = 0 - while True: + for index in range(data.max_length): is_first_call = index == 0 - index += 1 # Sleep between multiple service calls. if not is_first_call or data.initial_sleep: @@ -1257,13 +1255,9 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): listener = self.turn_on_off_listener listener.cancel_ongoing_adaptation_calls(data.entity_id, which=data.which) _LOGGER.debug( - "%s: execute_cancellable_adaptation_calls with data: %s" - "adaptation_tasks_brightness: %s" - "adaptation_tasks_color: %s", + "%s: execute_cancellable_adaptation_calls with data: %s", self._name, data, - listener.adaptation_tasks_brightness, - listener.adaptation_tasks_color, ) # Execute adaptation calls within a task try: @@ -1290,9 +1284,13 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): ) -> None: assert context is not None _LOGGER.debug( - "%s: '_update_attrs_and_maybe_adapt_lights' called with context.id='%s'", + "%s: '_update_attrs_and_maybe_adapt_lights' called with context.id='%s'" + " lights: '%s', transition: '%s', force: '%s'", self._name, context.id, + lights, + transition, + force, ) assert self.is_on self._settings.update( @@ -1919,8 +1917,8 @@ class TurnOnOffListener: adapt_brightness, adapt_color, ) - # if adaptation_data is None: - # return + if adaptation_data is None: + return # Take first adaptation item to apply it to this service call first_service_data = await adaptation_data.next_service_call_data() @@ -2193,10 +2191,6 @@ class TurnOnOffListener: entity_id, ) self.last_state_change[entity_id] = [new_state] - _LOGGER.debug( - "Last transition: %s", - self.last_service_data[entity_id].get(ATTR_TRANSITION), - ) self.start_transition_timer(entity_id) elif old_state is not None: self.last_state_change[entity_id].append(new_state)