From 9a2943db5060fb3c2ac19f7e1283dae0dbf40fca Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Tue, 11 Apr 2023 19:45:40 -0500 Subject: [PATCH] Revert a888afd6dcdfd335a22e45f8b6be0bbd959ebd87 --- custom_components/adaptive_lighting/const.py | 2 - custom_components/adaptive_lighting/switch.py | 95 ++++++------------- 2 files changed, 31 insertions(+), 66 deletions(-) diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index 6f73f86d..6f300b52 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -334,8 +334,6 @@ VALIDATION_TUPLES = [ ), ] -CONST_COLOR = "color" - def timedelta_as_int(value): """Convert a `datetime.timedelta` object to an integer. diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 64831526..d0b1492f 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -23,11 +23,7 @@ from homeassistant.components.light import ( ATTR_COLOR_NAME, ATTR_COLOR_TEMP_KELVIN, ATTR_HS_COLOR, - ATTR_MAX_COLOR_TEMP_KELVIN, - ATTR_MIN_COLOR_TEMP_KELVIN, ATTR_RGB_COLOR, - ATTR_RGBW_COLOR, - ATTR_RGBWW_COLOR, ATTR_SUPPORTED_COLOR_MODES, ATTR_TRANSITION, ATTR_XY_COLOR, @@ -36,7 +32,6 @@ from homeassistant.components.light import ( COLOR_MODE_HS, COLOR_MODE_RGB, COLOR_MODE_RGBW, - COLOR_MODE_RGBWW, COLOR_MODE_XY, ) from homeassistant.components.light import ( @@ -141,7 +136,6 @@ from .const import ( CONF_USE_DEFAULTS, CONF_WATCHED_LIGHTS, CONF_WHICH_SWITCH, - CONST_COLOR, DOMAIN, EXTRA_VALIDATION, ICON_BRIGHTNESS, @@ -170,16 +164,6 @@ _SUPPORT_OPTS = { "transition": SUPPORT_TRANSITION, } -VALID_COLOR_MODES = { - COLOR_MODE_BRIGHTNESS: ATTR_BRIGHTNESS, - COLOR_MODE_COLOR_TEMP: ATTR_COLOR_TEMP_KELVIN, - COLOR_MODE_HS: ATTR_HS_COLOR, - COLOR_MODE_RGB: ATTR_RGB_COLOR, - COLOR_MODE_RGBW: ATTR_RGBW_COLOR, - COLOR_MODE_RGBWW: ATTR_RGBWW_COLOR, - COLOR_MODE_XY: ATTR_XY_COLOR, -} - _ORDER = (SUN_EVENT_SUNRISE, SUN_EVENT_NOON, SUN_EVENT_SUNSET, SUN_EVENT_MIDNIGHT) _ALLOWED_ORDERS = {_ORDER[i:] + _ORDER[:i] for i in range(len(_ORDER))} @@ -746,51 +730,33 @@ def _expand_light_groups(hass: HomeAssistant, lights: list[str]) -> list[str]: return list(all_lights) -def _supported_to_attributes(supported): - supported_attributes = {} - supports_colors = False - for mode, attr in VALID_COLOR_MODES.items(): - if mode not in supported: - continue - supported_attributes[attr] = True - if ( - not supports_colors - and mode != COLOR_MODE_BRIGHTNESS - and mode != COLOR_MODE_COLOR_TEMP - ): - supports_colors = True - return supported_attributes, supports_colors - - def _supported_features(hass: HomeAssistant, light: str): state = hass.states.get(light) - legacy_supported_features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0) - legacy_supported = { - key for key, value in _SUPPORT_OPTS.items() if legacy_supported_features & value + supported_features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0) + supported = { + key for key, value in _SUPPORT_OPTS.items() if supported_features & value } supported_color_modes = state.attributes.get(ATTR_SUPPORTED_COLOR_MODES, set()) - supported, supports_colors = _supported_to_attributes( - legacy_supported.union(supported_color_modes) - ) - min_kelvin = state.attributes.get(ATTR_MIN_COLOR_TEMP_KELVIN) - max_kelvin = state.attributes.get(ATTR_MAX_COLOR_TEMP_KELVIN) - supported.update( - { - ATTR_MIN_COLOR_TEMP_KELVIN: min_kelvin, - ATTR_MAX_COLOR_TEMP_KELVIN: max_kelvin, - } - ) - if supports_colors: + if COLOR_MODE_RGB in supported_color_modes: + supported.add("color") # Adding brightness here, see # comment https://github.com/basnijholt/adaptive-lighting/issues/112#issuecomment-836944011 - supported[ATTR_BRIGHTNESS] = True - if CONST_COLOR not in legacy_supported: - # supports_colors = False - _LOGGER.debug( - "'supported_color_modes' supports color but the legacy 'supported_features'" - " bitfield says we do not. Despite this we'll assume light '%s' supports colors", - ) - return supported, supports_colors + supported.add("brightness") + if COLOR_MODE_RGBW in supported_color_modes: + supported.add("color") + supported.add("brightness") # see above url + if COLOR_MODE_XY in supported_color_modes: + supported.add("color") + supported.add("brightness") # see above url + if COLOR_MODE_HS in supported_color_modes: + supported.add("color") + supported.add("brightness") # see above url + if COLOR_MODE_COLOR_TEMP in supported_color_modes: + supported.add("color_temp") + supported.add("brightness") # see above url + if COLOR_MODE_BRIGHTNESS in supported_color_modes: + supported.add("brightness") + return supported def color_difference_redmean( @@ -1301,12 +1267,12 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): # Build service data. service_data = {ATTR_ENTITY_ID: light} - features, supports_colors = _supported_features(self.hass, light) + features = _supported_features(self.hass, light) # Check transition == 0 to fix #378 - if ATTR_TRANSITION in features and transition > 0: + if "transition" in features and transition > 0: service_data[ATTR_TRANSITION] = transition - if ATTR_BRIGHTNESS in features and adapt_brightness: + if "brightness" in features and adapt_brightness: brightness = round(255 * self._settings["brightness_pct"] / 100) service_data[ATTR_BRIGHTNESS] = brightness @@ -1315,18 +1281,19 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): and self._sun_light_settings.sleep_rgb_or_color_temp == "rgb_color" ) if ( - ATTR_COLOR_TEMP_KELVIN in features + "color_temp" in features and adapt_color - and not (prefer_rgb_color and supports_colors) - and not (sleep_rgb and supports_colors) + and not (prefer_rgb_color and "color" in features) + and not (sleep_rgb and "color" in features) ): _LOGGER.debug("%s: Setting color_temp of light %s", self._name, light) - min_kelvin = features[ATTR_MIN_COLOR_TEMP_KELVIN] - max_kelvin = features[ATTR_MAX_COLOR_TEMP_KELVIN] + attributes = self.hass.states.get(light).attributes + min_kelvin = attributes["min_color_temp_kelvin"] + max_kelvin = attributes["max_color_temp_kelvin"] color_temp_kelvin = self._settings["color_temp_kelvin"] color_temp_kelvin = max(min(color_temp_kelvin, max_kelvin), min_kelvin) service_data[ATTR_COLOR_TEMP_KELVIN] = color_temp_kelvin - elif supports_colors and adapt_color: + elif "color" in features and adapt_color: _LOGGER.debug("%s: Setting rgb_color of light %s", self._name, light) service_data[ATTR_RGB_COLOR] = self._settings["rgb_color"] # Check if service data differs from the last. See #80.