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
Add color_temp_command_template for mqtt light component #5105
Conversation
@jonfink, thanks for your PR! By analyzing the history of the files in this pull request, we identified @robbiet480, @pvizeli and @fabaff to be potential reviewers. |
@@ -266,7 +274,8 @@ def turn_on(self, **kwargs): | |||
|
|||
if ATTR_COLOR_TEMP in kwargs and \ | |||
self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None: | |||
color_temp = int(kwargs[ATTR_COLOR_TEMP]) | |||
color_temp = int(float((self.templates[CONF_COLOR_TEMP+"_COMMAND"](str(kwargs[ATTR_COLOR_TEMP]))))) | |||
_LOGGER.debug("Command temp %f mired, coverts to %d K" % (kwargs[ATTR_COLOR_TEMP], color_temp)) |
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.
line too long (107 > 79 characters)
@@ -266,7 +274,8 @@ def turn_on(self, **kwargs): | |||
|
|||
if ATTR_COLOR_TEMP in kwargs and \ | |||
self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None: | |||
color_temp = int(kwargs[ATTR_COLOR_TEMP]) | |||
color_temp = int(float((self.templates[CONF_COLOR_TEMP+"_COMMAND"](str(kwargs[ATTR_COLOR_TEMP]))))) |
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.
line too long (111 > 79 characters)
@@ -184,7 +191,8 @@ def rgb_received(topic, payload, qos): | |||
|
|||
def color_temp_received(topic, payload, qos): | |||
"""A new MQTT message for color temp has been received.""" | |||
self._color_temp = int(templates[CONF_COLOR_TEMP](payload)) | |||
self._color_temp = int(float((templates[CONF_COLOR_TEMP](payload)))) | |||
_LOGGER.debug("Received color temp: %f, converts to %d", float(payload), self._color_temp) |
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.
line too long (102 > 79 characters)
@@ -184,7 +191,8 @@ def rgb_received(topic, payload, qos): | |||
|
|||
def color_temp_received(topic, payload, qos): | |||
"""A new MQTT message for color temp has been received.""" | |||
self._color_temp = int(templates[CONF_COLOR_TEMP](payload)) | |||
self._color_temp = int(float((templates[CONF_COLOR_TEMP](payload)))) |
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.
line too long (80 > 79 characters)
@@ -85,7 +89,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None): | |||
CONF_STATE: config.get(CONF_STATE_VALUE_TEMPLATE), | |||
CONF_BRIGHTNESS: config.get(CONF_BRIGHTNESS_VALUE_TEMPLATE), | |||
CONF_RGB: config.get(CONF_RGB_VALUE_TEMPLATE), | |||
CONF_COLOR_TEMP: config.get(CONF_COLOR_TEMP_VALUE_TEMPLATE) | |||
CONF_COLOR_TEMP: config.get(CONF_COLOR_TEMP_VALUE_TEMPLATE), | |||
CONF_COLOR_TEMP+"_COMMAND": config.get(CONF_COLOR_TEMP_COMMAND_TEMPLATE) |
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.
line too long (84 > 79 characters)
@@ -184,7 +192,10 @@ def rgb_received(topic, payload, qos): | |||
|
|||
def color_temp_received(topic, payload, qos): | |||
"""A new MQTT message for color temp has been received.""" | |||
self._color_temp = int(templates[CONF_COLOR_TEMP](payload)) | |||
self._color_temp = |
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.
SyntaxError: invalid syntax
24773da
to
3b6e228
Compare
Documentation to support home-assistant/core#5105
@@ -85,7 +89,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None): | |||
CONF_STATE: config.get(CONF_STATE_VALUE_TEMPLATE), | |||
CONF_BRIGHTNESS: config.get(CONF_BRIGHTNESS_VALUE_TEMPLATE), | |||
CONF_RGB: config.get(CONF_RGB_VALUE_TEMPLATE), | |||
CONF_COLOR_TEMP: config.get(CONF_COLOR_TEMP_VALUE_TEMPLATE) | |||
CONF_COLOR_TEMP: config.get(CONF_COLOR_TEMP_VALUE_TEMPLATE), | |||
CONF_COLOR_TEMP+"_COMMAND": |
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 will become color_temp_command_COMMAND
?
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.
The dictionary key will be color_temp_command
to go alongside the existing dictionary key of color_temp
.
Honestly, I wasn't sure how best to handle this as it seemed more far-reaching than desired to make a change in the mqtt light component that required an addition to the homeassistant.const module. If that is the standard practice, then CONF_COLOR_TEMP_COMMAND
could be added to homeassistant/const.py
...
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.
Just use CONF_COLOR_TEMP_COMMAND
as key here.
@@ -283,7 +298,7 @@ def turn_on(self, **kwargs): | |||
should_update = True | |||
|
|||
if should_update: | |||
self.schedule_update_ha_state() | |||
self.update_ha_state() |
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.
please change this back.
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.
Done in 66012e9
@@ -293,4 +308,4 @@ def turn_off(self, **kwargs): | |||
if self._optimistic: | |||
# Optimistically assume that switch has changed state. | |||
self._state = False | |||
self.schedule_update_ha_state() | |||
self.update_ha_state() |
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.
please change this back.
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.
Done in 66012e9
What needs to be done here to get this approved? @jonfink how can I help? |
Home assistant internal unit for color temperature is Mired but some systems (e.g., SmartThings through mqtt bridge) use Kelvin. Conversion between these types can be handled with the value template concept (used previously to unpack json values). Added a "color_temp_command_template" configuration option for MQTT lights that specifies a function to be applied to the HA-internal color temperature value to get a light-compatible value.
66012e9
to
8b88d77
Compare
I think I've addressed all of the above comments with the exception of the dictionary keys used to instantiate the MqttLight class |
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.
See comments. There was a unit test failing also. Fix that and add test(s) for the new functionality.
@@ -85,7 +89,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None): | |||
CONF_STATE: config.get(CONF_STATE_VALUE_TEMPLATE), | |||
CONF_BRIGHTNESS: config.get(CONF_BRIGHTNESS_VALUE_TEMPLATE), | |||
CONF_RGB: config.get(CONF_RGB_VALUE_TEMPLATE), | |||
CONF_COLOR_TEMP: config.get(CONF_COLOR_TEMP_VALUE_TEMPLATE) | |||
CONF_COLOR_TEMP: config.get(CONF_COLOR_TEMP_VALUE_TEMPLATE), | |||
CONF_COLOR_TEMP+"_COMMAND": |
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.
Just use CONF_COLOR_TEMP_COMMAND
as key here.
@@ -266,7 +277,11 @@ def turn_on(self, **kwargs): | |||
|
|||
if ATTR_COLOR_TEMP in kwargs and \ | |||
self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None: | |||
color_temp = int(kwargs[ATTR_COLOR_TEMP]) | |||
color_temp = int(float(( | |||
self.templates[CONF_COLOR_TEMP+"_COMMAND"]( |
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.
Use CONF_COLOR_TEMP_COMMAND
as key here.
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.
Hey @MartinHjelmare -- doesn't CONF_COLOR_TEMP_COMMAND have to be added to https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/const.py ? Is that the change you are calling for?
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.
No, you've already added it to homeassistant/components/light/mqtt.py
which is fine. Constants that are only used in one module don't need to be in const.py
. Just use the constant now in the two places where it's needed.
How do the collaborators feel about this approach vs using a configuration option and doing the conversion from mired to kelvin there? I ask because, someone (me) is going to want to add RGB templating next, and you can't quite do RGB -> hue/sat very well in a template (as far as I know, the math would be too complex). So why not just add two more variables, color_temp_mode (similar to flux component: https://home-assistant.io/components/switch.flux/), and color_mode (rgb vs xy vs hue/sat) ? I think really, the only reason @jonfink is adding the support for this, is so we can convert HA's default color_temp (mired) to kelvin. I can't see any other use for templating out the command other than that conversion. |
@jjensn I like that approach 👍 |
@jonfink any updates on this? |
This PR seems to have gone stale. Closing it. You can reopen it when you're ready to finish it. |
Any chance on resurrecting this? Would be handy to control osram bulbs via HA->ST. |
Description:
Home assistant internal unit for color temperature is Mired but some
systems (e.g., SmartThings through mqtt bridge) use Kelvin. Conversion
between these types can be handled with the value template
concept (used previously to unpack json values).
Added a "color_temp_command_template" configuration option for MQTT
lights that specifies a function to be applied to the HA-internal
color temperature value to get a light-compatible value.
Related issue (if applicable):
n/a
Pull request in home-assistant.github.io with documentation (if applicable):
home-assistant/home-assistant.io#1673
Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
tox
run successfully. Your PR cannot be merged unless tests passREQUIREMENTS
variable (example).requirements_all.txt
by runningscript/gen_requirements_all.py
..coveragerc
.If the code does not interact with devices:
n/a code interacts with mqtt devices