From 0264aeaa09af34c05c4e1a918330bb91344c5f82 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Fri, 4 Sep 2020 16:18:45 +0200 Subject: [PATCH] add a more useful debug message for state changing --- .../circadian_lighting/switch.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index 879fdf04..9c0650f9 100755 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -124,6 +124,39 @@ def setup_platform(hass, config, add_devices, discovery_info=None): return False +def _difference_between_states(from_state, to_state): + start = "Lights adjusting because " + if from_state is None and to_state is None: + return start + "Both states None" + if from_state is None: + return start + f"from_state: None, to_state: {to_state}" + if to_state is None: + return start + f"from_state: {from_state}, to_state: None" + + changed_attrs = ", ".join( + [ + f"{key}: {val}" + for key, val in to_state.attributes.items() + if from_state.attributes.get(key) != val + ] + ) + if from_state.state == to_state.state: + return start + ( + f"{from_state.entity_id} is still {to_state.state} but" + f" these attributes changes: {changed_attrs}." + ) + elif changed_attrs != "": + return start + ( + f"{from_state.entity_id} changed from {from_state.state} to" + f" {to_state.state} and these attributes changes: {changed_attrs}." + ) + else: + return start + ( + f"{from_state.entity_id} changed from {from_state.state} to" + f" {to_state.state} and no attributes changed." + ) + + class CircadianSwitch(SwitchEntity, RestoreEntity): """Representation of a Circadian Lighting switch.""" @@ -346,7 +379,9 @@ class CircadianSwitch(SwitchEntity, RestoreEntity): async def _light_state_changed(self, entity_id, from_state, to_state): if to_state.state == "on" and from_state.state != "on": + _LOGGER.debug(_difference_between_states(from_state, to_state)) await self._force_update_switch(lights=[entity_id]) async def _state_changed(self, entity_id, from_state, to_state): + _LOGGER.debug(_difference_between_states(from_state, to_state)) await self._force_update_switch()