diff --git a/homeassistant/components/light/mqtt/schema_basic.py b/homeassistant/components/light/mqtt/schema_basic.py index fdd8f1aaf85..a263ed66d6d 100644 --- a/homeassistant/components/light/mqtt/schema_basic.py +++ b/homeassistant/components/light/mqtt/schema_basic.py @@ -34,6 +34,7 @@ CONF_BRIGHTNESS_COMMAND_TOPIC = 'brightness_command_topic' 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' @@ -75,6 +76,7 @@ PLATFORM_SCHEMA_BASIC = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({ 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, @@ -207,6 +209,8 @@ class MqttLight(MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, 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), @@ -682,6 +686,13 @@ class MqttLight(MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, 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), diff --git a/homeassistant/components/mqtt/discovery.py b/homeassistant/components/mqtt/discovery.py index 2bd21f06b3c..92e7ffdaf4f 100644 --- a/homeassistant/components/mqtt/discovery.py +++ b/homeassistant/components/mqtt/discovery.py @@ -68,6 +68,7 @@ ABBREVIATIONS = { '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', diff --git a/tests/components/light/test_mqtt.py b/tests/components/light/test_mqtt.py index 89f696747d3..951a9f04be9 100644 --- a/tests/components/light/test_mqtt.py +++ b/tests/components/light/test_mqtt.py @@ -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: {