mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 05:47:10 +00:00
Fix MQTT light value template (#39820)
This commit is contained in:
parent
9ca7efbe4c
commit
c2f16cf21d
@ -35,6 +35,7 @@ from homeassistant.const import (
|
|||||||
CONF_PAYLOAD_OFF,
|
CONF_PAYLOAD_OFF,
|
||||||
CONF_PAYLOAD_ON,
|
CONF_PAYLOAD_ON,
|
||||||
CONF_UNIQUE_ID,
|
CONF_UNIQUE_ID,
|
||||||
|
CONF_VALUE_TEMPLATE,
|
||||||
STATE_ON,
|
STATE_ON,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
@ -157,6 +158,9 @@ async def async_setup_entity_basic(
|
|||||||
hass, config, async_add_entities, config_entry, discovery_data=None
|
hass, config, async_add_entities, config_entry, discovery_data=None
|
||||||
):
|
):
|
||||||
"""Set up a MQTT Light."""
|
"""Set up a MQTT Light."""
|
||||||
|
if CONF_STATE_VALUE_TEMPLATE not in config and CONF_VALUE_TEMPLATE in config:
|
||||||
|
config[CONF_STATE_VALUE_TEMPLATE] = config[CONF_VALUE_TEMPLATE]
|
||||||
|
|
||||||
async_add_entities([MqttLight(hass, config, config_entry, discovery_data)])
|
async_add_entities([MqttLight(hass, config, config_entry, discovery_data)])
|
||||||
|
|
||||||
|
|
||||||
|
@ -648,6 +648,35 @@ async def test_controlling_state_via_topic_with_templates(hass, mqtt_mock):
|
|||||||
assert state.attributes.get("xy_color") == (0.14, 0.131)
|
assert state.attributes.get("xy_color") == (0.14, 0.131)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_controlling_state_via_topic_with_value_template(hass, mqtt_mock):
|
||||||
|
"""Test the setting of the state with undocumented value_template."""
|
||||||
|
config = {
|
||||||
|
light.DOMAIN: {
|
||||||
|
"platform": "mqtt",
|
||||||
|
"name": "test",
|
||||||
|
"state_topic": "test_light_rgb/status",
|
||||||
|
"command_topic": "test_light_rgb/set",
|
||||||
|
"value_template": "{{ value_json.hello }}",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert await async_setup_component(hass, light.DOMAIN, config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get("light.test")
|
||||||
|
assert state.state == STATE_OFF
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass, "test_light_rgb/status", '{"hello": "ON"}')
|
||||||
|
|
||||||
|
state = hass.states.get("light.test")
|
||||||
|
assert state.state == STATE_ON
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass, "test_light_rgb/status", '{"hello": "OFF"}')
|
||||||
|
|
||||||
|
state = hass.states.get("light.test")
|
||||||
|
assert state.state == STATE_OFF
|
||||||
|
|
||||||
|
|
||||||
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
|
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
|
||||||
"""Test the sending of command in optimistic mode."""
|
"""Test the sending of command in optimistic mode."""
|
||||||
config = {
|
config = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user