From 38dc367c565c5f738941db4351162be6ff7f26e8 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Tue, 25 Aug 2020 18:39:13 +0200 Subject: [PATCH] simplify adjust_lights even more --- .../circadian_lighting/switch.py | 48 +++++++------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index 6cf89b52..474e2ed1 100644 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -357,64 +357,50 @@ class CircadianSwitch(SwitchEntity, RestoreEntity): if not is_on(self.hass, light): continue + which = None + service_data = {ATTR_ENTITY_ID: light} + if transition is not None: + service_data[ATTR_TRANSITION] = transition + # Set color of array of ct. if self._lights_ct is not None and light in self._lights_ct: - service_data = {ATTR_ENTITY_ID: light} + which = "CT" if mired is not None: service_data[ATTR_COLOR_TEMP] = mired if brightness is not None: service_data[ATTR_BRIGHTNESS] = brightness - if transition is not None: - service_data[ATTR_TRANSITION] = transition - self.hass.services.call(LIGHT_DOMAIN, SERVICE_TURN_ON, service_data) - _LOGGER.debug( - f"{light} CT Adjusted - color_temp: {mired}, " - f"brightness: {brightness}, transition: {transition}" - ) # Set color of array of rgb. elif self._lights_rgb is not None and light in self._lights_rgb: - service_data = {ATTR_ENTITY_ID: light} + which = "RGB" if rgb is not None: service_data[ATTR_RGB_COLOR] = rgb if brightness is not None: service_data[ATTR_BRIGHTNESS] = brightness - if transition is not None: - service_data[ATTR_TRANSITION] = transition - self.hass.services.call(LIGHT_DOMAIN, SERVICE_TURN_ON, service_data) - _LOGGER.debug( - f"{light} RGB Adjusted - rgb_color: {rgb}, " - f"brightness: {brightness}, transition: {transition}" - ) # Set color of array of xy. elif self._lights_xy is not None and light in self._lights_xy: - service_data = {ATTR_ENTITY_ID: light} + which = "XY" if xy is not None: service_data[ATTR_XY_COLOR] = xy if brightness is not None: service_data[ATTR_BRIGHTNESS] = brightness service_data[ATTR_WHITE_VALUE] = brightness - if transition is not None: - service_data[ATTR_TRANSITION] = transition - self.hass.services.call(LIGHT_DOMAIN, SERVICE_TURN_ON, service_data) - _LOGGER.debug( - f"{light} XY Adjusted - xy_color: {xy}, brightness: {brightness}, " - f"transition: {transition}, white_value: {brightness}" - ) # Set color of array of brightness. - elif self._lights_brightness is not None and light in self._lights_brightness: - service_data = {ATTR_ENTITY_ID: light} + elif ( + self._lights_brightness is not None and light in self._lights_brightness + ): + which = "Brightness" if brightness is not None: service_data[ATTR_BRIGHTNESS] = brightness - if transition is not None: - service_data[ATTR_TRANSITION] = transition + + if which is not None: self.hass.services.call(LIGHT_DOMAIN, SERVICE_TURN_ON, service_data) - _LOGGER.debug( - f"{light} Brightness Adjusted - brightness: {brightness}, " - f"transition: {transition}" + msg = ", ".join( + [f"{k}: v" for k, v in d.items() if k != ATTR_ENTITY_ID] ) + _LOGGER.debug(f"{light} {which} Adjusted - {msg}") def light_state_changed(self, entity_id, from_state, to_state): with suppress(Exception):