Skip to content

Migrate homekit_controller light to color_mode#69261

Merged
bdraco merged 6 commits intodevfrom
homekit_controller_light_color_mode
Apr 20, 2022
Merged

Migrate homekit_controller light to color_mode#69261
bdraco merged 6 commits intodevfrom
homekit_controller_light_color_mode

Conversation

@emontnemery
Copy link
Copy Markdown
Contributor

Proposed change

Migrate homekit_controller 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 @Jc2k, @bdraco, mind taking a look at this pull request as it has been labeled with an integration (homekit_controller) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

Comment on lines +85 to +92
if self.service.has(CharacteristicsTypes.HUE):
return COLOR_MODE_HS

if self.service.has(CharacteristicsTypes.SATURATION):
return COLOR_MODE_HS

if self.service.has(CharacteristicsTypes.COLOR_TEMPERATURE):
return COLOR_MODE_COLOR_TEMP
Copy link
Copy Markdown
Contributor Author

@emontnemery emontnemery Apr 4, 2022

Choose a reason for hiding this comment

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

This does not change the behavior compared to without this PR because it would always report a non-None hs color for lights supporting hue/saturation which is interpreted as color mode hs by the base entity.
However, for lights which support both hs and white mode it would be better to report according to the light's state.
I did not immediately find anything useful in the library though. Any idea, @Jc2k , @bdraco?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

HomeKit (at least as defined in the public spec (https://developer.apple.com/file/?file=homekitspecification&agree=true)) doesn't have the concept of an active color mode. You can use h/s and you can use color temp. It has the characteristics you can see here, and thats it. There's no way to indicate which color mode is currently in use.

Sometimes you can figure out a bit more from the open source example code. There are some notes here:

https://github.com/apple/HomeKitADK/blob/b171c17dba6e593e243b5fdd61fb986e9fd3b5cb/HAP/HAPServiceTypes.h#L76

Again, theres no characteristic to indicate a color mode there.

#42807 (via @bdraco) says:

The HomeKit R2 spec explicitly states that the Color Temperature char must not be used for lamps which support color.

Does that help?

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.

OK, I see. I'll add a comment in the code stating that's the case.

Copy link
Copy Markdown
Member

@bdraco bdraco Apr 4, 2022

Choose a reason for hiding this comment

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

The situation has changed a bit in iOS 13+

The spec says that you're not supposed to have color and color temperature at the same time, but in practice even certified devices implement that now. Apple seems to be certifying them anyway, and the hope is that the spec will get updated in the next version to remove that restriction. iOS seems to handle it now so it seems like the documentation is behind the actual implementation

Copy link
Copy Markdown
Member

@bdraco bdraco Apr 4, 2022

Choose a reason for hiding this comment

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

The way iOS seems to handle it is, whichever characteristic was the last to get a notify/event for is the color mode you're in

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hopefully we'll get a new spec or an ADK update... (doubt it).

It might be less sucky after I've done all the refactoring I want to in aiohomekit (I want the abstraction to be higher level than it is right now).

Copy link
Copy Markdown
Contributor Author

@emontnemery emontnemery Apr 5, 2022

Choose a reason for hiding this comment

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

Based on your discussion I don't think the simplification I did in 5ee6331 was correct, it should instead be like this:

  • If the light reports support for both ct and hs, we should expose that as supported_color_modes = {COLOR_MODE_COLOR_TEMP, COLOR_MODE_HS} (no change compared to before this PR)
  • Because aiohomekit does not track which color mode the light is in, a light supporting both ct and hs should just report color_mode COLOR_MODE_HS (no change compared to before this PR)

Is that right?

Copy link
Copy Markdown
Member

@Jc2k Jc2k Apr 5, 2022

Choose a reason for hiding this comment

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

To be more pedantic on the second bullet: the HAP protocol itself (rather than aiohomekit) doesn't really have a concept of a current colour mode to track and aiohomekit's abstraction doesn't let us work around that. And even if it did, it would be an incomplete work around (we would lose the "current colour mode" on a simple HA restart for example). I assume the official iOS implementation is subject to the same limitations too.

(The thing I'm being pedantic about is that despite the changes in iOS 13 it's still not the case there is a variable /to/ track, we would have to track multiple variables for multiple event types (push, put and poll) and then infer our own value from the event timestamps of a subset of event types, only to have a partially working implementation at the end of it).

In the future we could implement the heuristics described to make it closer to the ideal, but short of Apple adding a new colour mode characteristic there is a potential for our implementation to seem "glitchy". So we might not want to implement such heuristics at all.

But the ultimate conclusion is the same, I think.

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.

OK, updated again.

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.

@bdraco, @Jc2k Is this PR OK now, or are more changes needed?

@emontnemery emontnemery marked this pull request as draft April 4, 2022 12:55
@emontnemery emontnemery marked this pull request as ready for review April 4, 2022 12:55
Copy link
Copy Markdown
Member

@Jc2k Jc2k left a comment

Choose a reason for hiding this comment

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

Sorry this dropped off my radar, LGTM 👍

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Apr 20, 2022

Looks good. Testing now

Copy link
Copy Markdown
Member

@bdraco bdraco left a comment

Choose a reason for hiding this comment

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

Testing passed 👍

@bdraco bdraco merged commit 7c0b0f7 into dev Apr 20, 2022
@bdraco bdraco deleted the homekit_controller_light_color_mode branch April 20, 2022 19:26
@github-actions github-actions bot locked and limited conversation to collaborators Apr 21, 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.

4 participants