Skip to content
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

More accurately convert colortemp to xy and hs #146

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion custom_components/circadian_lighting/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"issue_tracker": "https://github.com/claytonjn/hass-circadian_lighting/issues",
"dependencies": ["sensor","switch"],
"codeowners": ["@claytonjn"],
"requirements": [],
"requirements": ["colour-science"],
"iot_class": "calculated"
}
6 changes: 4 additions & 2 deletions custom_components/circadian_lighting/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from itertools import repeat

import voluptuous as vol
import colour

import homeassistant.helpers.config_validation as cv
from homeassistant.components.light import (
Expand Down Expand Up @@ -299,7 +300,7 @@ def _calc_rgb(self):
return color_temperature_to_rgb(self._color_temperature())

def _calc_xy(self):
return color_RGB_to_xy(*self._calc_rgb())
return list(colour.temperature.CCT_to_xy_CIE_D(self._color_temperature()))
Copy link
Owner

Choose a reason for hiding this comment

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

This is something that's bothered me too but adding additional package requirements has always caused problems in the past for users that have non "standard" installations. If the colour-science package fails to install then the entire CL integration will break. I think a better solution would be to wrap this in a try and if an exception occurs to use the current built-in method. I can make that change when I get a chance or feel free to handle it if you're so inclined.

Copy link
Author

Choose a reason for hiding this comment

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

Hey, thanks for the feedback. Yep, agree with your point, also because currently the packaged version of colour-science has a problem when running on raspberry-pi (this was fixed ~8 months ago on their repo, but it is not yet on the release version). I'll add this check by the end of the week :)

Choose a reason for hiding this comment

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

If the only method used is color.temperature.CCT_to_xy_CIE_D() another option would be to just extract the code for this method (https://github.com/colour-science/colour/blob/a412b670448534987fc25d3a1a09cedc2a3ea5f2/colour/temperature/cie_d.py#L127-L183) from the package and omit the rest of it.

This would completely ditch the dependency while still providing the same functionality.


def _calc_hs(self):
return color_xy_to_hs(*self._calc_xy())
Expand Down Expand Up @@ -352,7 +353,8 @@ async def _adjust_lights(self, lights, transition):
if not is_on(self.hass, light):
continue

service_data = {ATTR_ENTITY_ID: light, ATTR_TRANSITION: transition}
service_data = {ATTR_ENTITY_ID: light}
service_data[ATTR_TRANSITION] = transition
Copy link
Owner

Choose a reason for hiding this comment

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

Is there an advantage to breaking this into two lines? Have you run into issues where service_data was not set because of an issue with light or transition?

Copy link
Author

Choose a reason for hiding this comment

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

Ah well spotted! I actually didn't intend to commit this, but I forgot to remove it after I tested it on my system. The problem is that IKEA light bulbs don't handle transition and color changes commands issued at the same time very well. Particularly, they don't change the color when this happens, so I just commented out the transition part on my system.

if self._brightness is not None:
service_data[ATTR_BRIGHTNESS] = int((self._brightness / 100) * 254)

Expand Down