From a464189ce07b433416f8a5e7fda52bb967dbf05b Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sat, 5 Sep 2020 15:02:52 +0200 Subject: [PATCH 1/7] Fix case when from_state is None, closes #111 Made this commit on my phone and didn't test it yet. --- custom_components/circadian_lighting/switch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index 6887219c..eee3751e 100755 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -382,7 +382,7 @@ class CircadianSwitch(SwitchEntity, RestoreEntity): await asyncio.wait(tasks) async def _light_state_changed(self, entity_id, from_state, to_state): - if to_state.state == "on" and from_state.state != "on": + if to_state.state == "on" and (from_state is None or from_state.state != "on"): _LOGGER.debug(_difference_between_states(from_state, to_state)) await self._force_update_switch(lights=[entity_id]) From 37ac31c3c633ed3027b80554eae96dae6fb88cf9 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sat, 5 Sep 2020 15:08:26 +0200 Subject: [PATCH 2/7] to_state will always be on --- custom_components/circadian_lighting/switch.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index eee3751e..75e122fb 100755 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -382,7 +382,8 @@ class CircadianSwitch(SwitchEntity, RestoreEntity): await asyncio.wait(tasks) async def _light_state_changed(self, entity_id, from_state, to_state): - if to_state.state == "on" and (from_state is None or from_state.state != "on"): + assert to_state.state == "on" + if from_state is None or from_state.state != "on": _LOGGER.debug(_difference_between_states(from_state, to_state)) await self._force_update_switch(lights=[entity_id]) From 618234259d658e4165d25681eaf14131808391e2 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sat, 5 Sep 2020 17:39:19 +0200 Subject: [PATCH 3/7] transition should always exist --- custom_components/circadian_lighting/switch.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index 75e122fb..b77e04e4 100755 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -352,11 +352,9 @@ class CircadianSwitch(SwitchEntity, RestoreEntity): if not is_on(self.hass, light): continue - service_data = {ATTR_ENTITY_ID: light} + service_data = {ATTR_ENTITY_ID: light, ATTR_TRANSITION: transition} if self._brightness is not None: service_data[ATTR_BRIGHTNESS] = int((self._brightness / 100) * 254) - if transition is not None: - service_data[ATTR_TRANSITION] = transition light_type = self._lights_types[light] if light_type == "ct": From 46a74cb5a5aa76c5a9f81c36a8d526ef7ced4baf Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sat, 5 Sep 2020 20:09:56 +0200 Subject: [PATCH 4/7] prepend async_ to turn_on and turn_off --- custom_components/circadian_lighting/switch.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index b77e04e4..54bb5e92 100755 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -268,12 +268,12 @@ class CircadianSwitch(SwitchEntity, RestoreEntity): """Return the attributes of the switch.""" return {"hs_color": self._hs_color, "brightness": self._brightness} - async def turn_on(self, **kwargs): + async def async_turn_on(self, **kwargs): """Turn on circadian lighting.""" self._state = True await self._force_update_switch() - def turn_off(self, **kwargs): + def async_turn_off(self, **kwargs): """Turn off circadian lighting.""" self._state = False self._hs_color = None @@ -306,7 +306,7 @@ class CircadianSwitch(SwitchEntity, RestoreEntity): def _calc_brightness(self) -> float: if self._disable_brightness_adjust: - return None + return if self.is_sleep(): return self._sleep_brightness if self._circadian_lighting._percent > 0: From b381c5a96d873a6bb1cf0f697364f7e4b7114723 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sat, 5 Sep 2020 20:14:51 +0200 Subject: [PATCH 5/7] make is_sleep a private method --- custom_components/circadian_lighting/switch.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index 54bb5e92..43b61a88 100755 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -279,7 +279,7 @@ class CircadianSwitch(SwitchEntity, RestoreEntity): self._hs_color = None self._brightness = None - def is_sleep(self): + def _is_sleep(self): return ( self._sleep_entity is not None and self.hass.states.get(self._sleep_entity).state in self._sleep_state @@ -288,7 +288,7 @@ class CircadianSwitch(SwitchEntity, RestoreEntity): def _color_temperature(self): return ( self._circadian_lighting._colortemp - if not self.is_sleep() + if not self._is_sleep() else self._sleep_colortemp ) @@ -307,7 +307,7 @@ class CircadianSwitch(SwitchEntity, RestoreEntity): def _calc_brightness(self) -> float: if self._disable_brightness_adjust: return - if self.is_sleep(): + if self._is_sleep(): return self._sleep_brightness if self._circadian_lighting._percent > 0: return self._max_brightness From 3b868858b006f53a06e4e15dbf517fdd4779c8dc Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sat, 5 Sep 2020 20:16:15 +0200 Subject: [PATCH 6/7] make async_turn_off actually async --- custom_components/circadian_lighting/switch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index 43b61a88..3ded81a2 100755 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -273,7 +273,7 @@ class CircadianSwitch(SwitchEntity, RestoreEntity): self._state = True await self._force_update_switch() - def async_turn_off(self, **kwargs): + async def async_turn_off(self, **kwargs): """Turn off circadian lighting.""" self._state = False self._hs_color = None From 7744c2e939376c99448d37d61182ad4e9c48c925 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sat, 5 Sep 2020 20:17:14 +0200 Subject: [PATCH 7/7] decapitalize "Both" -> "both" --- custom_components/circadian_lighting/switch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index 3ded81a2..7c5b6cc5 100755 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -127,7 +127,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): 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" + 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: