From 40568296ed9e26b5a75caba701145e41292eb257 Mon Sep 17 00:00:00 2001 From: Clayton Nummer Date: Tue, 30 Jul 2019 17:15:46 -0400 Subject: [PATCH] Slight efficiency tweaks Don't calculate values for every light, and only set service_data if it's going to be used --- .../circadian_lighting/switch.py | 94 +++++++++---------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index 759eaa91..8264fc49 100644 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -268,12 +268,19 @@ class CircadianSwitch(SwitchDevice, RestoreEntity): transition = self._cl.data['transition'] brightness = int((self._attributes['brightness'] / 100) * 255) if self._attributes['brightness'] is not None else None + mired = int(self.calc_ct()) if self._lights_ct is not None else None + rgb = tuple(map(int, self.calc_rgb())) if self._lights_rgb is not None else None + xy = self.calc_xy() if self._lights_xy is not None else None for light in lights: - """Set color of array of ct light.""" - if self._lights_ct is not None and light in self._lights_ct: - mired = int(self.calc_ct()) - if is_on(self.hass, light): + """Set color of array of ct light if on.""" + if self._lights_ct is not None and light in self._lights_ct and is_on(self.hass, light): + """Check to see if light is already set properly""" + lightAttrs = self.hass.states.get(light).attributes + if ( (ATTR_COLOR_TEMP in lightAttrs and lightAttrs[ATTR_COLOR_TEMP] == mired) and + (ATTR_BRIGHTNESS in lightAttrs and lightAttrs[ATTR_BRIGHTNESS] == brightness) ): + _LOGGER.debug(light + " already set to the proper values, not adjusting") + else: service_data = {ATTR_ENTITY_ID: light} if mired is not None: service_data[ATTR_COLOR_TEMP] = mired @@ -281,19 +288,18 @@ class CircadianSwitch(SwitchDevice, RestoreEntity): service_data[ATTR_BRIGHTNESS] = brightness if transition is not None: service_data[ATTR_TRANSITION] = transition - lightAttrs = self.hass.states.get(light).attributes - if ( (ATTR_COLOR_TEMP in lightAttrs and lightAttrs[ATTR_COLOR_TEMP] == mired) and - (ATTR_BRIGHTNESS in lightAttrs and lightAttrs[ATTR_BRIGHTNESS] == brightness) ): - _LOGGER.debug(light + " already set to the proper values, not adjusting") - else: - self.hass.services.call( - LIGHT_DOMAIN, SERVICE_TURN_ON, service_data) - _LOGGER.debug(light + " CT Adjusted - color_temp: " + str(mired) + ", brightness: " + str(brightness) + ", transition: " + str(transition)) + self.hass.services.call( + LIGHT_DOMAIN, SERVICE_TURN_ON, service_data) + _LOGGER.debug(light + " CT Adjusted - color_temp: " + str(mired) + ", brightness: " + str(brightness) + ", transition: " + str(transition)) - """Set color of array of rgb light.""" - if self._lights_rgb is not None and light in self._lights_rgb: - rgb = tuple(map(int, self.calc_rgb())) - if is_on(self.hass, light): + """Set color of array of rgb light if on.""" + if self._lights_rgb is not None and light in self._lights_rgb and is_on(self.hass, light): + """Check to see if light is already set properly""" + lightAttrs = self.hass.states.get(light).attributes + if ( (ATTR_RGB_COLOR in lightAttrs and lightAttrs[ATTR_RGB_COLOR] == rgb) and + (ATTR_BRIGHTNESS in lightAttrs and lightAttrs[ATTR_BRIGHTNESS] == brightness) ): + _LOGGER.debug(light + " already set to the proper values, not adjusting") + else: service_data = {ATTR_ENTITY_ID: light} if rgb is not None: service_data[ATTR_RGB_COLOR] = rgb @@ -301,19 +307,19 @@ class CircadianSwitch(SwitchDevice, RestoreEntity): service_data[ATTR_BRIGHTNESS] = brightness if transition is not None: service_data[ATTR_TRANSITION] = transition - lightAttrs = self.hass.states.get(light).attributes - if ( (ATTR_RGB_COLOR in lightAttrs and lightAttrs[ATTR_RGB_COLOR] == rgb) and - (ATTR_BRIGHTNESS in lightAttrs and lightAttrs[ATTR_BRIGHTNESS] == brightness) ): - _LOGGER.debug(light + " already set to the proper values, not adjusting") - else: - self.hass.services.call( - LIGHT_DOMAIN, SERVICE_TURN_ON, service_data) - _LOGGER.debug(light + " RGB Adjusted - rgb_color: " + str(rgb) + ", brightness: " + str(brightness) + ", transition: " + str(transition)) + self.hass.services.call( + LIGHT_DOMAIN, SERVICE_TURN_ON, service_data) + _LOGGER.debug(light + " RGB Adjusted - rgb_color: " + str(rgb) + ", brightness: " + str(brightness) + ", transition: " + str(transition)) - """Set color of array of xy light.""" - if self._lights_xy is not None and light in self._lights_xy: - xy = self.calc_xy() - if is_on(self.hass, light): + """Set color of array of xy light if on.""" + if self._lights_xy is not None and light in self._lights_xy and is_on(self.hass, light): + """Check to see if light is already set properly""" + lightAttrs = self.hass.states.get(light).attributes + if ( (ATTR_XY_COLOR in lightAttrs and lightAttrs[ATTR_XY_COLOR] == xy) and + (ATTR_BRIGHTNESS in lightAttrs and lightAttrs[ATTR_BRIGHTNESS] == brightness) and + (ATTR_WHITE_VALUE in lightAttrs and lightAttrs[ATTR_WHITE_VALUE] == brightness) ): + _LOGGER.debug(light + " already set to the proper values, not adjusting") + else: service_data = {ATTR_ENTITY_ID: light} if xy is not None: service_data[ATTR_XY_COLOR] = xy @@ -322,31 +328,25 @@ class CircadianSwitch(SwitchDevice, RestoreEntity): service_data[ATTR_WHITE_VALUE] = brightness if transition is not None: service_data[ATTR_TRANSITION] = transition - lightAttrs = self.hass.states.get(light).attributes - if ( (ATTR_XY_COLOR in lightAttrs and lightAttrs[ATTR_XY_COLOR] == xy) and - (ATTR_BRIGHTNESS in lightAttrs and lightAttrs[ATTR_BRIGHTNESS] == brightness) and - (ATTR_WHITE_VALUE in lightAttrs and lightAttrs[ATTR_WHITE_VALUE] == brightness) ): - _LOGGER.debug(light + " already set to the proper values, not adjusting") - else: - self.hass.services.call( - LIGHT_DOMAIN, SERVICE_TURN_ON, service_data) - _LOGGER.debug(light + " XY Adjusted - xy_color: " + str(xy) + ", brightness: " + str(brightness) + ", transition: " + str(transition) + ", white_value: " + str(brightness)) + self.hass.services.call( + LIGHT_DOMAIN, SERVICE_TURN_ON, service_data) + _LOGGER.debug(light + " XY Adjusted - xy_color: " + str(xy) + ", brightness: " + str(brightness) + ", transition: " + str(transition) + ", white_value: " + str(brightness)) - """Set color of array of brightness light.""" - if self._lights_brightness is not None and light in self._lights_brightness: - if is_on(self.hass, light): + """Set color of array of brightness light if on.""" + if self._lights_brightness is not None and light in self._lights_brightness and is_on(self.hass, light): + """Check to see if light is already set properly""" + lightAttrs = self.hass.states.get(light).attributes + if (ATTR_BRIGHTNESS in lightAttrs and lightAttrs[ATTR_BRIGHTNESS] == brightness): + _LOGGER.debug(light + " already set to the proper values, not adjusting") + else: service_data = {ATTR_ENTITY_ID: light} if brightness is not None: service_data[ATTR_BRIGHTNESS] = brightness if transition is not None: service_data[ATTR_TRANSITION] = transition - lightAttrs = self.hass.states.get(light).attributes - if (ATTR_BRIGHTNESS in lightAttrs and lightAttrs[ATTR_BRIGHTNESS] == brightness): - _LOGGER.debug(light + " already set to the proper values, not adjusting") - else: - self.hass.services.call( - LIGHT_DOMAIN, SERVICE_TURN_ON, service_data) - _LOGGER.debug(light + " Brightness Adjusted - brightness: " + str(brightness) + ", transition: " + str(transition)) + self.hass.services.call( + LIGHT_DOMAIN, SERVICE_TURN_ON, service_data) + _LOGGER.debug(light + " Brightness Adjusted - brightness: " + str(brightness) + ", transition: " + str(transition)) def light_state_changed(self, entity_id, from_state, to_state): self.adjust_lights([entity_id], 1)