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

Add color_temp_command_template for mqtt light component #5105

Closed
wants to merge 4 commits into from

Conversation

jonfink
Copy link

@jonfink jonfink commented Dec 29, 2016

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:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:
n/a code interacts with mqtt devices

@mention-bot
Copy link

@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))

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])))))

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)

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))))

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)

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 =

Choose a reason for hiding this comment

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

SyntaxError: invalid syntax

@@ -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":
Copy link
Member

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 ?

Copy link
Author

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...

Copy link
Member

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()
Copy link
Member

Choose a reason for hiding this comment

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

please change this back.

Copy link
Author

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()
Copy link
Member

Choose a reason for hiding this comment

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

please change this back.

Copy link
Author

Choose a reason for hiding this comment

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

Done in 66012e9

@balloob balloob self-assigned this Jan 8, 2017
@jjensn
Copy link
Contributor

jjensn commented Jan 12, 2017

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.
@jonfink
Copy link
Author

jonfink commented Jan 13, 2017

I think I've addressed all of the above comments with the exception of the dictionary keys used to instantiate the MqttLight class template argument. I'm not entirely sure on the rationale in general for these keys coming from homeassistant.const since they are entirely internal to the mqtt light component, but if that part is important it would be a relatively small change to add an additional value to the const module.

Copy link
Member

@MartinHjelmare MartinHjelmare left a 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":
Copy link
Member

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"](
Copy link
Member

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.

Copy link
Contributor

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?

Copy link
Member

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.

@jjensn
Copy link
Contributor

jjensn commented Jan 20, 2017

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.

@balloob
Copy link
Member

balloob commented Jan 24, 2017

@jjensn I like that approach 👍

@homeassistant
Copy link
Contributor

Hi @jonfink,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@andrey-git
Copy link
Contributor

@jonfink any updates on this?

@balloob
Copy link
Member

balloob commented Feb 12, 2017

This PR seems to have gone stale. Closing it. You can reopen it when you're ready to finish it.

@balloob balloob closed this Feb 12, 2017
@raccettura
Copy link
Contributor

Any chance on resurrecting this? Would be handy to control osram bulbs via HA->ST.

@home-assistant home-assistant locked and limited conversation to collaborators Jul 17, 2017
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.

None yet

10 participants