2018-02-12 07:07:28 +00:00
|
|
|
"""The tests for the MQTT Template light platform.
|
|
|
|
|
|
|
|
Configuration example with all features:
|
|
|
|
|
|
|
|
light:
|
|
|
|
platform: mqtt_template
|
|
|
|
name: mqtt_template_light_1
|
|
|
|
state_topic: 'home/rgb1'
|
|
|
|
command_topic: 'home/rgb1/set'
|
|
|
|
command_on_template: >
|
|
|
|
on,{{ brightness|d }},{{ red|d }}-{{ green|d }}-{{ blue|d }}
|
|
|
|
command_off_template: 'off'
|
|
|
|
state_template: '{{ value.split(",")[0] }}'
|
|
|
|
brightness_template: '{{ value.split(",")[1] }}'
|
|
|
|
color_temp_template: '{{ value.split(",")[2] }}'
|
|
|
|
white_value_template: '{{ value.split(",")[3] }}'
|
|
|
|
red_template: '{{ value.split(",")[4].split("-")[0] }}'
|
|
|
|
green_template: '{{ value.split(",")[4].split("-")[1] }}'
|
|
|
|
blue_template: '{{ value.split(",")[4].split("-")[2] }}'
|
|
|
|
|
|
|
|
If your light doesn't support brightness feature, omit `brightness_template`.
|
|
|
|
|
|
|
|
If your light doesn't support color temp feature, omit `color_temp_template`.
|
|
|
|
|
|
|
|
If your light doesn't support white value feature, omit `white_value_template`.
|
|
|
|
|
|
|
|
If your light doesn't support RGB feature, omit `(red|green|blue)_template`.
|
|
|
|
"""
|
2020-03-09 16:40:00 +00:00
|
|
|
from unittest.mock import patch
|
2018-02-12 07:07:28 +00:00
|
|
|
|
2018-11-27 13:00:05 +00:00
|
|
|
from homeassistant.components import light, mqtt
|
|
|
|
from homeassistant.components.mqtt.discovery import async_start
|
2019-01-27 17:54:52 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ASSUMED_STATE,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
)
|
2018-05-15 10:25:50 +00:00
|
|
|
import homeassistant.core as ha
|
2019-01-27 17:54:52 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2018-09-27 21:13:11 +00:00
|
|
|
|
2020-03-09 16:40:00 +00:00
|
|
|
from .common import (
|
|
|
|
help_test_discovery_broken,
|
|
|
|
help_test_discovery_removal,
|
|
|
|
help_test_discovery_update,
|
|
|
|
help_test_discovery_update_attr,
|
2020-03-12 01:00:47 +00:00
|
|
|
help_test_entity_device_info_remove,
|
2020-03-09 16:40:00 +00:00
|
|
|
help_test_entity_device_info_update,
|
|
|
|
help_test_entity_device_info_with_identifier,
|
|
|
|
help_test_entity_id_update,
|
|
|
|
help_test_setting_attribute_via_mqtt_json_message,
|
|
|
|
help_test_unique_id,
|
|
|
|
help_test_update_with_json_attrs_bad_JSON,
|
|
|
|
help_test_update_with_json_attrs_not_dict,
|
|
|
|
)
|
|
|
|
|
2018-02-12 07:07:28 +00:00
|
|
|
from tests.common import (
|
2019-07-31 19:25:30 +00:00
|
|
|
MockConfigEntry,
|
|
|
|
assert_setup_component,
|
|
|
|
async_fire_mqtt_message,
|
|
|
|
mock_coro,
|
|
|
|
)
|
2018-02-12 07:07:28 +00:00
|
|
|
|
2020-03-11 16:34:19 +00:00
|
|
|
DEFAULT_CONFIG_ATTR = {
|
|
|
|
light.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"schema": "template",
|
|
|
|
"name": "test",
|
|
|
|
"command_topic": "test-topic",
|
|
|
|
"command_on_template": "on,{{ transition }}",
|
|
|
|
"command_off_template": "off,{{ transition|d }}",
|
|
|
|
"json_attributes_topic": "attr-topic",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFAULT_CONFIG_DEVICE_INFO = {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 1",
|
|
|
|
"schema": "template",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"command_topic": "test-command-topic",
|
|
|
|
"command_on_template": "on,{{ transition }}",
|
|
|
|
"command_off_template": "off,{{ transition|d }}",
|
|
|
|
"device": {
|
|
|
|
"identifiers": ["helloworld"],
|
|
|
|
"connections": [["mac", "02:5b:26:a8:dc:12"]],
|
|
|
|
"manufacturer": "Whatever",
|
|
|
|
"name": "Beer",
|
|
|
|
"model": "Glass",
|
|
|
|
"sw_version": "0.1-beta",
|
|
|
|
},
|
|
|
|
"unique_id": "veryunique",
|
|
|
|
}
|
|
|
|
|
2018-02-12 07:07:28 +00:00
|
|
|
|
2018-10-25 08:00:07 +00:00
|
|
|
async def test_setup_fails(hass, mqtt_mock):
|
|
|
|
"""Test that setup fails with missing required configuration items."""
|
|
|
|
with assert_setup_component(0, light.DOMAIN):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
light.DOMAIN,
|
|
|
|
{light.DOMAIN: {"platform": "mqtt", "schema": "template", "name": "test"}},
|
|
|
|
)
|
|
|
|
assert hass.states.get("light.test") is None
|
2018-02-12 07:07:28 +00:00
|
|
|
|
2018-11-27 13:00:05 +00:00
|
|
|
with assert_setup_component(0, light.DOMAIN):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
light.DOMAIN,
|
|
|
|
{
|
|
|
|
light.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"schema": "template",
|
|
|
|
"name": "test",
|
|
|
|
"command_topic": "test_topic",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert hass.states.get("light.test") is None
|
2018-11-27 13:00:05 +00:00
|
|
|
|
|
|
|
with assert_setup_component(0, light.DOMAIN):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
light.DOMAIN,
|
|
|
|
{
|
|
|
|
light.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"schema": "template",
|
|
|
|
"name": "test",
|
|
|
|
"command_topic": "test_topic",
|
|
|
|
"command_on_template": "on",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert hass.states.get("light.test") is None
|
2018-11-27 13:00:05 +00:00
|
|
|
|
|
|
|
with assert_setup_component(0, light.DOMAIN):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
light.DOMAIN,
|
|
|
|
{
|
|
|
|
light.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"schema": "template",
|
|
|
|
"name": "test",
|
|
|
|
"command_topic": "test_topic",
|
|
|
|
"command_off_template": "off",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert hass.states.get("light.test") is None
|
2018-11-27 13:00:05 +00:00
|
|
|
|
2018-02-12 07:07:28 +00:00
|
|
|
|
2018-10-25 08:00:07 +00:00
|
|
|
async def test_state_change_via_topic(hass, mqtt_mock):
|
|
|
|
"""Test state change via topic."""
|
|
|
|
with assert_setup_component(1, light.DOMAIN):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
light.DOMAIN,
|
|
|
|
{
|
|
|
|
light.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"schema": "template",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test_light_rgb",
|
|
|
|
"command_topic": "test_light_rgb/set",
|
|
|
|
"command_on_template": "on,"
|
|
|
|
"{{ brightness|d }},"
|
|
|
|
"{{ color_temp|d }},"
|
|
|
|
"{{ white_value|d }},"
|
|
|
|
"{{ red|d }}-"
|
|
|
|
"{{ green|d }}-"
|
|
|
|
"{{ blue|d }}",
|
|
|
|
"command_off_template": "off",
|
|
|
|
"state_template": '{{ value.split(",")[0] }}',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-02-12 07:07:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_OFF
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("rgb_color") is None
|
|
|
|
assert state.attributes.get("brightness") is None
|
|
|
|
assert state.attributes.get("color_temp") is None
|
|
|
|
assert state.attributes.get("white_value") is None
|
2018-10-25 08:00:07 +00:00
|
|
|
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "on")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_ON
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("rgb_color") is None
|
|
|
|
assert state.attributes.get("brightness") is None
|
|
|
|
assert state.attributes.get("color_temp") is None
|
|
|
|
assert state.attributes.get("white_value") is None
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_state_brightness_color_effect_temp_white_change_via_topic(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, mqtt_mock
|
|
|
|
):
|
2018-10-25 08:00:07 +00:00
|
|
|
"""Test state, bri, color, effect, color temp, white val change."""
|
|
|
|
with assert_setup_component(1, light.DOMAIN):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
light.DOMAIN,
|
|
|
|
{
|
|
|
|
light.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"schema": "template",
|
|
|
|
"name": "test",
|
|
|
|
"effect_list": ["rainbow", "colorloop"],
|
|
|
|
"state_topic": "test_light_rgb",
|
|
|
|
"command_topic": "test_light_rgb/set",
|
|
|
|
"command_on_template": "on,"
|
|
|
|
"{{ brightness|d }},"
|
|
|
|
"{{ color_temp|d }},"
|
|
|
|
"{{ white_value|d }},"
|
|
|
|
"{{ red|d }}-"
|
|
|
|
"{{ green|d }}-"
|
|
|
|
"{{ blue|d }},"
|
|
|
|
"{{ effect|d }}",
|
|
|
|
"command_off_template": "off",
|
|
|
|
"state_template": '{{ value.split(",")[0] }}',
|
|
|
|
"brightness_template": '{{ value.split(",")[1] }}',
|
|
|
|
"color_temp_template": '{{ value.split(",")[2] }}',
|
|
|
|
"white_value_template": '{{ value.split(",")[3] }}',
|
|
|
|
"red_template": '{{ value.split(",")[4].' 'split("-")[0] }}',
|
|
|
|
"green_template": '{{ value.split(",")[4].' 'split("-")[1] }}',
|
|
|
|
"blue_template": '{{ value.split(",")[4].' 'split("-")[2] }}',
|
|
|
|
"effect_template": '{{ value.split(",")[5] }}',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-02-12 07:07:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_OFF
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("rgb_color") is None
|
|
|
|
assert state.attributes.get("brightness") is None
|
|
|
|
assert state.attributes.get("effect") is None
|
|
|
|
assert state.attributes.get("color_temp") is None
|
|
|
|
assert state.attributes.get("white_value") is None
|
2018-10-25 08:00:07 +00:00
|
|
|
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
|
|
|
# turn on the light, full white
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "on,255,145,123,255-128-64,")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_ON
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("rgb_color") == (255, 128, 63)
|
|
|
|
assert state.attributes.get("brightness") == 255
|
|
|
|
assert state.attributes.get("color_temp") == 145
|
|
|
|
assert state.attributes.get("white_value") == 123
|
|
|
|
assert state.attributes.get("effect") is None
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# turn the light off
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "off")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_OFF
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# lower the brightness
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "on,100")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
light_state = hass.states.get("light.test")
|
|
|
|
assert light_state.attributes["brightness"] == 100
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# change the color temp
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "on,,195")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
light_state = hass.states.get("light.test")
|
|
|
|
assert light_state.attributes["color_temp"] == 195
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# change the color
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "on,,,,41-42-43")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
light_state = hass.states.get("light.test")
|
|
|
|
assert light_state.attributes.get("rgb_color") == (243, 249, 255)
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# change the white value
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "on,,,134")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
light_state = hass.states.get("light.test")
|
|
|
|
assert light_state.attributes["white_value"] == 134
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# change the effect
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "on,,,,41-42-43,rainbow")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
light_state = hass.states.get("light.test")
|
|
|
|
assert light_state.attributes.get("effect") == "rainbow"
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_optimistic(hass, mqtt_mock):
|
|
|
|
"""Test optimistic mode."""
|
2019-07-31 19:25:30 +00:00
|
|
|
fake_state = ha.State(
|
|
|
|
"light.test",
|
|
|
|
"on",
|
|
|
|
{
|
|
|
|
"brightness": 95,
|
|
|
|
"hs_color": [100, 100],
|
|
|
|
"effect": "random",
|
|
|
|
"color_temp": 100,
|
|
|
|
"white_value": 50,
|
|
|
|
},
|
|
|
|
)
|
2018-02-12 07:07:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2020-01-02 19:17:10 +00:00
|
|
|
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
2019-07-31 19:25:30 +00:00
|
|
|
return_value=mock_coro(fake_state),
|
|
|
|
):
|
|
|
|
with assert_setup_component(1, light.DOMAIN):
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
light.DOMAIN,
|
|
|
|
{
|
|
|
|
light.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"schema": "template",
|
|
|
|
"name": "test",
|
|
|
|
"command_topic": "test_light_rgb/set",
|
|
|
|
"command_on_template": "on,"
|
|
|
|
"{{ brightness|d }},"
|
|
|
|
"{{ color_temp|d }},"
|
|
|
|
"{{ white_value|d }},"
|
|
|
|
"{{ red|d }}-"
|
|
|
|
"{{ green|d }}-"
|
|
|
|
"{{ blue|d }}",
|
|
|
|
"command_off_template": "off",
|
|
|
|
"effect_list": ["colorloop", "random"],
|
|
|
|
"qos": 2,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_ON
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("brightness") == 95
|
|
|
|
assert state.attributes.get("hs_color") == (100, 100)
|
|
|
|
assert state.attributes.get("effect") == "random"
|
|
|
|
assert state.attributes.get("color_temp") == 100
|
|
|
|
assert state.attributes.get("white_value") == 50
|
2018-10-25 08:00:07 +00:00
|
|
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
2018-02-12 07:07:28 +00:00
|
|
|
|
|
|
|
|
2018-10-25 08:00:07 +00:00
|
|
|
async def test_flash(hass, mqtt_mock):
|
|
|
|
"""Test flash."""
|
|
|
|
with assert_setup_component(1, light.DOMAIN):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
light.DOMAIN,
|
|
|
|
{
|
|
|
|
light.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"schema": "template",
|
|
|
|
"name": "test",
|
|
|
|
"command_topic": "test_light_rgb/set",
|
|
|
|
"command_on_template": "on,{{ flash }}",
|
|
|
|
"command_off_template": "off",
|
|
|
|
"qos": 0,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-02-12 07:07:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_OFF
|
2018-02-12 07:07:28 +00:00
|
|
|
|
|
|
|
|
2018-10-25 08:00:07 +00:00
|
|
|
async def test_transition(hass, mqtt_mock):
|
|
|
|
"""Test for transition time being sent when included."""
|
|
|
|
with assert_setup_component(1, light.DOMAIN):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
light.DOMAIN,
|
|
|
|
{
|
|
|
|
light.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"schema": "template",
|
|
|
|
"name": "test",
|
|
|
|
"command_topic": "test_light_rgb/set",
|
|
|
|
"command_on_template": "on,{{ transition }}",
|
|
|
|
"command_off_template": "off,{{ transition|d }}",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-02-12 07:07:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_OFF
|
2018-02-12 07:07:28 +00:00
|
|
|
|
|
|
|
|
2018-10-25 08:00:07 +00:00
|
|
|
async def test_invalid_values(hass, mqtt_mock):
|
|
|
|
"""Test that invalid values are ignored."""
|
|
|
|
with assert_setup_component(1, light.DOMAIN):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
light.DOMAIN,
|
|
|
|
{
|
|
|
|
light.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"schema": "template",
|
|
|
|
"name": "test",
|
|
|
|
"effect_list": ["rainbow", "colorloop"],
|
|
|
|
"state_topic": "test_light_rgb",
|
|
|
|
"command_topic": "test_light_rgb/set",
|
|
|
|
"command_on_template": "on,"
|
|
|
|
"{{ brightness|d }},"
|
|
|
|
"{{ color_temp|d }},"
|
|
|
|
"{{ red|d }}-"
|
|
|
|
"{{ green|d }}-"
|
|
|
|
"{{ blue|d }},"
|
|
|
|
"{{ effect|d }}",
|
|
|
|
"command_off_template": "off",
|
|
|
|
"state_template": '{{ value.split(",")[0] }}',
|
|
|
|
"brightness_template": '{{ value.split(",")[1] }}',
|
|
|
|
"color_temp_template": '{{ value.split(",")[2] }}',
|
|
|
|
"white_value_template": '{{ value.split(",")[3] }}',
|
|
|
|
"red_template": '{{ value.split(",")[4].' 'split("-")[0] }}',
|
|
|
|
"green_template": '{{ value.split(",")[4].' 'split("-")[1] }}',
|
|
|
|
"blue_template": '{{ value.split(",")[4].' 'split("-")[2] }}',
|
|
|
|
"effect_template": '{{ value.split(",")[5] }}',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-02-12 07:07:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_OFF
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("rgb_color") is None
|
|
|
|
assert state.attributes.get("brightness") is None
|
|
|
|
assert state.attributes.get("color_temp") is None
|
|
|
|
assert state.attributes.get("effect") is None
|
|
|
|
assert state.attributes.get("white_value") is None
|
2018-10-25 08:00:07 +00:00
|
|
|
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
|
|
|
|
|
|
|
# turn on the light, full white
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(
|
|
|
|
hass, "test_light_rgb", "on,255,215,222,255-255-255,rainbow"
|
|
|
|
)
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_ON
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.attributes.get("brightness") == 255
|
|
|
|
assert state.attributes.get("color_temp") == 215
|
|
|
|
assert state.attributes.get("rgb_color") == (255, 255, 255)
|
|
|
|
assert state.attributes.get("white_value") == 222
|
|
|
|
assert state.attributes.get("effect") == "rainbow"
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# bad state value
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "offf")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# state should not have changed
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_ON
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# bad brightness values
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "on,off,255-255-255")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# brightness should not have changed
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
|
|
|
assert state.attributes.get("brightness") == 255
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# bad color temp values
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "on,,off,255-255-255")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# color temp should not have changed
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
|
|
|
assert state.attributes.get("color_temp") == 215
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# bad color values
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "on,255,a-b-c")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# color should not have changed
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
|
|
|
assert state.attributes.get("rgb_color") == (255, 255, 255)
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# bad white value values
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "on,,,off,255-255-255")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# white value should not have changed
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
|
|
|
assert state.attributes.get("white_value") == 222
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# bad effect value
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test_light_rgb", "on,255,a-b-c,white")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
# effect should not have changed
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
|
|
|
assert state.attributes.get("effect") == "rainbow"
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_default_availability_payload(hass, mqtt_mock):
|
|
|
|
"""Test availability by default payload with defined topic."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
light.DOMAIN,
|
|
|
|
{
|
|
|
|
light.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"schema": "template",
|
|
|
|
"name": "test",
|
|
|
|
"command_topic": "test_light_rgb/set",
|
|
|
|
"command_on_template": "on,{{ transition }}",
|
|
|
|
"command_off_template": "off,{{ transition|d }}",
|
|
|
|
"availability_topic": "availability-topic",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_UNAVAILABLE
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability-topic", "online")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state != STATE_UNAVAILABLE
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability-topic", "offline")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_UNAVAILABLE
|
2018-10-25 08:00:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_custom_availability_payload(hass, mqtt_mock):
|
|
|
|
"""Test availability by custom payload with defined topic."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
light.DOMAIN,
|
|
|
|
{
|
|
|
|
light.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"schema": "template",
|
|
|
|
"name": "test",
|
|
|
|
"command_topic": "test_light_rgb/set",
|
|
|
|
"command_on_template": "on,{{ transition }}",
|
|
|
|
"command_off_template": "off,{{ transition|d }}",
|
|
|
|
"availability_topic": "availability-topic",
|
|
|
|
"payload_available": "good",
|
|
|
|
"payload_not_available": "nogood",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_UNAVAILABLE
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability-topic", "good")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state != STATE_UNAVAILABLE
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "availability-topic", "nogood")
|
2018-10-25 08:00:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.test")
|
2019-04-22 12:48:50 +00:00
|
|
|
assert state.state == STATE_UNAVAILABLE
|
2018-11-27 13:00:05 +00:00
|
|
|
|
|
|
|
|
2019-01-17 18:54:22 +00:00
|
|
|
async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
|
|
|
|
"""Test the setting of attribute via MQTT with JSON payload."""
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_setting_attribute_via_mqtt_json_message(
|
2020-03-11 16:34:19 +00:00
|
|
|
hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_ATTR
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-01-17 18:54:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
|
|
|
|
"""Test attributes get extracted from a JSON result."""
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_update_with_json_attrs_not_dict(
|
2020-03-11 16:34:19 +00:00
|
|
|
hass, mqtt_mock, caplog, light.DOMAIN, DEFAULT_CONFIG_ATTR
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-01-17 18:54:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
|
|
|
|
"""Test attributes get extracted from a JSON result."""
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_update_with_json_attrs_bad_JSON(
|
2020-03-11 16:34:19 +00:00
|
|
|
hass, mqtt_mock, caplog, light.DOMAIN, DEFAULT_CONFIG_ATTR
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-01-17 18:54:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_update_attr(hass, mqtt_mock, caplog):
|
|
|
|
"""Test update of discovered MQTTAttributes."""
|
|
|
|
data1 = (
|
2020-03-11 16:34:19 +00:00
|
|
|
'{ "name": "test",'
|
2019-01-17 18:54:22 +00:00
|
|
|
' "schema": "template",'
|
|
|
|
' "command_topic": "test_topic",'
|
|
|
|
' "command_on_template": "on",'
|
|
|
|
' "command_off_template": "off",'
|
|
|
|
' "json_attributes_topic": "attr-topic1" }'
|
|
|
|
)
|
|
|
|
data2 = (
|
2020-03-11 16:34:19 +00:00
|
|
|
'{ "name": "test",'
|
2019-01-17 18:54:22 +00:00
|
|
|
' "schema": "template",'
|
|
|
|
' "command_topic": "test_topic",'
|
|
|
|
' "command_on_template": "on",'
|
|
|
|
' "command_off_template": "off",'
|
|
|
|
' "json_attributes_topic": "attr-topic2" }'
|
|
|
|
)
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_discovery_update_attr(
|
|
|
|
hass, mqtt_mock, caplog, light.DOMAIN, data1, data2
|
|
|
|
)
|
2019-01-17 18:54:22 +00:00
|
|
|
|
|
|
|
|
2018-12-13 14:51:50 +00:00
|
|
|
async def test_unique_id(hass):
|
|
|
|
"""Test unique id option only creates one light per unique_id."""
|
2020-03-09 16:40:00 +00:00
|
|
|
config = {
|
|
|
|
light.DOMAIN: [
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 1",
|
|
|
|
"schema": "template",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"command_topic": "test_topic",
|
|
|
|
"command_on_template": "on,{{ transition }}",
|
|
|
|
"command_off_template": "off,{{ transition|d }}",
|
|
|
|
"unique_id": "TOTALLY_UNIQUE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 2",
|
|
|
|
"schema": "template",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"command_topic": "test_topic",
|
|
|
|
"unique_id": "TOTALLY_UNIQUE",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
await help_test_unique_id(hass, light.DOMAIN, config)
|
2018-12-13 14:51:50 +00:00
|
|
|
|
|
|
|
|
2019-01-25 06:40:52 +00:00
|
|
|
async def test_discovery_removal(hass, mqtt_mock, caplog):
|
2018-11-27 13:00:05 +00:00
|
|
|
"""Test removal of discovered mqtt_json lights."""
|
|
|
|
data = (
|
2020-03-11 16:34:19 +00:00
|
|
|
'{ "name": "test",'
|
2018-11-27 13:00:05 +00:00
|
|
|
' "schema": "template",'
|
|
|
|
' "command_topic": "test_topic",'
|
|
|
|
' "command_on_template": "on",'
|
|
|
|
' "command_off_template": "off"}'
|
|
|
|
)
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_discovery_removal(hass, mqtt_mock, caplog, light.DOMAIN, data)
|
2018-11-27 13:00:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_deprecated(hass, mqtt_mock, caplog):
|
2018-12-02 15:16:36 +00:00
|
|
|
"""Test discovery of mqtt template light with deprecated option."""
|
2018-11-27 13:00:05 +00:00
|
|
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_start(hass, "homeassistant", {"mqtt": {}}, entry)
|
2018-11-27 13:00:05 +00:00
|
|
|
data = (
|
|
|
|
'{ "name": "Beer",'
|
|
|
|
' "platform": "mqtt_template",'
|
|
|
|
' "command_topic": "test_topic",'
|
|
|
|
' "command_on_template": "on",'
|
|
|
|
' "command_off_template": "off"}'
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "homeassistant/light/bla/config", data)
|
2018-11-27 13:00:05 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("light.beer")
|
2018-11-27 13:00:05 +00:00
|
|
|
assert state is not None
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.name == "Beer"
|
2018-12-02 15:16:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_update_light(hass, mqtt_mock, caplog):
|
2019-01-25 06:40:52 +00:00
|
|
|
"""Test update of discovered light."""
|
2018-12-02 15:16:36 +00:00
|
|
|
data1 = (
|
|
|
|
'{ "name": "Beer",'
|
|
|
|
' "schema": "template",'
|
2019-01-29 17:29:02 +00:00
|
|
|
' "state_topic": "test_topic",'
|
2018-12-02 15:16:36 +00:00
|
|
|
' "command_topic": "test_topic",'
|
|
|
|
' "command_on_template": "on",'
|
|
|
|
' "command_off_template": "off"}'
|
|
|
|
)
|
|
|
|
data2 = (
|
|
|
|
'{ "name": "Milk",'
|
|
|
|
' "schema": "template",'
|
2019-01-29 17:29:02 +00:00
|
|
|
' "state_topic": "test_topic",'
|
2018-12-02 15:16:36 +00:00
|
|
|
' "command_topic": "test_topic",'
|
|
|
|
' "command_on_template": "on",'
|
|
|
|
' "command_off_template": "off"}'
|
|
|
|
)
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_discovery_update(
|
|
|
|
hass, mqtt_mock, caplog, light.DOMAIN, data1, data2
|
|
|
|
)
|
2019-01-25 06:40:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_broken(hass, mqtt_mock, caplog):
|
|
|
|
"""Test handling of bad discovery message."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data1 = '{ "name": "Beer" }'
|
2019-01-25 06:40:52 +00:00
|
|
|
data2 = (
|
|
|
|
'{ "name": "Milk",'
|
|
|
|
' "schema": "template",'
|
2019-01-29 17:29:02 +00:00
|
|
|
' "state_topic": "test_topic",'
|
2019-01-25 06:40:52 +00:00
|
|
|
' "command_topic": "test_topic",'
|
|
|
|
' "command_on_template": "on",'
|
|
|
|
' "command_off_template": "off"}'
|
|
|
|
)
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_discovery_broken(
|
|
|
|
hass, mqtt_mock, caplog, light.DOMAIN, data1, data2
|
|
|
|
)
|
2018-12-13 14:51:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
|
|
|
|
"""Test MQTT light device registry integration."""
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_entity_device_info_with_identifier(
|
2020-03-11 16:34:19 +00:00
|
|
|
hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
|
2020-03-09 16:40:00 +00:00
|
|
|
)
|
2018-12-19 13:05:24 +00:00
|
|
|
|
|
|
|
|
2019-01-29 03:45:34 +00:00
|
|
|
async def test_entity_device_info_update(hass, mqtt_mock):
|
|
|
|
"""Test device registry update."""
|
2020-03-11 16:34:19 +00:00
|
|
|
await help_test_entity_device_info_update(
|
|
|
|
hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
|
|
|
|
)
|
2019-01-29 03:45:34 +00:00
|
|
|
|
|
|
|
|
2020-03-12 01:00:47 +00:00
|
|
|
async def test_entity_device_info_remove(hass, mqtt_mock):
|
|
|
|
"""Test device registry remove."""
|
|
|
|
config = {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 1",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"command_topic": "test-command-topic",
|
|
|
|
"device": {"identifiers": ["helloworld"]},
|
|
|
|
"unique_id": "veryunique",
|
|
|
|
}
|
|
|
|
await help_test_entity_device_info_remove(hass, mqtt_mock, light.DOMAIN, config)
|
|
|
|
|
|
|
|
|
2018-12-19 13:05:24 +00:00
|
|
|
async def test_entity_id_update(hass, mqtt_mock):
|
|
|
|
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
2020-03-09 16:40:00 +00:00
|
|
|
config = {
|
|
|
|
light.DOMAIN: [
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "beer",
|
|
|
|
"schema": "template",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"command_topic": "command-topic",
|
|
|
|
"command_on_template": "on,{{ transition }}",
|
|
|
|
"command_off_template": "off,{{ transition|d }}",
|
|
|
|
"availability_topic": "avty-topic",
|
|
|
|
"unique_id": "TOTALLY_UNIQUE",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
await help_test_entity_id_update(hass, mqtt_mock, light.DOMAIN, config)
|