-
-
Notifications
You must be signed in to change notification settings - Fork 38.2k
Migrate zha light to color_mode #70970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9e78df0
Migrate zha light to color_mode
emontnemery 7a05ac8
Fix restoring color mode
emontnemery abec02d
Correct set operations
emontnemery 214adf8
Derive color mode from group members
emontnemery eacb4b3
Add color mode to color channel
dmulcahey e2f4556
use Zigpy color mode enum
dmulcahey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,11 @@ | |
|
|
||
| from collections import Counter | ||
| from datetime import timedelta | ||
| import enum | ||
| import functools | ||
| import itertools | ||
| import logging | ||
| import random | ||
| from typing import Any | ||
| from typing import Any, cast | ||
|
|
||
| from zigpy.zcl.clusters.general import Identify, LevelControl, OnOff | ||
| from zigpy.zcl.clusters.lighting import Color | ||
|
|
@@ -17,16 +16,17 @@ | |
| from homeassistant.components import light | ||
| from homeassistant.components.light import ( | ||
| ATTR_BRIGHTNESS, | ||
| ATTR_COLOR_MODE, | ||
| ATTR_COLOR_TEMP, | ||
| ATTR_EFFECT, | ||
| ATTR_EFFECT_LIST, | ||
| ATTR_HS_COLOR, | ||
| ATTR_MAX_MIREDS, | ||
| ATTR_MIN_MIREDS, | ||
| SUPPORT_BRIGHTNESS, | ||
| SUPPORT_COLOR, | ||
| SUPPORT_COLOR_TEMP, | ||
| LightEntityFeature, | ||
| ATTR_SUPPORTED_COLOR_MODES, | ||
| ColorMode, | ||
| brightness_supported, | ||
| filter_supported_color_modes, | ||
| ) | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import ( | ||
|
|
@@ -86,24 +86,14 @@ | |
| PARALLEL_UPDATES = 0 | ||
| SIGNAL_LIGHT_GROUP_STATE_CHANGED = "zha_light_group_state_changed" | ||
|
|
||
| COLOR_MODES_GROUP_LIGHT = {ColorMode.COLOR_TEMP, ColorMode.HS} | ||
| SUPPORT_GROUP_LIGHT = ( | ||
| SUPPORT_BRIGHTNESS | ||
| | SUPPORT_COLOR_TEMP | ||
| | LightEntityFeature.EFFECT | ||
| | LightEntityFeature.FLASH | ||
| | SUPPORT_COLOR | ||
| | LightEntityFeature.TRANSITION | ||
| light.LightEntityFeature.EFFECT | ||
| | light.LightEntityFeature.FLASH | ||
| | light.LightEntityFeature.TRANSITION | ||
| ) | ||
|
|
||
|
|
||
| class LightColorMode(enum.IntEnum): | ||
| """ZCL light color mode enum.""" | ||
|
|
||
| HS_COLOR = 0x00 | ||
| XY_COLOR = 0x01 | ||
| COLOR_TEMP = 0x02 | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| config_entry: ConfigEntry, | ||
|
|
@@ -146,6 +136,7 @@ def __init__(self, *args, **kwargs): | |
| self._color_channel = None | ||
| self._identify_channel = None | ||
| self._default_transition = None | ||
| self._color_mode = ColorMode.UNKNOWN # Set by sub classes | ||
|
|
||
| @property | ||
| def extra_state_attributes(self) -> dict[str, Any]: | ||
|
|
@@ -160,6 +151,11 @@ def is_on(self) -> bool: | |
| return False | ||
| return self._state | ||
|
|
||
| @property | ||
| def color_mode(self): | ||
| """Return the color mode of this light.""" | ||
| return self._color_mode | ||
|
|
||
| @property | ||
| def brightness(self): | ||
| """Return the brightness of this light.""" | ||
|
|
@@ -230,9 +226,9 @@ async def async_turn_on(self, **kwargs): | |
| brightness = self._off_brightness | ||
|
|
||
| t_log = {} | ||
| if ( | ||
| brightness is not None or transition | ||
| ) and self._supported_features & light.SUPPORT_BRIGHTNESS: | ||
| if (brightness is not None or transition) and brightness_supported( | ||
| self._attr_supported_color_modes | ||
| ): | ||
| if brightness is not None: | ||
| level = min(254, brightness) | ||
| else: | ||
|
|
@@ -257,10 +253,7 @@ async def async_turn_on(self, **kwargs): | |
| self.debug("turned on: %s", t_log) | ||
| return | ||
| self._state = True | ||
| if ( | ||
| light.ATTR_COLOR_TEMP in kwargs | ||
| and self.supported_features & light.SUPPORT_COLOR_TEMP | ||
| ): | ||
| if light.ATTR_COLOR_TEMP in kwargs: | ||
| temperature = kwargs[light.ATTR_COLOR_TEMP] | ||
| result = await self._color_channel.move_to_color_temp(temperature, duration) | ||
| t_log["move_to_color_temp"] = result | ||
|
|
@@ -270,10 +263,7 @@ async def async_turn_on(self, **kwargs): | |
| self._color_temp = temperature | ||
| self._hs_color = None | ||
|
|
||
| if ( | ||
| light.ATTR_HS_COLOR in kwargs | ||
| and self.supported_features & light.SUPPORT_COLOR | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This guard was not needed, it's handled by the light base component |
||
| ): | ||
| if light.ATTR_HS_COLOR in kwargs: | ||
| hs_color = kwargs[light.ATTR_HS_COLOR] | ||
| xy_color = color_util.color_hs_to_xy(*hs_color) | ||
| result = await self._color_channel.move_to_color( | ||
|
|
@@ -286,10 +276,7 @@ async def async_turn_on(self, **kwargs): | |
| self._hs_color = hs_color | ||
| self._color_temp = None | ||
|
|
||
| if ( | ||
| effect == light.EFFECT_COLORLOOP | ||
| and self.supported_features & light.LightEntityFeature.EFFECT | ||
| ): | ||
| if effect == light.EFFECT_COLORLOOP: | ||
| result = await self._color_channel.color_loop_set( | ||
| UPDATE_COLORLOOP_ACTION | ||
| | UPDATE_COLORLOOP_DIRECTION | ||
|
|
@@ -302,9 +289,7 @@ async def async_turn_on(self, **kwargs): | |
| t_log["color_loop_set"] = result | ||
| self._effect = light.EFFECT_COLORLOOP | ||
| elif ( | ||
| self._effect == light.EFFECT_COLORLOOP | ||
| and effect != light.EFFECT_COLORLOOP | ||
| and self.supported_features & light.LightEntityFeature.EFFECT | ||
| self._effect == light.EFFECT_COLORLOOP and effect != light.EFFECT_COLORLOOP | ||
| ): | ||
| result = await self._color_channel.color_loop_set( | ||
| UPDATE_COLORLOOP_ACTION, | ||
|
|
@@ -316,10 +301,7 @@ async def async_turn_on(self, **kwargs): | |
| t_log["color_loop_set"] = result | ||
| self._effect = None | ||
|
|
||
| if ( | ||
| flash is not None | ||
| and self._supported_features & light.LightEntityFeature.FLASH | ||
| ): | ||
| if flash is not None: | ||
| result = await self._identify_channel.trigger_effect( | ||
| FLASH_EFFECTS[flash], EFFECT_DEFAULT_VARIANT | ||
| ) | ||
|
|
@@ -332,7 +314,7 @@ async def async_turn_on(self, **kwargs): | |
| async def async_turn_off(self, **kwargs): | ||
| """Turn the entity off.""" | ||
| duration = kwargs.get(light.ATTR_TRANSITION) | ||
| supports_level = self.supported_features & light.SUPPORT_BRIGHTNESS | ||
| supports_level = brightness_supported(self._attr_supported_color_modes) | ||
|
|
||
| if duration and supports_level: | ||
| result = await self._level_channel.move_to_level_with_on_off( | ||
|
|
@@ -356,6 +338,7 @@ async def async_turn_off(self, **kwargs): | |
| class Light(BaseLight, ZhaEntity): | ||
| """Representation of a ZHA or ZLL light.""" | ||
|
|
||
| _attr_supported_color_modes: set(ColorMode) | ||
| _REFRESH_INTERVAL = (45, 75) | ||
|
|
||
| def __init__(self, unique_id, zha_device: ZhaDeviceType, channels, **kwargs): | ||
|
|
@@ -372,19 +355,20 @@ def __init__(self, unique_id, zha_device: ZhaDeviceType, channels, **kwargs): | |
| self._cancel_refresh_handle = None | ||
| effect_list = [] | ||
|
|
||
| self._attr_supported_color_modes = {ColorMode.ONOFF} | ||
| if self._level_channel: | ||
| self._supported_features |= light.SUPPORT_BRIGHTNESS | ||
| self._attr_supported_color_modes.add(ColorMode.BRIGHTNESS) | ||
| self._supported_features |= light.LightEntityFeature.TRANSITION | ||
| self._brightness = self._level_channel.current_level | ||
|
|
||
| if self._color_channel: | ||
| color_capabilities = self._color_channel.color_capabilities | ||
| if color_capabilities & CAPABILITIES_COLOR_TEMP: | ||
| self._supported_features |= light.SUPPORT_COLOR_TEMP | ||
| self._attr_supported_color_modes.add(ColorMode.COLOR_TEMP) | ||
| self._color_temp = self._color_channel.color_temperature | ||
|
|
||
| if color_capabilities & CAPABILITIES_COLOR_XY: | ||
| self._supported_features |= light.SUPPORT_COLOR | ||
| self._attr_supported_color_modes.add(ColorMode.HS) | ||
| curr_x = self._color_channel.current_x | ||
| curr_y = self._color_channel.current_y | ||
| if curr_x is not None and curr_y is not None: | ||
|
|
@@ -399,6 +383,16 @@ def __init__(self, unique_id, zha_device: ZhaDeviceType, channels, **kwargs): | |
| effect_list.append(light.EFFECT_COLORLOOP) | ||
| if self._color_channel.color_loop_active == 1: | ||
| self._effect = light.EFFECT_COLORLOOP | ||
| self._attr_supported_color_modes = filter_supported_color_modes( | ||
| self._attr_supported_color_modes | ||
| ) | ||
| if len(self._attr_supported_color_modes) == 1: | ||
| self._color_mode = next(iter(self._attr_supported_color_modes)) | ||
| else: # Light supports color_temp + hs, determine which mode the light is in | ||
| if self._color_channel.color_mode == Color.ColorMode.Color_temperature: | ||
| self._color_mode = ColorMode.COLOR_TEMP | ||
| else: | ||
| self._color_mode = ColorMode.HS | ||
|
|
||
| if self._identify_channel: | ||
| self._supported_features |= light.LightEntityFeature.FLASH | ||
|
|
@@ -455,6 +449,8 @@ def async_restore_last_state(self, last_state): | |
| self._brightness = last_state.attributes["brightness"] | ||
| if "off_brightness" in last_state.attributes: | ||
| self._off_brightness = last_state.attributes["off_brightness"] | ||
| if "color_mode" in last_state.attributes: | ||
| self._color_mode = ColorMode(last_state.attributes["color_mode"]) | ||
| if "color_temp" in last_state.attributes: | ||
| self._color_temp = last_state.attributes["color_temp"] | ||
| if "hs_color" in last_state.attributes: | ||
|
|
@@ -493,12 +489,14 @@ async def async_get_state(self): | |
| ) | ||
|
|
||
| if (color_mode := results.get("color_mode")) is not None: | ||
| if color_mode == LightColorMode.COLOR_TEMP: | ||
| if color_mode == Color.ColorMode.Color_temperature: | ||
| self._color_mode = ColorMode.COLOR_TEMP | ||
| color_temp = results.get("color_temperature") | ||
| if color_temp is not None and color_mode: | ||
| self._color_temp = color_temp | ||
| self._hs_color = None | ||
| else: | ||
| self._color_mode = ColorMode.HS | ||
| color_x = results.get("current_x") | ||
| color_y = results.get("current_y") | ||
| if color_x is not None and color_y is not None: | ||
|
|
@@ -573,6 +571,7 @@ def __init__( | |
| CONF_DEFAULT_LIGHT_TRANSITION, | ||
| 0, | ||
| ) | ||
| self._color_mode = None | ||
|
|
||
| async def async_added_to_hass(self): | ||
| """Run when about to be added to hass.""" | ||
|
|
@@ -633,6 +632,29 @@ async def async_update(self) -> None: | |
| effects_count = Counter(itertools.chain(all_effects)) | ||
| self._effect = effects_count.most_common(1)[0][0] | ||
|
|
||
| self._attr_color_mode = None | ||
| all_color_modes = list( | ||
| helpers.find_state_attributes(on_states, ATTR_COLOR_MODE) | ||
| ) | ||
| if all_color_modes: | ||
| # Report the most common color mode, select brightness and onoff last | ||
| color_mode_count = Counter(itertools.chain(all_color_modes)) | ||
| if ColorMode.ONOFF in color_mode_count: | ||
| color_mode_count[ColorMode.ONOFF] = -1 | ||
| if ColorMode.BRIGHTNESS in color_mode_count: | ||
| color_mode_count[ColorMode.BRIGHTNESS] = 0 | ||
| self._attr_color_mode = color_mode_count.most_common(1)[0][0] | ||
|
|
||
| self._attr_supported_color_modes = None | ||
| all_supported_color_modes = list( | ||
| helpers.find_state_attributes(states, ATTR_SUPPORTED_COLOR_MODES) | ||
| ) | ||
| if all_supported_color_modes: | ||
| # Merge all color modes. | ||
| self._attr_supported_color_modes = cast( | ||
| set[str], set().union(*all_supported_color_modes) | ||
| ) | ||
|
|
||
| self._supported_features = 0 | ||
| for support in helpers.find_state_attributes(states, ATTR_SUPPORTED_FEATURES): | ||
| # Merge supported features by emulating support for every feature | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This guard was not needed, it's handled by the light base component