Skip to content

Migrate zha light to color_mode#70970

Merged
emontnemery merged 6 commits intodevfrom
zha_light_color_mode
May 27, 2022
Merged

Migrate zha light to color_mode#70970
emontnemery merged 6 commits intodevfrom
zha_light_color_mode

Conversation

@emontnemery
Copy link
Copy Markdown
Contributor

Proposed change

Migrate zha light to color_mode

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@probot-home-assistant
Copy link
Copy Markdown

Hey there @dmulcahey, @Adminiuga, mind taking a look at this pull request as it has been labeled with an integration (zha) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@emontnemery emontnemery marked this pull request as draft April 28, 2022 08:31
self._state = True
if (
light.ATTR_COLOR_TEMP in kwargs
and self.supported_features & light.SUPPORT_COLOR_TEMP
Copy link
Copy Markdown
Contributor Author

@emontnemery emontnemery Apr 28, 2022

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


if (
light.ATTR_HS_COLOR in kwargs
and self.supported_features & light.SUPPORT_COLOR
Copy link
Copy Markdown
Contributor Author

@emontnemery emontnemery Apr 28, 2022

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
Same comment for the removed guards below.


if (
effect == light.EFFECT_COLORLOOP
and self.supported_features & light.SUPPORT_EFFECT
Copy link
Copy Markdown
Contributor Author

@emontnemery emontnemery Apr 28, 2022

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

elif (
self._effect == light.EFFECT_COLORLOOP
and effect != light.EFFECT_COLORLOOP
and self.supported_features & light.SUPPORT_EFFECT
Copy link
Copy Markdown
Contributor Author

@emontnemery emontnemery Apr 28, 2022

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

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 == LightColorMode.COLOR_TEMP:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the correct way to determine the color mode of the light when the entity is initialized?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

color_mode doesn’t exist on the channel.

class ColorChannel(ZigbeeChannel):

It will need to be added. The attribute will also need to be added to the ZCL_INIT_ATTRS dict with False to make sure it’s read during initialization and not read from cache.

then the comparisons need to be against the Zigpy enum: https://github.com/zigpy/zigpy/blob/b166e2e1f22c49bad201d7ecdf202f0fc9b7b087/zigpy/zcl/clusters/lighting.py#L14

CONF_DEFAULT_LIGHT_TRANSITION,
0,
)
self._attr_supported_color_modes = COLOR_MODES_GROUP_LIGHT
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the ZHA light group, it seems to derive its state not from zigpy but from the HA state machine, while it changes the state of the group by talking to zigpy?

Is it OK to set the light group to either HS or CT; and how do we determine which one the group is in; the members could be in states: ONOFF, BRIGHTNESS (dimmers with no adjustable color temperature), COLOR_TEMP or HS.

We could map ONOFF + BRIGHTNESS + COLOR_TEMP to a COLOR_TEMP group and HS to a HS group and make a majority decision?

For reference, this is the implementation in the standard light group:

self._attr_color_mode = None
all_color_modes = list(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]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the ZHA light group, it seems to derive its state not from zigpy but from the HA state machine, while it changes the state of the group by talking to zigpy?

It derives the group state by aggregating the state of each member entity from HA state machine.

Is it OK to set the light group to either HS or CT; and how do we determine which one the group is in; the members could be in states: ONOFF, BRIGHTNESS (dimmers with no adjustable color temperature), COLOR_TEMP or HS.

We could map ONOFF + BRIGHTNESS + COLOR_TEMP to a COLOR_TEMP group and HS to a HS group and make a majority decision?

IIRC currently as long as one member is ON then the entire group is on. The group state is calculated based on the ON entities. Need to verify.

The ZigBee groups could be crazy, as I do mix in one group CT lamps and color lamps. At the ZigBee network level, lamps without color support just ignore the color commands

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improved in 214adf8, zha groups is now using same logic as the standard light group

@epenet

This comment was marked as outdated.

@emontnemery emontnemery force-pushed the zha_light_color_mode branch from ce67d33 to 9e78df0 Compare May 13, 2022 08:35
@emontnemery emontnemery marked this pull request as ready for review May 27, 2022 08:08
@emontnemery emontnemery merged commit 5ca82b2 into dev May 27, 2022
@emontnemery emontnemery deleted the zha_light_color_mode branch May 27, 2022 13:38
@Adminiuga
Copy link
Copy Markdown
Contributor

So far nothing jumps out at me, but i have a question: what happens when the color_mode attribute is None (unsupported)? IIRC there are some color bulbs which don't support this attribute. It still would behave as an older version, right?

@github-actions github-actions bot locked and limited conversation to collaborators May 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants