Skip to content

Commit

Permalink
Add support for color_temp_command_template in MQTT light component (#…
Browse files Browse the repository at this point in the history
…19675)

* Add support for color_temp_command_template in MQTT light component
  • Loading branch information
dchesterton authored and emontnemery committed Jan 1, 2019
1 parent b9f4a72 commit 61d5b30
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions homeassistant/components/light/mqtt/schema_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
CONF_BRIGHTNESS_SCALE = 'brightness_scale'
CONF_BRIGHTNESS_STATE_TOPIC = 'brightness_state_topic'
CONF_BRIGHTNESS_VALUE_TEMPLATE = 'brightness_value_template'
CONF_COLOR_TEMP_COMMAND_TEMPLATE = 'color_temp_command_template'
CONF_COLOR_TEMP_COMMAND_TOPIC = 'color_temp_command_topic'
CONF_COLOR_TEMP_STATE_TOPIC = 'color_temp_state_topic'
CONF_COLOR_TEMP_VALUE_TEMPLATE = 'color_temp_value_template'
Expand Down Expand Up @@ -75,6 +76,7 @@
vol.All(vol.Coerce(int), vol.Range(min=1)),
vol.Optional(CONF_BRIGHTNESS_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_BRIGHTNESS_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_COLOR_TEMP_COMMAND_TEMPLATE): cv.template,
vol.Optional(CONF_COLOR_TEMP_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_COLOR_TEMP_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_COLOR_TEMP_VALUE_TEMPLATE): cv.template,
Expand Down Expand Up @@ -207,6 +209,8 @@ def _setup_from_config(self, config):
self._templates = {
CONF_BRIGHTNESS: config.get(CONF_BRIGHTNESS_VALUE_TEMPLATE),
CONF_COLOR_TEMP: config.get(CONF_COLOR_TEMP_VALUE_TEMPLATE),
CONF_COLOR_TEMP_COMMAND_TEMPLATE:
config.get(CONF_COLOR_TEMP_COMMAND_TEMPLATE),
CONF_EFFECT: config.get(CONF_EFFECT_VALUE_TEMPLATE),
CONF_HS: config.get(CONF_HS_VALUE_TEMPLATE),
CONF_RGB: config.get(CONF_RGB_VALUE_TEMPLATE),
Expand Down Expand Up @@ -682,6 +686,13 @@ async def async_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])
tpl = self._templates[CONF_COLOR_TEMP_COMMAND_TEMPLATE]

if tpl:
color_temp = tpl.async_render({
'value': color_temp,
})

mqtt.async_publish(
self.hass, self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC],
color_temp, self._config.get(CONF_QOS),
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/mqtt/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
'bri_scl': 'brightness_scale',
'bri_stat_t': 'brightness_state_topic',
'bri_val_tpl': 'brightness_value_template',
'clr_temp_cmd_tpl': 'color_temp_command_template',
'clr_temp_cmd_t': 'color_temp_command_topic',
'clr_temp_stat_t': 'color_temp_state_topic',
'clr_temp_val_tpl': 'color_temp_value_template',
Expand Down
31 changes: 31 additions & 0 deletions tests/components/light/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,37 @@ async def test_sending_mqtt_rgb_command_with_template(hass, mqtt_mock):
assert (255, 128, 63) == state.attributes['rgb_color']


async def test_sending_mqtt_color_temp_command_with_template(hass, mqtt_mock):
"""Test the sending of Color Temp command with template."""
config = {light.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'command_topic': 'test_light_color_temp/set',
'color_temp_command_topic': 'test_light_color_temp/color_temp/set',
'color_temp_command_template': '{{ (1000 / value) | round(0) }}',
'payload_on': 'on',
'payload_off': 'off',
'qos': 0
}}

assert await async_setup_component(hass, light.DOMAIN, config)

state = hass.states.get('light.test')
assert STATE_OFF == state.state

common.async_turn_on(hass, 'light.test', color_temp=100)
await hass.async_block_till_done()

mqtt_mock.async_publish.assert_has_calls([
mock.call('test_light_color_temp/set', 'on', 0, False),
mock.call('test_light_color_temp/color_temp/set', '10', 0, False),
], any_order=True)

state = hass.states.get('light.test')
assert STATE_ON == state.state
assert 100 == state.attributes['color_temp']


async def test_show_brightness_if_only_command_topic(hass, mqtt_mock):
"""Test the brightness if only a command topic is present."""
config = {light.DOMAIN: {
Expand Down

0 comments on commit 61d5b30

Please sign in to comment.