Only react to light state change to 'on'

And only when light isn't already on. This should allow for users to disable CL when they adjust a light from the frontend, without CL immediately readjusting. This also should remove the need for checking state of each light before adjusting, because infinite loops should no longer trigger.
This commit is contained in:
Clayton Nummer 2019-07-31 14:53:34 -04:00
commit 7185fe3feb
2 changed files with 44 additions and 65 deletions

View file

@ -48,7 +48,7 @@ from homeassistant.util.dt import utcnow as dt_utcnow, as_local
from datetime import datetime, timedelta
VERSION = '1.0.7'
VERSION = '1.0.8'
_LOGGER = logging.getLogger(__name__)

View file

@ -275,82 +275,61 @@ class CircadianSwitch(SwitchDevice, RestoreEntity):
for light in lights:
"""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
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(light + " CT Adjusted - color_temp: " + str(mired) + ", brightness: " + str(brightness) + ", transition: " + str(transition))
service_data = {ATTR_ENTITY_ID: light}
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(light + " CT Adjusted - color_temp: " + str(mired) + ", brightness: " + str(brightness) + ", transition: " + str(transition))
"""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
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(light + " RGB Adjusted - rgb_color: " + str(rgb) + ", brightness: " + str(brightness) + ", transition: " + str(transition))
service_data = {ATTR_ENTITY_ID: light}
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(light + " RGB Adjusted - rgb_color: " + str(rgb) + ", brightness: " + str(brightness) + ", transition: " + str(transition))
"""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
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(light + " XY Adjusted - xy_color: " + str(xy) + ", brightness: " + str(brightness) + ", transition: " + str(transition) + ", white_value: " + str(brightness))
service_data = {ATTR_ENTITY_ID: light}
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(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 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
self.hass.services.call(
LIGHT_DOMAIN, SERVICE_TURN_ON, service_data)
_LOGGER.debug(light + " Brightness Adjusted - brightness: " + str(brightness) + ", transition: " + str(transition))
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
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)
_LOGGER.debug(entity_id + " change from " + str(from_state) + " to " + str(to_state))
if to_state.state == 'on' and from_state.state != 'on':
self.adjust_lights([entity_id], 1)
def sleep_state_changed(self, entity_id, from_state, to_state):
_LOGGER.debug(entity_id + " change from " + str(from_state) + " to " + str(to_state))
if to_state.state == self._sleep_state or from_state.state == self._sleep_state:
self.update_switch(1)