Refactor MQTT tests to use modern platform schema part 2 (#77525)

* Tests light json

* Tests light template

* Missed test light json

* Tests light

* Tests lock

* Tests number

* Tests scene

* Tests select

* Tests sensor

* Tests siren

* Tests state vacuuum

* Tests switch

* Derive DEFAULT_CONFIG_LEGACY from DEFAULT_CONFIG

* Suggested comment changes
This commit is contained in:
Jan Bouwhuis 2022-09-06 11:03:02 +02:00 committed by GitHub
parent a4792998a2
commit 13188a5c63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 1782 additions and 1414 deletions

View File

@ -2,9 +2,9 @@
Configuration for RGB Version with brightness:
mqtt:
light:
platform: mqtt
name: "Office Light RGB"
- name: "Office Light RGB"
state_topic: "office/rgb1/light/status"
command_topic: "office/rgb1/light/switch"
brightness_state_topic: "office/rgb1/brightness/status"
@ -17,8 +17,9 @@ light:
Configuration for XY Version with brightness:
mqtt:
light:
platform: mqtt
- platform: mqtt
name: "Office Light XY"
state_topic: "office/xy1/light/status"
command_topic: "office/xy1/light/switch"
@ -32,9 +33,9 @@ light:
config without RGB:
mqtt:
light:
platform: mqtt
name: "Office Light"
- name: "Office Light"
state_topic: "office/rgb1/light/status"
command_topic: "office/rgb1/light/switch"
brightness_state_topic: "office/rgb1/brightness/status"
@ -45,9 +46,9 @@ light:
config without RGB and brightness:
mqtt:
light:
platform: mqtt
name: "Office Light"
- name: "Office Light"
state_topic: "office/rgb1/light/status"
command_topic: "office/rgb1/light/switch"
qos: 0
@ -56,9 +57,9 @@ light:
config for RGB Version with brightness and scale:
mqtt:
light:
platform: mqtt
name: "Office Light RGB"
- name: "Office Light RGB"
state_topic: "office/rgb1/light/status"
command_topic: "office/rgb1/light/switch"
brightness_state_topic: "office/rgb1/brightness/status"
@ -73,9 +74,9 @@ light:
config with brightness and color temp
mqtt:
light:
platform: mqtt
name: "Office Light Color Temp"
- name: "Office Light Color Temp"
state_topic: "office/rgb1/light/status"
command_topic: "office/rgb1/light/switch"
brightness_state_topic: "office/rgb1/brightness/status"
@ -89,9 +90,9 @@ light:
config with brightness and effect
mqtt:
light:
platform: mqtt
name: "Office Light Color Temp"
- name: "Office Light Color Temp"
state_topic: "office/rgb1/light/status"
command_topic: "office/rgb1/light/switch"
brightness_state_topic: "office/rgb1/brightness/status"
@ -108,9 +109,9 @@ light:
config for RGB Version with RGB command template:
mqtt:
light:
platform: mqtt
name: "Office Light RGB"
- name: "Office Light RGB"
state_topic: "office/rgb1/light/status"
command_topic: "office/rgb1/light/switch"
rgb_state_topic: "office/rgb1/rgb/status"
@ -122,9 +123,9 @@ light:
Configuration for HS Version with brightness:
mqtt:
light:
platform: mqtt
name: "Office Light HS"
- name: "Office Light HS"
state_topic: "office/hs1/light/status"
command_topic: "office/hs1/light/switch"
brightness_state_topic: "office/hs1/brightness/status"
@ -137,9 +138,9 @@ light:
Configuration with brightness command template:
mqtt:
light:
platform: mqtt
name: "Office Light"
- name: "Office Light"
state_topic: "office/rgb1/light/status"
command_topic: "office/rgb1/light/switch"
brightness_state_topic: "office/rgb1/brightness/status"
@ -151,9 +152,9 @@ light:
Configuration with effect command template:
mqtt:
light:
platform: mqtt
name: "Office Light Color Temp"
- name: "Office Light Color Temp"
state_topic: "office/rgb1/light/status"
command_topic: "office/rgb1/light/switch"
effect_state_topic: "office/rgb1/effect/status"
@ -172,7 +173,7 @@ from unittest.mock import call, patch
import pytest
from homeassistant.components import light
from homeassistant.components import light, mqtt
from homeassistant.components.mqtt.light.schema_basic import (
CONF_BRIGHTNESS_COMMAND_TOPIC,
CONF_COLOR_TEMP_COMMAND_TOPIC,
@ -226,17 +227,18 @@ from .test_common import (
help_test_update_with_json_attrs_not_dict,
)
from tests.common import (
assert_setup_component,
async_fire_mqtt_message,
mock_restore_cache,
)
from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.light import common
DEFAULT_CONFIG = {
light.DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"}
mqtt.DOMAIN: {light.DOMAIN: {"name": "test", "command_topic": "test-topic"}}
}
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN])
DEFAULT_CONFIG_LEGACY[light.DOMAIN]["platform"] = mqtt.DOMAIN
@pytest.fixture(autouse=True)
def light_platform_only():
@ -245,14 +247,15 @@ def light_platform_only():
yield
async def test_fail_setup_if_no_command_topic(hass, mqtt_mock_entry_no_yaml_config):
async def test_fail_setup_if_no_command_topic(hass, caplog):
"""Test if command fails with command topic."""
assert await async_setup_component(
hass, light.DOMAIN, {light.DOMAIN: {"platform": "mqtt", "name": "test"}}
assert not await async_setup_component(
hass, mqtt.DOMAIN, {mqtt.DOMAIN: {light.DOMAIN: {"name": "test"}}}
)
assert (
"Invalid config for [mqtt]: required key not provided @ data['mqtt']['light'][0]['command_topic']. Got None."
in caplog.text
)
await hass.async_block_till_done()
await mqtt_mock_entry_no_yaml_config()
assert hass.states.get("light.test") is None
async def test_no_color_brightness_color_temp_hs_white_xy_if_no_topics(
@ -261,14 +264,15 @@ async def test_no_color_brightness_color_temp_hs_white_xy_if_no_topics(
"""Test if there is no color and brightness if no topic."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test_light_rgb/status",
"command_topic": "test_light_rgb/set",
}
}
},
)
await hass.async_block_till_done()
@ -317,7 +321,6 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi
"""Test the controlling of the state via topic."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test_light_rgb/status",
"command_topic": "test_light_rgb/set",
@ -344,7 +347,7 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi
}
color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"]
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -437,7 +440,6 @@ async def test_invalid_state_via_topic(hass, mqtt_mock_entry_with_yaml_config, c
"""Test handling of empty data via topic."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test_light_rgb/status",
"command_topic": "test_light_rgb/set",
@ -464,7 +466,7 @@ async def test_invalid_state_via_topic(hass, mqtt_mock_entry_with_yaml_config, c
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -570,13 +572,12 @@ async def test_invalid_state_via_topic(hass, mqtt_mock_entry_with_yaml_config, c
async def test_brightness_controlling_scale(hass, mqtt_mock_entry_with_yaml_config):
"""Test the brightness controlling scale."""
with assert_setup_component(1, light.DOMAIN):
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test_scale/status",
"command_topic": "test_scale/set",
@ -587,6 +588,7 @@ async def test_brightness_controlling_scale(hass, mqtt_mock_entry_with_yaml_conf
"payload_on": "on",
"payload_off": "off",
}
}
},
)
await hass.async_block_till_done()
@ -620,13 +622,12 @@ async def test_brightness_from_rgb_controlling_scale(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test the brightness controlling scale."""
with assert_setup_component(1, light.DOMAIN):
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test_scale_rgb/status",
"command_topic": "test_scale_rgb/set",
@ -636,6 +637,7 @@ async def test_brightness_from_rgb_controlling_scale(
"payload_on": "on",
"payload_off": "off",
}
}
},
)
await hass.async_block_till_done()
@ -664,7 +666,6 @@ async def test_controlling_state_via_topic_with_templates(
"""Test the setting of the state with a template."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test_light_rgb/status",
"command_topic": "test_light_rgb/set",
@ -697,7 +698,7 @@ async def test_controlling_state_via_topic_with_templates(
}
color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"]
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -767,7 +768,6 @@ async def test_sending_mqtt_commands_and_optimistic(
"""Test the sending of command in optimistic mode."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light_rgb/set",
"brightness_command_topic": "test_light_rgb/brightness/set",
@ -798,8 +798,7 @@ async def test_sending_mqtt_commands_and_optimistic(
)
mock_restore_cache(hass, (fake_state,))
with assert_setup_component(1, light.DOMAIN):
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -958,7 +957,6 @@ async def test_sending_mqtt_rgb_command_with_template(
"""Test the sending of RGB command with template."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light_rgb/set",
"rgb_command_topic": "test_light_rgb/rgb/set",
@ -970,7 +968,7 @@ async def test_sending_mqtt_rgb_command_with_template(
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -998,7 +996,6 @@ async def test_sending_mqtt_rgbw_command_with_template(
"""Test the sending of RGBW command with template."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light_rgb/set",
"rgbw_command_topic": "test_light_rgb/rgbw/set",
@ -1010,7 +1007,7 @@ async def test_sending_mqtt_rgbw_command_with_template(
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1038,7 +1035,6 @@ async def test_sending_mqtt_rgbww_command_with_template(
"""Test the sending of RGBWW command with template."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light_rgb/set",
"rgbww_command_topic": "test_light_rgb/rgbww/set",
@ -1050,7 +1046,7 @@ async def test_sending_mqtt_rgbww_command_with_template(
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1078,7 +1074,6 @@ async def test_sending_mqtt_color_temp_command_with_template(
"""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",
@ -1089,7 +1084,7 @@ async def test_sending_mqtt_color_temp_command_with_template(
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1115,7 +1110,6 @@ async def test_on_command_first(hass, mqtt_mock_entry_with_yaml_config):
"""Test on command being sent before brightness."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light/set",
"brightness_command_topic": "test_light/bright",
@ -1123,7 +1117,7 @@ async def test_on_command_first(hass, mqtt_mock_entry_with_yaml_config):
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1152,14 +1146,13 @@ async def test_on_command_last(hass, mqtt_mock_entry_with_yaml_config):
"""Test on command being sent after brightness."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light/set",
"brightness_command_topic": "test_light/bright",
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1188,7 +1181,6 @@ async def test_on_command_brightness(hass, mqtt_mock_entry_with_yaml_config):
"""Test on command being sent as only brightness."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light/set",
"brightness_command_topic": "test_light/bright",
@ -1197,7 +1189,7 @@ async def test_on_command_brightness(hass, mqtt_mock_entry_with_yaml_config):
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1244,7 +1236,6 @@ async def test_on_command_brightness_scaled(hass, mqtt_mock_entry_with_yaml_conf
"""Test brightness scale."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light/set",
"brightness_command_topic": "test_light/bright",
@ -1254,7 +1245,7 @@ async def test_on_command_brightness_scaled(hass, mqtt_mock_entry_with_yaml_conf
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1315,14 +1306,13 @@ async def test_on_command_rgb(hass, mqtt_mock_entry_with_yaml_config):
"""Test on command in RGB brightness mode."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light/set",
"rgb_command_topic": "test_light/rgb",
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1406,14 +1396,13 @@ async def test_on_command_rgbw(hass, mqtt_mock_entry_with_yaml_config):
"""Test on command in RGBW brightness mode."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light/set",
"rgbw_command_topic": "test_light/rgbw",
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1497,14 +1486,13 @@ async def test_on_command_rgbww(hass, mqtt_mock_entry_with_yaml_config):
"""Test on command in RGBWW brightness mode."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light/set",
"rgbww_command_topic": "test_light/rgbww",
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1588,7 +1576,6 @@ async def test_on_command_rgb_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test on command in RGB brightness mode with RGB template."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light/set",
"rgb_command_topic": "test_light/rgb",
@ -1596,7 +1583,7 @@ async def test_on_command_rgb_template(hass, mqtt_mock_entry_with_yaml_config):
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1626,7 +1613,6 @@ async def test_on_command_rgbw_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test on command in RGBW brightness mode with RGBW template."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light/set",
"rgbw_command_topic": "test_light/rgbw",
@ -1634,7 +1620,7 @@ async def test_on_command_rgbw_template(hass, mqtt_mock_entry_with_yaml_config):
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1663,7 +1649,6 @@ async def test_on_command_rgbww_template(hass, mqtt_mock_entry_with_yaml_config)
"""Test on command in RGBWW brightness mode with RGBWW template."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light/set",
"rgbww_command_topic": "test_light/rgbww",
@ -1671,7 +1656,7 @@ async def test_on_command_rgbww_template(hass, mqtt_mock_entry_with_yaml_config)
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1701,7 +1686,6 @@ async def test_on_command_white(hass, mqtt_mock_entry_with_yaml_config):
"""Test sending commands for RGB + white light."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "tasmota_B94927/cmnd/POWER",
"state_value_template": "{{ value_json.POWER }}",
@ -1721,7 +1705,7 @@ async def test_on_command_white(hass, mqtt_mock_entry_with_yaml_config):
}
color_modes = ["rgb", "white"]
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -1779,7 +1763,6 @@ async def test_explicit_color_mode(hass, mqtt_mock_entry_with_yaml_config):
"""Test explicit color mode over mqtt."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test_light_rgb/status",
"command_topic": "test_light_rgb/set",
@ -1807,7 +1790,7 @@ async def test_explicit_color_mode(hass, mqtt_mock_entry_with_yaml_config):
}
color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"]
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -1928,7 +1911,6 @@ async def test_explicit_color_mode_templated(hass, mqtt_mock_entry_with_yaml_con
"""Test templated explicit color mode over mqtt."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test_light_rgb/status",
"command_topic": "test_light_rgb/set",
@ -1947,7 +1929,7 @@ async def test_explicit_color_mode_templated(hass, mqtt_mock_entry_with_yaml_con
}
color_modes = ["color_temp", "hs"]
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -2010,7 +1992,6 @@ async def test_white_state_update(hass, mqtt_mock_entry_with_yaml_config):
"""Test state updates for RGB + white light."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "tasmota_B94927/tele/STATE",
"command_topic": "tasmota_B94927/cmnd/POWER",
@ -2034,7 +2015,7 @@ async def test_white_state_update(hass, mqtt_mock_entry_with_yaml_config):
}
color_modes = ["rgb", "white"]
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -2075,7 +2056,6 @@ async def test_effect(hass, mqtt_mock_entry_with_yaml_config):
"""Test effect."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light/set",
"effect_command_topic": "test_light/effect/set",
@ -2083,7 +2063,7 @@ async def test_effect(hass, mqtt_mock_entry_with_yaml_config):
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -2114,28 +2094,28 @@ async def test_availability_when_connection_lost(
):
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by custom payload with defined topic."""
await help_test_custom_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -2144,7 +2124,7 @@ async def test_setting_attribute_via_mqtt_json_message(
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -2156,7 +2136,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
hass,
mqtt_mock_entry_no_yaml_config,
light.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
MQTT_LIGHT_ATTRIBUTES_BLOCKED,
)
@ -2164,7 +2144,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -2173,7 +2153,11 @@ async def test_update_with_json_attrs_not_dict(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
hass, mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
light.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -2182,14 +2166,22 @@ async def test_update_with_json_attrs_bad_JSON(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
light.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass, mqtt_mock_entry_no_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_no_yaml_config,
caplog,
light.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -2230,6 +2222,8 @@ async def test_discovery_removal_light(hass, mqtt_mock_entry_no_yaml_config, cap
)
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
async def test_discovery_deprecated(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test discovery of mqtt light with deprecated platform option."""
await mqtt_mock_entry_no_yaml_config()
@ -2750,42 +2744,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT light device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT light device registry integration."""
await help_test_entity_device_info_with_identifier(
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry update."""
await help_test_entity_device_info_update(
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config):
"""Test MQTT subscriptions are managed when entity_id is updated."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -2795,7 +2789,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
hass,
mqtt_mock_entry_no_yaml_config,
light.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
light.SERVICE_TURN_ON,
)
@ -2804,7 +2798,6 @@ async def test_max_mireds(hass, mqtt_mock_entry_with_yaml_config):
"""Test setting min_mireds and max_mireds."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_max_mireds/set",
"color_temp_command_topic": "test_max_mireds/color_temp/set",
@ -2812,7 +2805,7 @@ async def test_max_mireds(hass, mqtt_mock_entry_with_yaml_config):
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -2921,7 +2914,7 @@ async def test_publishing_with_custom_encoding(
):
"""Test publishing MQTT payload with different encoding."""
domain = light.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[domain])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
if topic == "effect_command_topic":
config["effect_list"] = ["random", "color_loop"]
elif topic == "white_command_topic":
@ -2946,7 +2939,7 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
"""Test reloading the MQTT platform."""
domain = light.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@ -2955,7 +2948,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
"""Test reloading the MQTT platform with late entry setup."""
domain = light.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable_late(hass, caplog, tmp_path, domain, config)
@ -3000,7 +2993,7 @@ async def test_encoding_subscribable_topics(
init_payload,
):
"""Test handling of incoming encoded payload."""
config = copy.deepcopy(DEFAULT_CONFIG[light.DOMAIN])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[light.DOMAIN])
config[CONF_EFFECT_COMMAND_TOPIC] = "light/CONF_EFFECT_COMMAND_TOPIC"
config[CONF_RGB_COMMAND_TOPIC] = "light/CONF_RGB_COMMAND_TOPIC"
config[CONF_BRIGHTNESS_COMMAND_TOPIC] = "light/CONF_BRIGHTNESS_COMMAND_TOPIC"
@ -3043,7 +3036,7 @@ async def test_encoding_subscribable_topics_brightness(
init_payload,
):
"""Test handling of incoming encoded payload for a brightness only light."""
config = copy.deepcopy(DEFAULT_CONFIG[light.DOMAIN])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[light.DOMAIN])
config[CONF_BRIGHTNESS_COMMAND_TOPIC] = "light/CONF_BRIGHTNESS_COMMAND_TOPIC"
await help_test_encoding_subscribable_topics(
@ -3066,7 +3059,6 @@ async def test_sending_mqtt_brightness_command_with_template(
"""Test the sending of Brightness command with template."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light_brightness/set",
"brightness_command_topic": "test_light_brightness/brightness/set",
@ -3077,7 +3069,7 @@ async def test_sending_mqtt_brightness_command_with_template(
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -3105,7 +3097,6 @@ async def test_sending_mqtt_effect_command_with_template(
"""Test the sending of Effect command with template."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test_light_brightness/set",
"brightness_command_topic": "test_light_brightness/brightness/set",
@ -3118,7 +3109,7 @@ async def test_sending_mqtt_effect_command_with_template(
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -3147,7 +3138,7 @@ async def test_sending_mqtt_effect_command_with_template(
async def test_setup_manual_entity_from_yaml(hass):
"""Test setup manual configured MQTT entity."""
platform = light.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[platform])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform])
config["name"] = "test"
del config["platform"]
await help_test_setup_manual_entity_from_yaml(hass, platform, config)
@ -3157,7 +3148,20 @@ async def test_setup_manual_entity_from_yaml(hass):
async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path):
"""Test unloading the config entry."""
domain = light.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
)
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config):
"""Test a setup with deprecated yaml platform schema."""
domain = light.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
config["name"] = "test"
assert await async_setup_component(hass, domain, {domain: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert hass.states.get(f"{domain}.test") is not None

View File

@ -2,8 +2,9 @@
Configuration with RGB, brightness, color temp, effect, and XY:
mqtt:
light:
platform: mqtt_json
schema: json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
@ -15,8 +16,9 @@ light:
Configuration with RGB, brightness, color temp and effect:
mqtt:
light:
platform: mqtt_json
schema: json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
@ -27,8 +29,9 @@ light:
Configuration with RGB, brightness and color temp:
mqtt:
light:
platform: mqtt_json
schema: json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
@ -38,8 +41,9 @@ light:
Configuration with RGB, brightness:
mqtt:
light:
platform: mqtt_json
schema: json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
@ -48,8 +52,9 @@ light:
Config without RGB:
mqtt:
light:
platform: mqtt_json
schema: json
name: mqtt_json_light_1
state_topic: "home/rgb1"
command_topic: "home/rgb1/set"
@ -79,7 +84,7 @@ from unittest.mock import call, patch
import pytest
from homeassistant.components import light
from homeassistant.components import light, mqtt
from homeassistant.components.mqtt.light.schema_basic import (
MQTT_LIGHT_ATTRIBUTES_BLOCKED,
)
@ -128,13 +133,19 @@ from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.light import common
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"command_topic": "test-topic",
}
}
}
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN])
DEFAULT_CONFIG_LEGACY[light.DOMAIN]["platform"] = mqtt.DOMAIN
@pytest.fixture(autouse=True)
@ -156,22 +167,21 @@ class JsonValidator:
return json.loads(self.jsondata) == json.loads(other)
async def test_fail_setup_if_no_command_topic(hass, mqtt_mock_entry_no_yaml_config):
async def test_fail_setup_if_no_command_topic(hass, caplog):
"""Test if setup fails with no command topic."""
assert await async_setup_component(
assert not await async_setup_component(
hass,
light.DOMAIN,
{light.DOMAIN: {"platform": "mqtt", "schema": "json", "name": "test"}},
mqtt.DOMAIN,
{mqtt.DOMAIN: {light.DOMAIN: {"schema": "json", "name": "test"}}},
)
assert (
"Invalid config for [mqtt]: required key not provided @ data['mqtt']['light'][0]['command_topic']. Got None."
in caplog.text
)
await hass.async_block_till_done()
await mqtt_mock_entry_no_yaml_config()
assert hass.states.get("light.test") is None
@pytest.mark.parametrize("deprecated", ("color_temp", "hs", "rgb", "xy"))
async def test_fail_setup_if_color_mode_deprecated(
hass, mqtt_mock_entry_no_yaml_config, deprecated
):
async def test_fail_setup_if_color_mode_deprecated(hass, caplog, deprecated):
"""Test if setup fails if color mode is combined with deprecated config keys."""
supported_color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"]
@ -181,27 +191,32 @@ async def test_fail_setup_if_color_mode_deprecated(
"color_mode": True,
"command_topic": "test_light_rgb/set",
"name": "test",
"platform": "mqtt",
"schema": "json",
"supported_color_modes": supported_color_modes,
}
}
config[light.DOMAIN][deprecated] = True
assert await async_setup_component(
assert not await async_setup_component(
hass,
light.DOMAIN,
config,
mqtt.DOMAIN,
{mqtt.DOMAIN: config},
)
assert (
"Invalid config for [mqtt]: color_mode must not be combined with any of"
in caplog.text
)
await hass.async_block_till_done()
await mqtt_mock_entry_no_yaml_config()
assert hass.states.get("light.test") is None
@pytest.mark.parametrize(
"supported_color_modes", [["onoff", "rgb"], ["brightness", "rgb"], ["unknown"]]
"supported_color_modes,error",
[
(["onoff", "rgb"], "Unknown error calling mqtt CONFIG_SCHEMA"),
(["brightness", "rgb"], "Unknown error calling mqtt CONFIG_SCHEMA"),
(["unknown"], "Invalid config for [mqtt]: value must be one of [<ColorMode."),
],
)
async def test_fail_setup_if_color_modes_invalid(
hass, mqtt_mock_entry_no_yaml_config, supported_color_modes
hass, caplog, supported_color_modes, error
):
"""Test if setup fails if supported color modes is invalid."""
config = {
@ -210,34 +225,32 @@ async def test_fail_setup_if_color_modes_invalid(
"color_mode": True,
"command_topic": "test_light_rgb/set",
"name": "test",
"platform": "mqtt",
"schema": "json",
"supported_color_modes": supported_color_modes,
}
}
assert await async_setup_component(
assert not await async_setup_component(
hass,
light.DOMAIN,
config,
mqtt.DOMAIN,
{mqtt.DOMAIN: config},
)
await hass.async_block_till_done()
await mqtt_mock_entry_no_yaml_config()
assert hass.states.get("light.test") is None
assert error in caplog.text
async def test_rgb_light(hass, mqtt_mock_entry_with_yaml_config):
"""Test RGB light flags brightness support."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
"rgb": True,
}
}
},
)
await hass.async_block_till_done()
@ -259,15 +272,16 @@ async def test_no_color_brightness_color_temp_if_no_topics(
"""Test for no RGB, brightness, color temp, effector XY."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"state_topic": "test_light_rgb",
"command_topic": "test_light_rgb/set",
}
}
},
)
await hass.async_block_till_done()
@ -310,10 +324,10 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi
"""Test the controlling of the state via topic."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"state_topic": "test_light_rgb",
@ -326,6 +340,7 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi
"hs": True,
"qos": "0",
}
}
},
)
await hass.async_block_till_done()
@ -434,20 +449,21 @@ async def test_controlling_state_via_topic2(
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"brightness": True,
"color_mode": True,
"command_topic": "test_light_rgb/set",
"effect": True,
"name": "test",
"platform": "mqtt",
"qos": "0",
"schema": "json",
"state_topic": "test_light_rgb",
"supported_color_modes": supported_color_modes,
}
}
},
)
await hass.async_block_till_done()
@ -618,10 +634,10 @@ async def test_sending_mqtt_commands_and_optimistic(
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
@ -633,6 +649,7 @@ async def test_sending_mqtt_commands_and_optimistic(
"xy": True,
"qos": 2,
}
}
},
)
await hass.async_block_till_done()
@ -755,19 +772,20 @@ async def test_sending_mqtt_commands_and_optimistic2(
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"brightness": True,
"color_mode": True,
"command_topic": "test_light_rgb/set",
"effect": True,
"name": "test",
"platform": "mqtt",
"qos": 2,
"schema": "json",
"supported_color_modes": supported_color_modes,
}
}
},
)
await hass.async_block_till_done()
@ -975,16 +993,17 @@ async def test_sending_hs_color(hass, mqtt_mock_entry_with_yaml_config):
"""Test light.turn_on with hs color sends hs color parameters."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
"brightness": True,
"hs": True,
}
}
},
)
await hass.async_block_till_done()
@ -1035,15 +1054,16 @@ async def test_sending_rgb_color_no_brightness(hass, mqtt_mock_entry_with_yaml_c
"""Test light.turn_on with hs color sends rgb color parameters."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
"rgb": True,
}
}
},
)
await hass.async_block_till_done()
@ -1090,16 +1110,17 @@ async def test_sending_rgb_color_no_brightness2(hass, mqtt_mock_entry_with_yaml_
supported_color_modes = ["rgb", "rgbw", "rgbww"]
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"color_mode": True,
"command_topic": "test_light_rgb/set",
"name": "test",
"platform": "mqtt",
"schema": "json",
"supported_color_modes": supported_color_modes,
}
}
},
)
await hass.async_block_till_done()
@ -1169,16 +1190,17 @@ async def test_sending_rgb_color_with_brightness(
"""Test light.turn_on with hs color sends rgb color parameters."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
"brightness": True,
"rgb": True,
}
}
},
)
await hass.async_block_till_done()
@ -1236,10 +1258,10 @@ async def test_sending_rgb_color_with_scaled_brightness(
"""Test light.turn_on with hs color sends rgb color parameters."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
@ -1247,6 +1269,7 @@ async def test_sending_rgb_color_with_scaled_brightness(
"brightness_scale": 100,
"rgb": True,
}
}
},
)
await hass.async_block_till_done()
@ -1302,10 +1325,10 @@ async def test_sending_scaled_white(hass, mqtt_mock_entry_with_yaml_config):
"""Test light.turn_on with scaled white."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
@ -1315,6 +1338,7 @@ async def test_sending_scaled_white(hass, mqtt_mock_entry_with_yaml_config):
"supported_color_modes": ["hs", "white"],
"white_scale": 50,
}
}
},
)
await hass.async_block_till_done()
@ -1346,16 +1370,17 @@ async def test_sending_xy_color(hass, mqtt_mock_entry_with_yaml_config):
"""Test light.turn_on with hs color sends xy color parameters."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
"brightness": True,
"xy": True,
}
}
},
)
await hass.async_block_till_done()
@ -1405,16 +1430,17 @@ async def test_effect(hass, mqtt_mock_entry_with_yaml_config):
"""Test for effect being sent when included."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
"effect": True,
"qos": 0,
}
}
},
)
await hass.async_block_till_done()
@ -1468,10 +1494,10 @@ async def test_flash_short_and_long(hass, mqtt_mock_entry_with_yaml_config):
"""Test for flash length being sent when included."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
@ -1479,6 +1505,7 @@ async def test_flash_short_and_long(hass, mqtt_mock_entry_with_yaml_config):
"flash_time_long": 15,
"qos": 0,
}
}
},
)
await hass.async_block_till_done()
@ -1530,15 +1557,16 @@ async def test_transition(hass, mqtt_mock_entry_with_yaml_config):
"""Test for transition time being sent when included."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
"qos": 0,
}
}
},
)
await hass.async_block_till_done()
@ -1577,10 +1605,10 @@ async def test_brightness_scale(hass, mqtt_mock_entry_with_yaml_config):
"""Test for brightness scaling."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"state_topic": "test_light_bright_scale",
@ -1588,6 +1616,7 @@ async def test_brightness_scale(hass, mqtt_mock_entry_with_yaml_config):
"brightness": True,
"brightness_scale": 99,
}
}
},
)
await hass.async_block_till_done()
@ -1619,10 +1648,10 @@ async def test_white_scale(hass, mqtt_mock_entry_with_yaml_config):
"""Test for white scaling."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"state_topic": "test_light_bright_scale",
@ -1633,6 +1662,7 @@ async def test_white_scale(hass, mqtt_mock_entry_with_yaml_config):
"supported_color_modes": ["hs", "white"],
"white_scale": 50,
}
}
},
)
await hass.async_block_till_done()
@ -1675,10 +1705,10 @@ async def test_invalid_values(hass, mqtt_mock_entry_with_yaml_config):
"""Test that invalid color/brightness/etc. values are ignored."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"state_topic": "test_light_rgb",
@ -1688,6 +1718,7 @@ async def test_invalid_values(hass, mqtt_mock_entry_with_yaml_config):
"rgb": True,
"qos": "0",
}
}
},
)
await hass.async_block_till_done()
@ -1799,28 +1830,28 @@ async def test_availability_when_connection_lost(
):
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by custom payload with defined topic."""
await help_test_custom_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -1829,7 +1860,7 @@ async def test_setting_attribute_via_mqtt_json_message(
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -1841,7 +1872,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
hass,
mqtt_mock_entry_no_yaml_config,
light.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
MQTT_LIGHT_ATTRIBUTES_BLOCKED,
)
@ -1849,7 +1880,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -1858,7 +1889,11 @@ async def test_update_with_json_attrs_not_dict(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
hass, mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
light.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -1867,7 +1902,11 @@ async def test_update_with_json_attrs_bad_JSON(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
light.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -1878,7 +1917,7 @@ async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplo
mqtt_mock_entry_no_yaml_config,
caplog,
light.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
)
@ -1994,7 +2033,7 @@ async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_
hass,
mqtt_mock_entry_no_yaml_config,
light.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
)
@ -2004,7 +2043,7 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_
hass,
mqtt_mock_entry_no_yaml_config,
light.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
)
@ -2014,7 +2053,7 @@ async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
hass,
mqtt_mock_entry_no_yaml_config,
light.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
)
@ -2024,7 +2063,7 @@ async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
hass,
mqtt_mock_entry_no_yaml_config,
light.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
)
@ -2034,7 +2073,7 @@ async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_co
hass,
mqtt_mock_entry_with_yaml_config,
light.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
)
@ -2044,7 +2083,7 @@ async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_c
hass,
mqtt_mock_entry_no_yaml_config,
light.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
)
@ -2054,7 +2093,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
hass,
mqtt_mock_entry_no_yaml_config,
light.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
light.SERVICE_TURN_ON,
command_payload='{"state":"ON"}',
state_payload='{"state":"ON"}',
@ -2065,7 +2104,6 @@ async def test_max_mireds(hass, mqtt_mock_entry_with_yaml_config):
"""Test setting min_mireds and max_mireds."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"schema": "json",
"name": "test",
"command_topic": "test_max_mireds/set",
@ -2074,7 +2112,7 @@ async def test_max_mireds(hass, mqtt_mock_entry_with_yaml_config):
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -2120,7 +2158,7 @@ async def test_publishing_with_custom_encoding(
):
"""Test publishing MQTT payload with different encoding."""
domain = light.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[domain])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
if topic == "effect_command_topic":
config["effect_list"] = ["random", "color_loop"]
@ -2143,7 +2181,7 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
"""Test reloading the MQTT platform."""
domain = light.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@ -2152,7 +2190,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
"""Test reloading the MQTT platform with late entry setup."""
domain = light.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable_late(hass, caplog, tmp_path, domain, config)
@ -2179,7 +2217,7 @@ async def test_encoding_subscribable_topics(
init_payload,
):
"""Test handling of incoming encoded payload."""
config = copy.deepcopy(DEFAULT_CONFIG[light.DOMAIN])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[light.DOMAIN])
config["color_mode"] = True
config["supported_color_modes"] = [
"color_temp",
@ -2207,8 +2245,21 @@ async def test_encoding_subscribable_topics(
async def test_setup_manual_entity_from_yaml(hass):
"""Test setup manual configured MQTT entity."""
platform = light.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[platform])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform])
config["name"] = "test"
del config["platform"]
await help_test_setup_manual_entity_from_yaml(hass, platform, config)
assert hass.states.get(f"{platform}.test") is not None
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config):
"""Test a setup with deprecated yaml platform schema."""
domain = light.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
config["name"] = "test"
assert await async_setup_component(hass, domain, {domain: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert hass.states.get(f"{domain}.test") is not None

View File

@ -2,8 +2,9 @@
Configuration example with all features:
mqtt:
light:
platform: mqtt_template
schema: template
name: mqtt_template_light_1
state_topic: 'home/rgb1'
command_topic: 'home/rgb1/set'
@ -28,7 +29,7 @@ from unittest.mock import patch
import pytest
from homeassistant.components import light
from homeassistant.components import light, mqtt
from homeassistant.components.mqtt.light.schema_basic import (
MQTT_LIGHT_ATTRIBUTES_BLOCKED,
)
@ -74,16 +75,12 @@ from .test_common import (
help_test_update_with_json_attrs_not_dict,
)
from tests.common import (
assert_setup_component,
async_fire_mqtt_message,
mock_restore_cache,
)
from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.light import common
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "template",
"name": "test",
"command_topic": "test-topic",
@ -91,6 +88,12 @@ DEFAULT_CONFIG = {
"command_off_template": "off,{{ transition|d }}",
}
}
}
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN])
DEFAULT_CONFIG_LEGACY[light.DOMAIN]["platform"] = mqtt.DOMAIN
@pytest.fixture(autouse=True)
@ -103,10 +106,9 @@ def light_platform_only():
@pytest.mark.parametrize(
"test_config",
[
({"platform": "mqtt", "schema": "template", "name": "test"},),
({"schema": "template", "name": "test"},),
(
{
"platform": "mqtt",
"schema": "template",
"name": "test",
"command_topic": "test_topic",
@ -114,7 +116,6 @@ def light_platform_only():
),
(
{
"platform": "mqtt",
"schema": "template",
"name": "test",
"command_topic": "test_topic",
@ -123,7 +124,6 @@ def light_platform_only():
),
(
{
"platform": "mqtt",
"schema": "template",
"name": "test",
"command_topic": "test_topic",
@ -132,28 +132,24 @@ def light_platform_only():
),
],
)
async def test_setup_fails(hass, mqtt_mock_entry_no_yaml_config, test_config):
async def test_setup_fails(hass, caplog, test_config):
"""Test that setup fails with missing required configuration items."""
with assert_setup_component(0, light.DOMAIN) as setup_config:
assert await async_setup_component(
assert not await async_setup_component(
hass,
light.DOMAIN,
{light.DOMAIN: test_config},
mqtt.DOMAIN,
{mqtt.DOMAIN: {light.DOMAIN: test_config}},
)
await hass.async_block_till_done()
await mqtt_mock_entry_no_yaml_config()
assert not setup_config[light.DOMAIN]
assert hass.states.get("light.test") is None
assert "Invalid config for [mqtt]" in caplog.text
async def test_rgb_light(hass, mqtt_mock_entry_with_yaml_config):
"""Test RGB light flags brightness support."""
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "template",
"name": "test",
"command_topic": "test_light_rgb/set",
@ -163,6 +159,7 @@ async def test_rgb_light(hass, mqtt_mock_entry_with_yaml_config):
"green_template": '{{ value.split(",")[4].' 'split("-")[1] }}',
"blue_template": '{{ value.split(",")[4].' 'split("-")[2] }}',
}
}
},
)
await hass.async_block_till_done()
@ -181,13 +178,12 @@ async def test_rgb_light(hass, mqtt_mock_entry_with_yaml_config):
async def test_state_change_via_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test state change via topic."""
with assert_setup_component(1, light.DOMAIN):
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "template",
"name": "test",
"state_topic": "test_light_rgb",
@ -201,6 +197,7 @@ async def test_state_change_via_topic(hass, mqtt_mock_entry_with_yaml_config):
"command_off_template": "off",
"state_template": '{{ value.split(",")[0] }}',
}
}
},
)
await hass.async_block_till_done()
@ -236,13 +233,12 @@ async def test_state_brightness_color_effect_temp_change_via_topic(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test state, bri, color, effect, color temp change."""
with assert_setup_component(1, light.DOMAIN):
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "template",
"name": "test",
"effect_list": ["rainbow", "colorloop"],
@ -264,6 +260,7 @@ async def test_state_brightness_color_effect_temp_change_via_topic(
"blue_template": '{{ value.split(",")[3].' 'split("-")[2] }}',
"effect_template": '{{ value.split(",")[4] }}',
}
}
},
)
await hass.async_block_till_done()
@ -340,13 +337,12 @@ async def test_sending_mqtt_commands_and_optimistic(
)
mock_restore_cache(hass, (fake_state,))
with assert_setup_component(1, light.DOMAIN):
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "template",
"name": "test",
"command_topic": "test_light_rgb/set",
@ -369,6 +365,7 @@ async def test_sending_mqtt_commands_and_optimistic(
"effect_template": '{{ value.split(",")[4] }}',
"qos": 2,
}
}
},
)
await hass.async_block_till_done()
@ -470,13 +467,12 @@ async def test_sending_mqtt_commands_non_optimistic_brightness_template(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test the sending of command in optimistic mode."""
with assert_setup_component(1, light.DOMAIN):
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "template",
"name": "test",
"effect_list": ["rainbow", "colorloop"],
@ -499,6 +495,7 @@ async def test_sending_mqtt_commands_non_optimistic_brightness_template(
"blue_template": '{{ value.split(",")[3].' 'split("-")[2] }}',
"effect_template": '{{ value.split(",")[4] }}',
}
}
},
)
await hass.async_block_till_done()
@ -591,13 +588,12 @@ async def test_sending_mqtt_commands_non_optimistic_brightness_template(
async def test_effect(hass, mqtt_mock_entry_with_yaml_config):
"""Test effect sent over MQTT in optimistic mode."""
with assert_setup_component(1, light.DOMAIN):
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "template",
"effect_list": ["rainbow", "colorloop"],
"name": "test",
@ -606,6 +602,7 @@ async def test_effect(hass, mqtt_mock_entry_with_yaml_config):
"command_off_template": "off",
"qos": 0,
}
}
},
)
await hass.async_block_till_done()
@ -644,13 +641,12 @@ async def test_effect(hass, mqtt_mock_entry_with_yaml_config):
async def test_flash(hass, mqtt_mock_entry_with_yaml_config):
"""Test flash sent over MQTT in optimistic mode."""
with assert_setup_component(1, light.DOMAIN):
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "template",
"name": "test",
"command_topic": "test_light_rgb/set",
@ -658,6 +654,7 @@ async def test_flash(hass, mqtt_mock_entry_with_yaml_config):
"command_off_template": "off",
"qos": 0,
}
}
},
)
await hass.async_block_till_done()
@ -693,13 +690,12 @@ async def test_flash(hass, mqtt_mock_entry_with_yaml_config):
async def test_transition(hass, mqtt_mock_entry_with_yaml_config):
"""Test for transition time being sent when included."""
with assert_setup_component(1, light.DOMAIN):
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "template",
"name": "test",
"command_topic": "test_light_rgb/set",
@ -707,6 +703,7 @@ async def test_transition(hass, mqtt_mock_entry_with_yaml_config):
"command_off_template": "off,{{ transition|int|d }}",
"qos": 1,
}
}
},
)
await hass.async_block_till_done()
@ -735,13 +732,12 @@ async def test_transition(hass, mqtt_mock_entry_with_yaml_config):
async def test_invalid_values(hass, mqtt_mock_entry_with_yaml_config):
"""Test that invalid values are ignored."""
with assert_setup_component(1, light.DOMAIN):
assert await async_setup_component(
hass,
light.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
light.DOMAIN: {
"platform": "mqtt",
"schema": "template",
"name": "test",
"effect_list": ["rainbow", "colorloop"],
@ -763,6 +759,7 @@ async def test_invalid_values(hass, mqtt_mock_entry_with_yaml_config):
"blue_template": '{{ value.split(",")[3].' 'split("-")[2] }}',
"effect_template": '{{ value.split(",")[4] }}',
}
}
},
)
await hass.async_block_till_done()
@ -827,28 +824,28 @@ async def test_availability_when_connection_lost(
):
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by custom payload with defined topic."""
await help_test_custom_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -857,7 +854,7 @@ async def test_setting_attribute_via_mqtt_json_message(
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -869,7 +866,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
hass,
mqtt_mock_entry_no_yaml_config,
light.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
MQTT_LIGHT_ATTRIBUTES_BLOCKED,
)
@ -877,7 +874,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -886,7 +883,11 @@ async def test_update_with_json_attrs_not_dict(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
hass, mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
light.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -895,14 +896,22 @@ async def test_update_with_json_attrs_bad_JSON(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock_entry_with_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
light.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass, mqtt_mock_entry_no_yaml_config, caplog, light.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_no_yaml_config,
caplog,
light.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -1017,42 +1026,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT light device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT light device registry integration."""
await help_test_entity_device_info_with_identifier(
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry update."""
await help_test_entity_device_info_update(
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config):
"""Test MQTT subscriptions are managed when entity_id is updated."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, light.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -1082,7 +1091,6 @@ async def test_max_mireds(hass, mqtt_mock_entry_with_yaml_config):
"""Test setting min_mireds and max_mireds."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"schema": "template",
"name": "test",
"command_topic": "test_max_mireds/set",
@ -1093,7 +1101,7 @@ async def test_max_mireds(hass, mqtt_mock_entry_with_yaml_config):
}
}
assert await async_setup_component(hass, light.DOMAIN, config)
assert await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -1139,7 +1147,7 @@ async def test_publishing_with_custom_encoding(
):
"""Test publishing MQTT payload with different encoding."""
domain = light.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[domain])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
if topic == "effect_command_topic":
config["effect_list"] = ["random", "color_loop"]
@ -1162,7 +1170,7 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
"""Test reloading the MQTT platform."""
domain = light.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@ -1171,7 +1179,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
"""Test reloading the MQTT platform with late entry setup."""
domain = light.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable_late(hass, caplog, tmp_path, domain, config)
@ -1192,7 +1200,7 @@ async def test_encoding_subscribable_topics(
init_payload,
):
"""Test handling of incoming encoded payload."""
config = copy.deepcopy(DEFAULT_CONFIG[light.DOMAIN])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[light.DOMAIN])
config["state_template"] = "{{ value }}"
await help_test_encoding_subscribable_topics(
hass,
@ -1211,7 +1219,7 @@ async def test_encoding_subscribable_topics(
async def test_setup_manual_entity_from_yaml(hass):
"""Test setup manual configured MQTT entity."""
platform = light.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[platform])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform])
config["name"] = "test"
del config["platform"]
await help_test_setup_manual_entity_from_yaml(hass, platform, config)
@ -1221,7 +1229,20 @@ async def test_setup_manual_entity_from_yaml(hass):
async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path):
"""Test unloading the config entry."""
domain = light.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
)
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config):
"""Test a setup with deprecated yaml platform schema."""
domain = light.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
config["name"] = "test"
assert await async_setup_component(hass, domain, {domain: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert hass.states.get(f"{domain}.test") is not None

View File

@ -4,14 +4,14 @@ from unittest.mock import patch
import pytest
from homeassistant.components import lock, mqtt
from homeassistant.components.lock import (
DOMAIN as LOCK_DOMAIN,
SERVICE_LOCK,
SERVICE_OPEN,
SERVICE_UNLOCK,
STATE_LOCKED,
STATE_UNLOCKED,
SUPPORT_OPEN,
LockEntityFeature,
)
from homeassistant.components.mqtt.lock import MQTT_LOCK_ATTRIBUTES_BLOCKED
from homeassistant.const import (
@ -56,9 +56,14 @@ from .test_common import (
from tests.common import async_fire_mqtt_message
DEFAULT_CONFIG = {
LOCK_DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"}
mqtt.DOMAIN: {lock.DOMAIN: {"name": "test", "command_topic": "test-topic"}}
}
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN])
DEFAULT_CONFIG_LEGACY[lock.DOMAIN]["platform"] = mqtt.DOMAIN
@pytest.fixture(autouse=True)
def lock_platform_only():
@ -71,10 +76,10 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi
"""Test the controlling state via topic."""
assert await async_setup_component(
hass,
LOCK_DOMAIN,
mqtt.DOMAIN,
{
LOCK_DOMAIN: {
"platform": "mqtt",
mqtt.DOMAIN: {
lock.DOMAIN: {
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
@ -83,6 +88,7 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi
"state_locked": "LOCKED",
"state_unlocked": "UNLOCKED",
}
}
},
)
await hass.async_block_till_done()
@ -110,10 +116,10 @@ async def test_controlling_non_default_state_via_topic(
"""Test the controlling state via topic."""
assert await async_setup_component(
hass,
LOCK_DOMAIN,
mqtt.DOMAIN,
{
LOCK_DOMAIN: {
"platform": "mqtt",
mqtt.DOMAIN: {
lock.DOMAIN: {
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
@ -122,6 +128,7 @@ async def test_controlling_non_default_state_via_topic(
"state_locked": "closed",
"state_unlocked": "open",
}
}
},
)
await hass.async_block_till_done()
@ -148,10 +155,10 @@ async def test_controlling_state_via_topic_and_json_message(
"""Test the controlling state via topic and JSON message."""
assert await async_setup_component(
hass,
LOCK_DOMAIN,
mqtt.DOMAIN,
{
LOCK_DOMAIN: {
"platform": "mqtt",
mqtt.DOMAIN: {
lock.DOMAIN: {
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
@ -161,6 +168,7 @@ async def test_controlling_state_via_topic_and_json_message(
"state_unlocked": "UNLOCKED",
"value_template": "{{ value_json.val }}",
}
}
},
)
await hass.async_block_till_done()
@ -186,10 +194,10 @@ async def test_controlling_non_default_state_via_topic_and_json_message(
"""Test the controlling state via topic and JSON message."""
assert await async_setup_component(
hass,
LOCK_DOMAIN,
mqtt.DOMAIN,
{
LOCK_DOMAIN: {
"platform": "mqtt",
mqtt.DOMAIN: {
lock.DOMAIN: {
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
@ -199,6 +207,7 @@ async def test_controlling_non_default_state_via_topic_and_json_message(
"state_unlocked": "open",
"value_template": "{{ value_json.val }}",
}
}
},
)
await hass.async_block_till_done()
@ -224,10 +233,10 @@ async def test_sending_mqtt_commands_and_optimistic(
"""Test optimistic mode without state topic."""
assert await async_setup_component(
hass,
LOCK_DOMAIN,
mqtt.DOMAIN,
{
LOCK_DOMAIN: {
"platform": "mqtt",
mqtt.DOMAIN: {
lock.DOMAIN: {
"name": "test",
"command_topic": "command-topic",
"payload_lock": "LOCK",
@ -235,6 +244,7 @@ async def test_sending_mqtt_commands_and_optimistic(
"state_locked": "LOCKED",
"state_unlocked": "UNLOCKED",
}
}
},
)
await hass.async_block_till_done()
@ -245,7 +255,7 @@ async def test_sending_mqtt_commands_and_optimistic(
assert state.attributes.get(ATTR_ASSUMED_STATE)
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
lock.DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "LOCK", 0, False)
@ -255,7 +265,7 @@ async def test_sending_mqtt_commands_and_optimistic(
assert state.attributes.get(ATTR_ASSUMED_STATE)
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
lock.DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "UNLOCK", 0, False)
@ -271,10 +281,10 @@ async def test_sending_mqtt_commands_and_explicit_optimistic(
"""Test optimistic mode without state topic."""
assert await async_setup_component(
hass,
LOCK_DOMAIN,
mqtt.DOMAIN,
{
LOCK_DOMAIN: {
"platform": "mqtt",
mqtt.DOMAIN: {
lock.DOMAIN: {
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
@ -284,6 +294,7 @@ async def test_sending_mqtt_commands_and_explicit_optimistic(
"state_unlocked": "UNLOCKED",
"optimistic": True,
}
}
},
)
await hass.async_block_till_done()
@ -294,7 +305,7 @@ async def test_sending_mqtt_commands_and_explicit_optimistic(
assert state.attributes.get(ATTR_ASSUMED_STATE)
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
lock.DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "LOCK", 0, False)
@ -304,7 +315,7 @@ async def test_sending_mqtt_commands_and_explicit_optimistic(
assert state.attributes.get(ATTR_ASSUMED_STATE)
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
lock.DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "UNLOCK", 0, False)
@ -320,10 +331,10 @@ async def test_sending_mqtt_commands_support_open_and_optimistic(
"""Test open function of the lock without state topic."""
assert await async_setup_component(
hass,
LOCK_DOMAIN,
mqtt.DOMAIN,
{
LOCK_DOMAIN: {
"platform": "mqtt",
mqtt.DOMAIN: {
lock.DOMAIN: {
"name": "test",
"command_topic": "command-topic",
"payload_lock": "LOCK",
@ -332,6 +343,7 @@ async def test_sending_mqtt_commands_support_open_and_optimistic(
"state_locked": "LOCKED",
"state_unlocked": "UNLOCKED",
}
}
},
)
await hass.async_block_till_done()
@ -340,10 +352,10 @@ async def test_sending_mqtt_commands_support_open_and_optimistic(
state = hass.states.get("lock.test")
assert state.state is STATE_UNLOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == SUPPORT_OPEN
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == LockEntityFeature.OPEN
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
lock.DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "LOCK", 0, False)
@ -353,7 +365,7 @@ async def test_sending_mqtt_commands_support_open_and_optimistic(
assert state.attributes.get(ATTR_ASSUMED_STATE)
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
lock.DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "UNLOCK", 0, False)
@ -363,7 +375,7 @@ async def test_sending_mqtt_commands_support_open_and_optimistic(
assert state.attributes.get(ATTR_ASSUMED_STATE)
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_OPEN, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
lock.DOMAIN, SERVICE_OPEN, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "OPEN", 0, False)
@ -379,10 +391,10 @@ async def test_sending_mqtt_commands_support_open_and_explicit_optimistic(
"""Test open function of the lock without state topic."""
assert await async_setup_component(
hass,
LOCK_DOMAIN,
mqtt.DOMAIN,
{
LOCK_DOMAIN: {
"platform": "mqtt",
mqtt.DOMAIN: {
lock.DOMAIN: {
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
@ -393,6 +405,7 @@ async def test_sending_mqtt_commands_support_open_and_explicit_optimistic(
"state_unlocked": "UNLOCKED",
"optimistic": True,
}
}
},
)
await hass.async_block_till_done()
@ -401,10 +414,10 @@ async def test_sending_mqtt_commands_support_open_and_explicit_optimistic(
state = hass.states.get("lock.test")
assert state.state is STATE_UNLOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == SUPPORT_OPEN
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == LockEntityFeature.OPEN
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
lock.DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "LOCK", 0, False)
@ -414,7 +427,7 @@ async def test_sending_mqtt_commands_support_open_and_explicit_optimistic(
assert state.attributes.get(ATTR_ASSUMED_STATE)
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
lock.DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "UNLOCK", 0, False)
@ -424,7 +437,7 @@ async def test_sending_mqtt_commands_support_open_and_explicit_optimistic(
assert state.attributes.get(ATTR_ASSUMED_STATE)
await hass.services.async_call(
LOCK_DOMAIN, SERVICE_OPEN, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
lock.DOMAIN, SERVICE_OPEN, {ATTR_ENTITY_ID: "lock.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "OPEN", 0, False)
@ -439,28 +452,28 @@ async def test_availability_when_connection_lost(
):
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by custom payload with defined topic."""
await help_test_custom_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -469,7 +482,7 @@ async def test_setting_attribute_via_mqtt_json_message(
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -480,8 +493,8 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
await help_test_setting_blocked_attribute_via_mqtt_json_message(
hass,
mqtt_mock_entry_no_yaml_config,
LOCK_DOMAIN,
DEFAULT_CONFIG,
lock.DOMAIN,
DEFAULT_CONFIG_LEGACY,
MQTT_LOCK_ATTRIBUTES_BLOCKED,
)
@ -489,7 +502,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -498,7 +511,11 @@ async def test_update_with_json_attrs_not_dict(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
hass, mqtt_mock_entry_with_yaml_config, caplog, LOCK_DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
lock.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -507,21 +524,25 @@ async def test_update_with_json_attrs_bad_json(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock_entry_with_yaml_config, caplog, LOCK_DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
lock.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass, mqtt_mock_entry_no_yaml_config, caplog, LOCK_DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, caplog, lock.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config):
"""Test unique id option only creates one lock per unique_id."""
config = {
LOCK_DOMAIN: [
lock.DOMAIN: [
{
"platform": "mqtt",
"name": "Test 1",
@ -539,7 +560,7 @@ async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config):
]
}
await help_test_unique_id(
hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, config
hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, config
)
@ -547,7 +568,7 @@ async def test_discovery_removal_lock(hass, mqtt_mock_entry_no_yaml_config, capl
"""Test removal of discovered lock."""
data = '{ "name": "test",' ' "command_topic": "test_topic" }'
await help_test_discovery_removal(
hass, mqtt_mock_entry_no_yaml_config, caplog, LOCK_DOMAIN, data
hass, mqtt_mock_entry_no_yaml_config, caplog, lock.DOMAIN, data
)
@ -566,7 +587,7 @@ async def test_discovery_update_lock(hass, mqtt_mock_entry_no_yaml_config, caplo
"availability_topic": "availability_topic2",
}
await help_test_discovery_update(
hass, mqtt_mock_entry_no_yaml_config, caplog, LOCK_DOMAIN, config1, config2
hass, mqtt_mock_entry_no_yaml_config, caplog, lock.DOMAIN, config1, config2
)
@ -586,7 +607,7 @@ async def test_discovery_update_unchanged_lock(
hass,
mqtt_mock_entry_no_yaml_config,
caplog,
LOCK_DOMAIN,
lock.DOMAIN,
data1,
discovery_update,
)
@ -598,49 +619,49 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
data1 = '{ "name": "Beer" }'
data2 = '{ "name": "Milk",' ' "command_topic": "test_topic" }'
await help_test_discovery_broken(
hass, mqtt_mock_entry_no_yaml_config, caplog, LOCK_DOMAIN, data1, data2
hass, mqtt_mock_entry_no_yaml_config, caplog, lock.DOMAIN, data1, data2
)
async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT lock device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock_entry_no_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT lock device registry integration."""
await help_test_entity_device_info_with_identifier(
hass, mqtt_mock_entry_no_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry update."""
await help_test_entity_device_info_update(
hass, mqtt_mock_entry_no_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock_entry_no_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config):
"""Test MQTT subscriptions are managed when entity_id is updated."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock_entry_with_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock_entry_no_yaml_config, LOCK_DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, lock.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -649,8 +670,8 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
await help_test_entity_debug_info_message(
hass,
mqtt_mock_entry_no_yaml_config,
LOCK_DOMAIN,
DEFAULT_CONFIG,
lock.DOMAIN,
DEFAULT_CONFIG_LEGACY,
SERVICE_LOCK,
command_payload="LOCK",
)
@ -679,8 +700,8 @@ async def test_publishing_with_custom_encoding(
template,
):
"""Test publishing MQTT payload with different encoding."""
domain = LOCK_DOMAIN
config = DEFAULT_CONFIG[domain]
domain = lock.DOMAIN
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_publishing_with_custom_encoding(
hass,
@ -698,8 +719,8 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
"""Test reloading the MQTT platform."""
domain = LOCK_DOMAIN
config = DEFAULT_CONFIG[domain]
domain = lock.DOMAIN
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@ -707,8 +728,8 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
"""Test reloading the MQTT platform with late entry setup."""
domain = LOCK_DOMAIN
config = DEFAULT_CONFIG[domain]
domain = lock.DOMAIN
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable_late(hass, caplog, tmp_path, domain, config)
@ -732,8 +753,8 @@ async def test_encoding_subscribable_topics(
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
LOCK_DOMAIN,
DEFAULT_CONFIG[LOCK_DOMAIN],
lock.DOMAIN,
DEFAULT_CONFIG_LEGACY[lock.DOMAIN],
topic,
value,
attribute,
@ -743,8 +764,8 @@ async def test_encoding_subscribable_topics(
async def test_setup_manual_entity_from_yaml(hass, caplog, tmp_path):
"""Test setup manual configured MQTT entity."""
platform = LOCK_DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[platform])
platform = lock.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform])
config["name"] = "test"
del config["platform"]
await help_test_setup_manual_entity_from_yaml(hass, platform, config)
@ -753,8 +774,21 @@ async def test_setup_manual_entity_from_yaml(hass, caplog, tmp_path):
async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path):
"""Test unloading the config entry."""
domain = LOCK_DOMAIN
config = DEFAULT_CONFIG[domain]
domain = lock.DOMAIN
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
)
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config):
"""Test a setup with deprecated yaml platform schema."""
domain = lock.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
config["name"] = "test"
assert await async_setup_component(hass, domain, {domain: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert hass.states.get(f"{domain}.test") is not None

View File

@ -66,9 +66,14 @@ from .test_common import (
from tests.common import async_fire_mqtt_message, mock_restore_cache_with_extra_data
DEFAULT_CONFIG = {
number.DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"}
mqtt.DOMAIN: {number.DOMAIN: {"name": "test", "command_topic": "test-topic"}}
}
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN])
DEFAULT_CONFIG_LEGACY[number.DOMAIN]["platform"] = mqtt.DOMAIN
@pytest.fixture(autouse=True)
def number_platform_only():
@ -82,10 +87,10 @@ async def test_run_number_setup(hass, mqtt_mock_entry_with_yaml_config):
topic = "test/number"
await async_setup_component(
hass,
"number",
mqtt.DOMAIN,
{
"number": {
"platform": "mqtt",
mqtt.DOMAIN: {
number.DOMAIN: {
"state_topic": topic,
"command_topic": topic,
"name": "Test Number",
@ -93,6 +98,7 @@ async def test_run_number_setup(hass, mqtt_mock_entry_with_yaml_config):
"unit_of_measurement": TEMP_FAHRENHEIT,
"payload_reset": "reset!",
}
}
},
)
await hass.async_block_till_done()
@ -131,15 +137,16 @@ async def test_value_template(hass, mqtt_mock_entry_with_yaml_config):
topic = "test/number"
await async_setup_component(
hass,
"number",
mqtt.DOMAIN,
{
"number": {
"platform": "mqtt",
mqtt.DOMAIN: {
number.DOMAIN: {
"state_topic": topic,
"command_topic": topic,
"name": "Test Number",
"value_template": "{{ value_json.val }}",
}
}
},
)
await hass.async_block_till_done()
@ -184,15 +191,16 @@ async def test_restore_native_value(hass, mqtt_mock_entry_with_yaml_config):
)
assert await async_setup_component(
hass,
number.DOMAIN,
mqtt.DOMAIN,
{
"number": {
"platform": "mqtt",
mqtt.DOMAIN: {
number.DOMAIN: {
"command_topic": topic,
"device_class": "temperature",
"unit_of_measurement": TEMP_FAHRENHEIT,
"name": "Test Number",
}
}
},
)
await hass.async_block_till_done()
@ -220,13 +228,14 @@ async def test_run_number_service_optimistic(hass, mqtt_mock_entry_with_yaml_con
)
assert await async_setup_component(
hass,
number.DOMAIN,
mqtt.DOMAIN,
{
"number": {
"platform": "mqtt",
mqtt.DOMAIN: {
number.DOMAIN: {
"command_topic": topic,
"name": "Test Number",
}
}
},
)
await hass.async_block_till_done()
@ -295,14 +304,15 @@ async def test_run_number_service_optimistic_with_command_template(
)
assert await async_setup_component(
hass,
number.DOMAIN,
mqtt.DOMAIN,
{
"number": {
"platform": "mqtt",
mqtt.DOMAIN: {
number.DOMAIN: {
"command_topic": topic,
"name": "Test Number",
"command_template": '{"number": {{ value }} }',
}
}
},
)
await hass.async_block_till_done()
@ -361,14 +371,15 @@ async def test_run_number_service(hass, mqtt_mock_entry_with_yaml_config):
assert await async_setup_component(
hass,
number.DOMAIN,
mqtt.DOMAIN,
{
"number": {
"platform": "mqtt",
mqtt.DOMAIN: {
number.DOMAIN: {
"command_topic": cmd_topic,
"state_topic": state_topic,
"name": "Test Number",
}
}
},
)
await hass.async_block_till_done()
@ -398,15 +409,16 @@ async def test_run_number_service_with_command_template(
assert await async_setup_component(
hass,
number.DOMAIN,
mqtt.DOMAIN,
{
"number": {
"platform": "mqtt",
mqtt.DOMAIN: {
number.DOMAIN: {
"command_topic": cmd_topic,
"state_topic": state_topic,
"name": "Test Number",
"command_template": '{"number": {{ value }} }',
}
}
},
)
await hass.async_block_till_done()
@ -434,28 +446,28 @@ async def test_availability_when_connection_lost(
):
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by custom payload with defined topic."""
await help_test_custom_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -464,7 +476,7 @@ async def test_setting_attribute_via_mqtt_json_message(
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -476,7 +488,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
hass,
mqtt_mock_entry_no_yaml_config,
number.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
MQTT_NUMBER_ATTRIBUTES_BLOCKED,
)
@ -484,7 +496,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -493,7 +505,11 @@ async def test_update_with_json_attrs_not_dict(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
hass, mqtt_mock_entry_with_yaml_config, caplog, number.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
number.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -502,14 +518,22 @@ async def test_update_with_json_attrs_bad_JSON(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock_entry_with_yaml_config, caplog, number.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
number.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass, mqtt_mock_entry_no_yaml_config, caplog, number.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_no_yaml_config,
caplog,
number.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -540,7 +564,7 @@ async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config):
async def test_discovery_removal_number(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test removal of discovered number."""
data = json.dumps(DEFAULT_CONFIG[number.DOMAIN])
data = json.dumps(DEFAULT_CONFIG_LEGACY[number.DOMAIN])
await help_test_discovery_removal(
hass, mqtt_mock_entry_no_yaml_config, caplog, number.DOMAIN, data
)
@ -600,42 +624,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT number device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT number device registry integration."""
await help_test_entity_device_info_with_identifier(
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry update."""
await help_test_entity_device_info_update(
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config):
"""Test MQTT subscriptions are managed when entity_id is updated."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, number.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -645,7 +669,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
hass,
mqtt_mock_entry_no_yaml_config,
number.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
SERVICE_SET_VALUE,
service_parameters={ATTR_VALUE: 45},
command_payload="45",
@ -658,10 +682,10 @@ async def test_min_max_step_attributes(hass, mqtt_mock_entry_with_yaml_config):
topic = "test/number"
await async_setup_component(
hass,
"number",
mqtt.DOMAIN,
{
"number": {
"platform": "mqtt",
mqtt.DOMAIN: {
number.DOMAIN: {
"state_topic": topic,
"command_topic": topic,
"name": "Test Number",
@ -669,6 +693,7 @@ async def test_min_max_step_attributes(hass, mqtt_mock_entry_with_yaml_config):
"max": 110,
"step": 20,
}
}
},
)
await hass.async_block_till_done()
@ -680,25 +705,24 @@ async def test_min_max_step_attributes(hass, mqtt_mock_entry_with_yaml_config):
assert state.attributes.get(ATTR_STEP) == 20
async def test_invalid_min_max_attributes(hass, caplog, mqtt_mock_entry_no_yaml_config):
async def test_invalid_min_max_attributes(hass, caplog):
"""Test invalid min/max attributes."""
topic = "test/number"
await async_setup_component(
assert not await async_setup_component(
hass,
"number",
mqtt.DOMAIN,
{
"number": {
"platform": "mqtt",
mqtt.DOMAIN: {
number.DOMAIN: {
"state_topic": topic,
"command_topic": topic,
"name": "Test Number",
"min": 35,
"max": 10,
}
}
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_no_yaml_config()
assert f"'{CONF_MAX}' must be > '{CONF_MIN}'" in caplog.text
@ -779,16 +803,17 @@ async def test_mqtt_payload_not_a_number_warning(
):
"""Test warning for MQTT payload which is not a number."""
topic = "test/number"
await async_setup_component(
assert await async_setup_component(
hass,
"number",
mqtt.DOMAIN,
{
"number": {
"platform": "mqtt",
mqtt.DOMAIN: {
number.DOMAIN: {
"state_topic": topic,
"command_topic": topic,
"name": "Test Number",
}
}
},
)
await hass.async_block_till_done()
@ -808,16 +833,17 @@ async def test_mqtt_payload_out_of_range_error(
topic = "test/number"
await async_setup_component(
hass,
"number",
mqtt.DOMAIN,
{
"number": {
"platform": "mqtt",
mqtt.DOMAIN: {
number.DOMAIN: {
"state_topic": topic,
"command_topic": topic,
"name": "Test Number",
"min": 5,
"max": 110,
}
}
},
)
await hass.async_block_till_done()
@ -856,7 +882,7 @@ async def test_publishing_with_custom_encoding(
):
"""Test publishing MQTT payload with different encoding."""
domain = NUMBER_DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_publishing_with_custom_encoding(
hass,
@ -875,7 +901,7 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
"""Test reloading the MQTT platform."""
domain = number.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@ -884,7 +910,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
"""Test reloading the MQTT platform with late entry setup."""
domain = number.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable_late(hass, caplog, tmp_path, domain, config)
@ -910,7 +936,7 @@ async def test_encoding_subscribable_topics(
mqtt_mock_entry_with_yaml_config,
caplog,
"number",
DEFAULT_CONFIG["number"],
DEFAULT_CONFIG_LEGACY["number"],
topic,
value,
attribute,
@ -921,7 +947,7 @@ async def test_encoding_subscribable_topics(
async def test_setup_manual_entity_from_yaml(hass):
"""Test setup manual configured MQTT entity."""
platform = number.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[platform])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform])
config["name"] = "test"
del config["platform"]
await help_test_setup_manual_entity_from_yaml(hass, platform, config)
@ -931,7 +957,20 @@ async def test_setup_manual_entity_from_yaml(hass):
async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path):
"""Test unloading the config entry."""
domain = number.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
)
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config):
"""Test a setup with deprecated yaml platform schema."""
domain = number.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
config["name"] = "test"
assert await async_setup_component(hass, domain, {domain: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert hass.states.get(f"{domain}.test") is not None

View File

@ -4,7 +4,7 @@ from unittest.mock import patch
import pytest
from homeassistant.components import scene
from homeassistant.components import mqtt, scene
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON, STATE_UNKNOWN, Platform
import homeassistant.core as ha
from homeassistant.setup import async_setup_component
@ -28,13 +28,19 @@ from .test_common import (
from tests.common import mock_restore_cache
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
scene.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test-topic",
"payload_on": "test-payload-on",
}
}
}
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN])
DEFAULT_CONFIG_LEGACY[scene.DOMAIN]["platform"] = mqtt.DOMAIN
@pytest.fixture(autouse=True)
@ -51,14 +57,15 @@ async def test_sending_mqtt_commands(hass, mqtt_mock_entry_with_yaml_config):
assert await async_setup_component(
hass,
scene.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
scene.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "command-topic",
"payload_on": "beer on",
},
}
},
)
await hass.async_block_till_done()
@ -80,14 +87,14 @@ async def test_availability_when_connection_lost(
):
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config, scene.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, scene.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock_entry_with_yaml_config, scene.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, scene.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -168,8 +175,8 @@ async def test_discovery_removal_scene(hass, mqtt_mock_entry_no_yaml_config, cap
async def test_discovery_update_payload(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test update of discovered scene."""
config1 = copy.deepcopy(DEFAULT_CONFIG[scene.DOMAIN])
config2 = copy.deepcopy(DEFAULT_CONFIG[scene.DOMAIN])
config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[scene.DOMAIN])
config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[scene.DOMAIN])
config1["name"] = "Beer"
config2["name"] = "Milk"
config1["payload_on"] = "ON"
@ -216,7 +223,7 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
"""Test reloading the MQTT platform."""
domain = scene.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@ -225,14 +232,14 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
"""Test reloading the MQTT platform with late entry setup."""
domain = scene.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable_late(hass, caplog, tmp_path, domain, config)
async def test_setup_manual_entity_from_yaml(hass):
"""Test setup manual configured MQTT entity."""
platform = scene.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[platform])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform])
config["name"] = "test"
del config["platform"]
await help_test_setup_manual_entity_from_yaml(hass, platform, config)
@ -242,7 +249,20 @@ async def test_setup_manual_entity_from_yaml(hass):
async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path):
"""Test unloading the config entry."""
domain = scene.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
)
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config):
"""Test a setup with deprecated yaml platform schema."""
domain = scene.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
config["name"] = "test"
assert await async_setup_component(hass, domain, {domain: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert hass.states.get(f"{domain}.test") is not None

View File

@ -5,7 +5,7 @@ from unittest.mock import patch
import pytest
from homeassistant.components import select
from homeassistant.components import mqtt, select
from homeassistant.components.mqtt.select import MQTT_SELECT_ATTRIBUTES_BLOCKED
from homeassistant.components.select import (
ATTR_OPTION,
@ -56,13 +56,19 @@ from .test_common import (
from tests.common import async_fire_mqtt_message, mock_restore_cache
DEFAULT_CONFIG = {
mqtt.DOMAIN: {
select.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test-topic",
"options": ["milk", "beer"],
}
}
}
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN])
DEFAULT_CONFIG_LEGACY[select.DOMAIN]["platform"] = mqtt.DOMAIN
@pytest.fixture(autouse=True)
@ -77,15 +83,16 @@ async def test_run_select_setup(hass, mqtt_mock_entry_with_yaml_config):
topic = "test/select"
await async_setup_component(
hass,
"select",
mqtt.DOMAIN,
{
"select": {
"platform": "mqtt",
mqtt.DOMAIN: {
select.DOMAIN: {
"state_topic": topic,
"command_topic": topic,
"name": "Test Select",
"options": ["milk", "beer"],
}
}
},
)
await hass.async_block_till_done()
@ -111,16 +118,17 @@ async def test_value_template(hass, mqtt_mock_entry_with_yaml_config):
topic = "test/select"
await async_setup_component(
hass,
"select",
mqtt.DOMAIN,
{
"select": {
"platform": "mqtt",
mqtt.DOMAIN: {
select.DOMAIN: {
"state_topic": topic,
"command_topic": topic,
"name": "Test Select",
"options": ["milk", "beer"],
"value_template": "{{ value_json.val }}",
}
}
},
)
await hass.async_block_till_done()
@ -157,14 +165,15 @@ async def test_run_select_service_optimistic(hass, mqtt_mock_entry_with_yaml_con
assert await async_setup_component(
hass,
select.DOMAIN,
mqtt.DOMAIN,
{
"select": {
"platform": "mqtt",
mqtt.DOMAIN: {
select.DOMAIN: {
"command_topic": topic,
"name": "Test Select",
"options": ["milk", "beer"],
}
}
},
)
await hass.async_block_till_done()
@ -198,15 +207,16 @@ async def test_run_select_service_optimistic_with_command_template(
assert await async_setup_component(
hass,
select.DOMAIN,
mqtt.DOMAIN,
{
"select": {
"platform": "mqtt",
mqtt.DOMAIN: {
select.DOMAIN: {
"command_topic": topic,
"name": "Test Select",
"options": ["milk", "beer"],
"command_template": '{"option": "{{ value }}"}',
}
}
},
)
await hass.async_block_till_done()
@ -238,15 +248,16 @@ async def test_run_select_service(hass, mqtt_mock_entry_with_yaml_config):
assert await async_setup_component(
hass,
select.DOMAIN,
mqtt.DOMAIN,
{
"select": {
"platform": "mqtt",
mqtt.DOMAIN: {
select.DOMAIN: {
"command_topic": cmd_topic,
"state_topic": state_topic,
"name": "Test Select",
"options": ["milk", "beer"],
}
}
},
)
await hass.async_block_till_done()
@ -276,16 +287,17 @@ async def test_run_select_service_with_command_template(
assert await async_setup_component(
hass,
select.DOMAIN,
mqtt.DOMAIN,
{
"select": {
"platform": "mqtt",
mqtt.DOMAIN: {
select.DOMAIN: {
"command_topic": cmd_topic,
"state_topic": state_topic,
"name": "Test Select",
"options": ["milk", "beer"],
"command_template": '{"option": "{{ value }}"}',
}
}
},
)
await hass.async_block_till_done()
@ -311,28 +323,28 @@ async def test_availability_when_connection_lost(
):
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by custom payload with defined topic."""
await help_test_custom_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -341,7 +353,7 @@ async def test_setting_attribute_via_mqtt_json_message(
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -353,7 +365,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
hass,
mqtt_mock_entry_no_yaml_config,
select.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
MQTT_SELECT_ATTRIBUTES_BLOCKED,
)
@ -361,7 +373,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -370,7 +382,11 @@ async def test_update_with_json_attrs_not_dict(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
hass, mqtt_mock_entry_with_yaml_config, caplog, select.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
select.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -379,14 +395,22 @@ async def test_update_with_json_attrs_bad_JSON(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock_entry_with_yaml_config, caplog, select.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
select.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass, mqtt_mock_entry_no_yaml_config, caplog, select.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_no_yaml_config,
caplog,
select.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -419,7 +443,7 @@ async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config):
async def test_discovery_removal_select(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test removal of discovered select."""
data = json.dumps(DEFAULT_CONFIG[select.DOMAIN])
data = json.dumps(DEFAULT_CONFIG_LEGACY[select.DOMAIN])
await help_test_discovery_removal(
hass, mqtt_mock_entry_no_yaml_config, caplog, select.DOMAIN, data
)
@ -477,42 +501,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT select device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT select device registry integration."""
await help_test_entity_device_info_with_identifier(
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry update."""
await help_test_entity_device_info_update(
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config):
"""Test MQTT subscriptions are managed when entity_id is updated."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, select.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -522,7 +546,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
hass,
mqtt_mock_entry_no_yaml_config,
select.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
select.SERVICE_SELECT_OPTION,
service_parameters={ATTR_OPTION: "beer"},
command_payload="beer",
@ -536,15 +560,16 @@ async def test_options_attributes(hass, mqtt_mock_entry_with_yaml_config, option
topic = "test/select"
await async_setup_component(
hass,
"select",
mqtt.DOMAIN,
{
"select": {
"platform": "mqtt",
mqtt.DOMAIN: {
select.DOMAIN: {
"state_topic": topic,
"command_topic": topic,
"name": "Test select",
"options": options,
}
}
},
)
await hass.async_block_till_done()
@ -561,15 +586,16 @@ async def test_mqtt_payload_not_an_option_warning(
topic = "test/select"
await async_setup_component(
hass,
"select",
mqtt.DOMAIN,
{
"select": {
"platform": "mqtt",
mqtt.DOMAIN: {
select.DOMAIN: {
"state_topic": topic,
"command_topic": topic,
"name": "Test Select",
"options": ["milk", "beer"],
}
}
},
)
await hass.async_block_till_done()
@ -609,7 +635,7 @@ async def test_publishing_with_custom_encoding(
):
"""Test publishing MQTT payload with different encoding."""
domain = select.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
config["options"] = ["milk", "beer"]
await help_test_publishing_with_custom_encoding(
@ -629,7 +655,7 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
"""Test reloading the MQTT platform."""
domain = select.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@ -638,7 +664,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
"""Test reloading the MQTT platform with late entry setup."""
domain = select.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable_late(hass, caplog, tmp_path, domain, config)
@ -659,7 +685,7 @@ async def test_encoding_subscribable_topics(
attribute_value,
):
"""Test handling of incoming encoded payload."""
config = copy.deepcopy(DEFAULT_CONFIG["select"])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY["select"])
config["options"] = ["milk", "beer"]
await help_test_encoding_subscribable_topics(
hass,
@ -677,7 +703,7 @@ async def test_encoding_subscribable_topics(
async def test_setup_manual_entity_from_yaml(hass):
"""Test setup manual configured MQTT entity."""
platform = select.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[platform])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform])
config["name"] = "test"
del config["platform"]
await help_test_setup_manual_entity_from_yaml(hass, platform, config)
@ -687,7 +713,20 @@ async def test_setup_manual_entity_from_yaml(hass):
async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path):
"""Test unloading the config entry."""
domain = select.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
)
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config):
"""Test a setup with deprecated yaml platform schema."""
domain = select.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
config["name"] = "test"
assert await async_setup_component(hass, domain, {domain: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert hass.states.get(f"{domain}.test") is not None

View File

@ -6,8 +6,8 @@ from unittest.mock import MagicMock, patch
import pytest
from homeassistant.components import mqtt, sensor
from homeassistant.components.mqtt.sensor import MQTT_SENSOR_ATTRIBUTES_BLOCKED
import homeassistant.components.sensor as sensor
from homeassistant.const import (
EVENT_STATE_CHANGED,
STATE_UNAVAILABLE,
@ -64,16 +64,20 @@ from .test_common import (
)
from tests.common import (
assert_setup_component,
async_fire_mqtt_message,
async_fire_time_changed,
mock_restore_cache_with_extra_data,
)
DEFAULT_CONFIG = {
sensor.DOMAIN: {"platform": "mqtt", "name": "test", "state_topic": "test-topic"}
mqtt.DOMAIN: {sensor.DOMAIN: {"name": "test", "state_topic": "test-topic"}}
}
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN])
DEFAULT_CONFIG_LEGACY[sensor.DOMAIN]["platform"] = mqtt.DOMAIN
@pytest.fixture(autouse=True)
def sensor_platform_only():
@ -88,14 +92,15 @@ async def test_setting_sensor_value_via_mqtt_message(
"""Test the setting of the value via MQTT."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test-topic",
"unit_of_measurement": "fav unit",
}
}
},
)
await hass.async_block_till_done()
@ -146,14 +151,15 @@ async def test_setting_sensor_native_value_handling_via_mqtt_message(
"""Test the setting of the value via MQTT."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test-topic",
"device_class": device_class,
}
}
},
)
await hass.async_block_till_done()
@ -173,16 +179,17 @@ async def test_setting_sensor_value_expires_availability_topic(
"""Test the expiration of the value."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test-topic",
"expire_after": 4,
"force_update": True,
"availability_topic": "availability-topic",
}
}
},
)
await hass.async_block_till_done()
@ -206,16 +213,17 @@ async def test_setting_sensor_value_expires(
"""Test the expiration of the value."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test-topic",
"unit_of_measurement": "fav unit",
"expire_after": "4",
"force_update": True,
}
}
},
)
await hass.async_block_till_done()
@ -285,15 +293,16 @@ async def test_setting_sensor_value_via_mqtt_json_message(
"""Test the setting of the value via MQTT with JSON payload."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test-topic",
"unit_of_measurement": "fav unit",
"value_template": "{{ value_json.val }}",
}
}
},
)
await hass.async_block_till_done()
@ -311,15 +320,16 @@ async def test_setting_sensor_value_via_mqtt_json_message_and_default_current_st
"""Test the setting of the value via MQTT with fall back to current state."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test-topic",
"unit_of_measurement": "fav unit",
"value_template": "{{ value_json.val | is_defined }}-{{ value_json.par }}",
}
}
},
)
await hass.async_block_till_done()
@ -344,16 +354,17 @@ async def test_setting_sensor_last_reset_via_mqtt_message(
"""Test the setting of the last_reset property via MQTT."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_class": "total",
"state_topic": "test-topic",
"unit_of_measurement": "fav unit",
"last_reset_topic": "last-reset-topic",
}
}
},
)
await hass.async_block_till_done()
@ -376,16 +387,17 @@ async def test_setting_sensor_bad_last_reset_via_mqtt_message(
"""Test the setting of the last_reset property via MQTT."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_class": "total",
"state_topic": "test-topic",
"unit_of_measurement": "fav unit",
"last_reset_topic": "last-reset-topic",
}
}
},
)
await hass.async_block_till_done()
@ -403,16 +415,17 @@ async def test_setting_sensor_empty_last_reset_via_mqtt_message(
"""Test the setting of the last_reset property via MQTT."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_class": "total",
"state_topic": "test-topic",
"unit_of_measurement": "fav unit",
"last_reset_topic": "last-reset-topic",
}
}
},
)
await hass.async_block_till_done()
@ -430,10 +443,10 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message(
"""Test the setting of the value via MQTT with JSON payload."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_class": "total",
"state_topic": "test-topic",
@ -441,6 +454,7 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message(
"last_reset_topic": "last-reset-topic",
"last_reset_value_template": "{{ value_json.last_reset }}",
}
}
},
)
await hass.async_block_till_done()
@ -460,11 +474,11 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message_2(
"""Test the setting of the value via MQTT with JSON payload."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
**{
"platform": "mqtt",
"name": "test",
"state_class": "total",
"state_topic": "test-topic",
@ -474,6 +488,7 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message_2(
},
**extra,
}
}
},
)
await hass.async_block_till_done()
@ -498,14 +513,15 @@ async def test_force_update_disabled(hass, mqtt_mock_entry_with_yaml_config):
"""Test force update option."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test-topic",
"unit_of_measurement": "fav unit",
}
}
},
)
await hass.async_block_till_done()
@ -532,15 +548,16 @@ async def test_force_update_enabled(hass, mqtt_mock_entry_with_yaml_config):
"""Test force update option."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test-topic",
"unit_of_measurement": "fav unit",
"force_update": True,
}
}
},
)
await hass.async_block_till_done()
@ -568,21 +585,21 @@ async def test_availability_when_connection_lost(
):
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -591,7 +608,7 @@ async def test_default_availability_list_payload(
):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_list_payload(
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -600,7 +617,7 @@ async def test_default_availability_list_payload_all(
):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_list_payload_all(
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -609,7 +626,7 @@ async def test_default_availability_list_payload_any(
):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_list_payload_any(
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -618,21 +635,25 @@ async def test_default_availability_list_single(
):
"""Test availability list and availability_topic are mutually exclusive."""
await help_test_default_availability_list_single(
hass, mqtt_mock_entry_no_yaml_config, caplog, sensor.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_no_yaml_config,
caplog,
sensor.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by custom payload with defined topic."""
await help_test_custom_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_discovery_update_availability(hass, mqtt_mock_entry_no_yaml_config):
"""Test availability discovery update."""
await help_test_discovery_update_availability(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -642,12 +663,13 @@ async def test_invalid_device_class(hass, mqtt_mock_entry_no_yaml_config):
hass,
sensor.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test-topic",
"device_class": "foobarnotreal",
}
}
},
)
await hass.async_block_till_done()
@ -661,17 +683,18 @@ async def test_valid_device_class(hass, mqtt_mock_entry_with_yaml_config):
"""Test device_class option with valid values."""
assert await async_setup_component(
hass,
"sensor",
mqtt.DOMAIN,
{
"sensor": [
mqtt.DOMAIN: {
sensor.DOMAIN: [
{
"platform": "mqtt",
"name": "Test 1",
"state_topic": "test-topic",
"device_class": "temperature",
},
{"platform": "mqtt", "name": "Test 2", "state_topic": "test-topic"},
{"name": "Test 2", "state_topic": "test-topic"},
]
}
},
)
await hass.async_block_till_done()
@ -689,12 +712,13 @@ async def test_invalid_state_class(hass, mqtt_mock_entry_no_yaml_config):
hass,
sensor.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test-topic",
"state_class": "foobarnotreal",
}
}
},
)
await hass.async_block_till_done()
@ -708,17 +732,18 @@ async def test_valid_state_class(hass, mqtt_mock_entry_with_yaml_config):
"""Test state_class option with valid values."""
assert await async_setup_component(
hass,
"sensor",
mqtt.DOMAIN,
{
"sensor": [
mqtt.DOMAIN: {
sensor.DOMAIN: [
{
"platform": "mqtt",
"name": "Test 1",
"state_topic": "test-topic",
"state_class": "measurement",
},
{"platform": "mqtt", "name": "Test 2", "state_topic": "test-topic"},
{"name": "Test 2", "state_topic": "test-topic"},
]
}
},
)
await hass.async_block_till_done()
@ -735,7 +760,7 @@ async def test_setting_attribute_via_mqtt_json_message(
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -747,7 +772,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
hass,
mqtt_mock_entry_no_yaml_config,
sensor.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
MQTT_SENSOR_ATTRIBUTES_BLOCKED,
)
@ -755,7 +780,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -764,7 +789,11 @@ async def test_update_with_json_attrs_not_dict(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
hass, mqtt_mock_entry_with_yaml_config, caplog, sensor.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
sensor.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -773,14 +802,22 @@ async def test_update_with_json_attrs_bad_JSON(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock_entry_with_yaml_config, caplog, sensor.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
sensor.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass, mqtt_mock_entry_no_yaml_config, caplog, sensor.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_no_yaml_config,
caplog,
sensor.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -914,42 +951,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT sensor device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT sensor device registry integration."""
await help_test_entity_device_info_with_identifier(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry update."""
await help_test_entity_device_info_update(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config):
"""Test MQTT subscriptions are managed when entity_id is updated."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -967,7 +1004,6 @@ async def test_entity_device_info_with_hub(hass, mqtt_mock_entry_no_yaml_config)
data = json.dumps(
{
"platform": "mqtt",
"name": "Test 1",
"state_topic": "test-topic",
"device": {"identifiers": ["helloworld"], "via_device": "hub-id"},
@ -985,42 +1021,42 @@ async def test_entity_device_info_with_hub(hass, mqtt_mock_entry_no_yaml_config)
async def test_entity_debug_info(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT sensor debug info."""
await help_test_entity_debug_info(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_debug_info_max_messages(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT sensor debug info."""
await help_test_entity_debug_info_max_messages(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT debug info."""
await help_test_entity_debug_info_message(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG, None
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY, None
)
async def test_entity_debug_info_remove(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT sensor debug info."""
await help_test_entity_debug_info_remove(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_debug_info_update_entity_id(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT sensor debug info."""
await help_test_entity_debug_info_update_entity_id(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_disabled_by_default(hass, mqtt_mock_entry_no_yaml_config):
"""Test entity disabled by default."""
await help_test_entity_disabled_by_default(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -1028,7 +1064,7 @@ async def test_entity_disabled_by_default(hass, mqtt_mock_entry_no_yaml_config):
async def test_entity_category(hass, mqtt_mock_entry_no_yaml_config):
"""Test entity category."""
await help_test_entity_category(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -1036,10 +1072,10 @@ async def test_value_template_with_entity_id(hass, mqtt_mock_entry_with_yaml_con
"""Test the access to attributes in value_template via the entity_id."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test-topic",
"unit_of_measurement": "fav unit",
@ -1050,6 +1086,7 @@ async def test_value_template_with_entity_id(hass, mqtt_mock_entry_with_yaml_con
{{ value }} \
{% endif %}',
}
}
},
)
await hass.async_block_till_done()
@ -1064,7 +1101,7 @@ async def test_value_template_with_entity_id(hass, mqtt_mock_entry_with_yaml_con
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
"""Test reloading the MQTT platform."""
domain = sensor.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@ -1073,7 +1110,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
"""Test reloading the MQTT platform with late entry setup."""
domain = sensor.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable_late(hass, caplog, tmp_path, domain, config)
@ -1082,14 +1119,14 @@ async def test_cleanup_triggers_and_restoring_state(
):
"""Test cleanup old triggers at reloading and restoring the state."""
domain = sensor.DOMAIN
config1 = copy.deepcopy(DEFAULT_CONFIG[domain])
config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain])
config1["name"] = "test1"
config1["expire_after"] = 30
config1["state_topic"] = "test-topic1"
config1["device_class"] = "temperature"
config1["unit_of_measurement"] = TEMP_FAHRENHEIT
config2 = copy.deepcopy(DEFAULT_CONFIG[domain])
config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain])
config2["name"] = "test2"
config2["expire_after"] = 5
config2["state_topic"] = "test-topic2"
@ -1100,8 +1137,8 @@ async def test_cleanup_triggers_and_restoring_state(
assert await async_setup_component(
hass,
domain,
{domain: [config1, config2]},
mqtt.DOMAIN,
{mqtt.DOMAIN: {domain: [config1, config2]}},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -1116,7 +1153,7 @@ async def test_cleanup_triggers_and_restoring_state(
freezer.move_to("2022-02-02 12:01:10+01:00")
await help_test_reload_with_config(
hass, caplog, tmp_path, {domain: [config1, config2]}
hass, caplog, tmp_path, {mqtt.DOMAIN: {domain: [config1, config2]}}
)
await hass.async_block_till_done()
@ -1150,7 +1187,7 @@ async def test_skip_restoring_state_with_over_due_expire_trigger(
freezer.move_to("2022-02-02 12:02:00+01:00")
domain = sensor.DOMAIN
config3 = copy.deepcopy(DEFAULT_CONFIG[domain])
config3 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain])
config3["name"] = "test3"
config3["expire_after"] = 10
config3["state_topic"] = "test-topic3"
@ -1163,8 +1200,9 @@ async def test_skip_restoring_state_with_over_due_expire_trigger(
fake_extra_data = MagicMock()
mock_restore_cache_with_extra_data(hass, ((fake_state, fake_extra_data),))
with assert_setup_component(1, domain):
assert await async_setup_component(hass, domain, {domain: config3})
assert await async_setup_component(
hass, mqtt.DOMAIN, {mqtt.DOMAIN: {domain: config3}}
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert "Skip state recovery after reload for sensor.test3" in caplog.text
@ -1192,7 +1230,7 @@ async def test_encoding_subscribable_topics(
mqtt_mock_entry_with_yaml_config,
caplog,
sensor.DOMAIN,
DEFAULT_CONFIG[sensor.DOMAIN],
DEFAULT_CONFIG_LEGACY[sensor.DOMAIN],
topic,
value,
attribute,
@ -1204,7 +1242,7 @@ async def test_encoding_subscribable_topics(
async def test_setup_manual_entity_from_yaml(hass):
"""Test setup manual configured MQTT entity."""
platform = sensor.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[platform])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform])
config["name"] = "test"
del config["platform"]
await help_test_setup_manual_entity_from_yaml(hass, platform, config)
@ -1214,7 +1252,20 @@ async def test_setup_manual_entity_from_yaml(hass):
async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path):
"""Test unloading the config entry."""
domain = sensor.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
)
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config):
"""Test a setup with deprecated yaml platform schema."""
domain = sensor.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
config["name"] = "test"
assert await async_setup_component(hass, domain, {domain: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert hass.states.get(f"{domain}.test") is not None

View File

@ -4,7 +4,7 @@ from unittest.mock import patch
import pytest
from homeassistant.components import siren
from homeassistant.components import mqtt, siren
from homeassistant.components.siren.const import ATTR_VOLUME_LEVEL
from homeassistant.const import (
ATTR_ASSUMED_STATE,
@ -53,9 +53,14 @@ from .test_common import (
from tests.common import async_fire_mqtt_message
DEFAULT_CONFIG = {
siren.DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"}
mqtt.DOMAIN: {siren.DOMAIN: {"name": "test", "command_topic": "test-topic"}}
}
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN])
DEFAULT_CONFIG_LEGACY[siren.DOMAIN]["platform"] = mqtt.DOMAIN
@pytest.fixture(autouse=True)
def siren_platform_only():
@ -83,16 +88,17 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi
"""Test the controlling state via topic."""
assert await async_setup_component(
hass,
siren.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
siren.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
"payload_on": 1,
"payload_off": 0,
}
}
},
)
await hass.async_block_till_done()
@ -119,16 +125,17 @@ async def test_sending_mqtt_commands_and_optimistic(
"""Test the sending MQTT commands in optimistic mode."""
assert await async_setup_component(
hass,
siren.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
siren.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "command-topic",
"payload_on": "beer on",
"payload_off": "beer off",
"qos": "2",
}
}
},
)
await hass.async_block_till_done()
@ -162,10 +169,10 @@ async def test_controlling_state_via_topic_and_json_message(
"""Test the controlling state via topic and JSON message."""
assert await async_setup_component(
hass,
siren.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
siren.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
@ -173,6 +180,7 @@ async def test_controlling_state_via_topic_and_json_message(
"payload_off": "beer off",
"state_value_template": "{{ value_json.val }}",
}
}
},
)
await hass.async_block_till_done()
@ -202,10 +210,10 @@ async def test_controlling_state_and_attributes_with_json_message_without_templa
"""Test the controlling state via topic and JSON message without a value template."""
assert await async_setup_component(
hass,
siren.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
siren.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
@ -213,6 +221,7 @@ async def test_controlling_state_and_attributes_with_json_message_without_templa
"payload_off": "beer off",
"available_tones": ["ping", "siren", "bell"],
}
}
},
)
await hass.async_block_till_done()
@ -284,7 +293,6 @@ async def test_filtering_not_supported_attributes_optimistic(
):
"""Test setting attributes with support flags optimistic."""
config = {
"platform": "mqtt",
"command_topic": "command-topic",
"available_tones": ["ping", "siren", "bell"],
}
@ -300,8 +308,8 @@ async def test_filtering_not_supported_attributes_optimistic(
assert await async_setup_component(
hass,
siren.DOMAIN,
{siren.DOMAIN: [config1, config2, config3]},
mqtt.DOMAIN,
{mqtt.DOMAIN: {siren.DOMAIN: [config1, config2, config3]}},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -370,7 +378,6 @@ async def test_filtering_not_supported_attributes_via_state(
):
"""Test setting attributes with support flags via state."""
config = {
"platform": "mqtt",
"command_topic": "command-topic",
"available_tones": ["ping", "siren", "bell"],
}
@ -389,8 +396,8 @@ async def test_filtering_not_supported_attributes_via_state(
assert await async_setup_component(
hass,
siren.DOMAIN,
{siren.DOMAIN: [config1, config2, config3]},
mqtt.DOMAIN,
{mqtt.DOMAIN: {siren.DOMAIN: [config1, config2, config3]}},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -450,14 +457,14 @@ async def test_availability_when_connection_lost(
):
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -513,10 +520,10 @@ async def test_custom_state_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test the state payload."""
assert await async_setup_component(
hass,
siren.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
siren.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
@ -525,6 +532,7 @@ async def test_custom_state_payload(hass, mqtt_mock_entry_with_yaml_config):
"state_on": "HIGH",
"state_off": "LOW",
}
}
},
)
await hass.async_block_till_done()
@ -550,7 +558,7 @@ async def test_setting_attribute_via_mqtt_json_message(
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -559,14 +567,14 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_blocked_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG, {}
hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY, {}
)
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -575,7 +583,11 @@ async def test_update_with_json_attrs_not_dict(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
hass, mqtt_mock_entry_with_yaml_config, caplog, siren.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
siren.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -584,14 +596,22 @@ async def test_update_with_json_attrs_bad_JSON(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock_entry_with_yaml_config, caplog, siren.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
siren.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass, mqtt_mock_entry_no_yaml_config, caplog, siren.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_no_yaml_config,
caplog,
siren.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -636,8 +656,8 @@ async def test_discovery_update_siren_topic_template(
hass, mqtt_mock_entry_no_yaml_config, caplog
):
"""Test update of discovered siren."""
config1 = copy.deepcopy(DEFAULT_CONFIG[siren.DOMAIN])
config2 = copy.deepcopy(DEFAULT_CONFIG[siren.DOMAIN])
config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[siren.DOMAIN])
config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[siren.DOMAIN])
config1["name"] = "Beer"
config2["name"] = "Milk"
config1["state_topic"] = "siren/state1"
@ -673,8 +693,8 @@ async def test_discovery_update_siren_template(
hass, mqtt_mock_entry_no_yaml_config, caplog
):
"""Test update of discovered siren."""
config1 = copy.deepcopy(DEFAULT_CONFIG[siren.DOMAIN])
config2 = copy.deepcopy(DEFAULT_CONFIG[siren.DOMAIN])
config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[siren.DOMAIN])
config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[siren.DOMAIN])
config1["name"] = "Beer"
config2["name"] = "Milk"
config1["state_topic"] = "siren/state1"
@ -706,7 +726,7 @@ async def test_discovery_update_siren_template(
async def test_command_templates(hass, mqtt_mock_entry_with_yaml_config, caplog):
"""Test siren with command templates optimistic."""
config1 = copy.deepcopy(DEFAULT_CONFIG[siren.DOMAIN])
config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][siren.DOMAIN])
config1["name"] = "Beer"
config1["available_tones"] = ["ping", "chimes"]
config1[
@ -719,8 +739,8 @@ async def test_command_templates(hass, mqtt_mock_entry_with_yaml_config, caplog)
assert await async_setup_component(
hass,
siren.DOMAIN,
{siren.DOMAIN: [config1, config2]},
mqtt.DOMAIN,
{mqtt.DOMAIN: {siren.DOMAIN: [config1, config2]}},
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -824,42 +844,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT siren device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT siren device registry integration."""
await help_test_entity_device_info_with_identifier(
hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry update."""
await help_test_entity_device_info_update(
hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config):
"""Test MQTT subscriptions are managed when entity_id is updated."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, siren.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -869,7 +889,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
hass,
mqtt_mock_entry_no_yaml_config,
siren.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
siren.SERVICE_TURN_ON,
command_payload='{"state":"ON"}',
)
@ -906,7 +926,7 @@ async def test_publishing_with_custom_encoding(
):
"""Test publishing MQTT payload with command templates and different encoding."""
domain = siren.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[domain])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
config[siren.ATTR_AVAILABLE_TONES] = ["siren", "xylophone"]
await help_test_publishing_with_custom_encoding(
@ -926,7 +946,7 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
"""Test reloading the MQTT platform."""
domain = siren.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@ -935,7 +955,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
"""Test reloading the MQTT platform with late entry setup."""
domain = siren.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable_late(hass, caplog, tmp_path, domain, config)
@ -960,7 +980,7 @@ async def test_encoding_subscribable_topics(
mqtt_mock_entry_with_yaml_config,
caplog,
siren.DOMAIN,
DEFAULT_CONFIG[siren.DOMAIN],
DEFAULT_CONFIG_LEGACY[siren.DOMAIN],
topic,
value,
attribute,
@ -971,7 +991,7 @@ async def test_encoding_subscribable_topics(
async def test_setup_manual_entity_from_yaml(hass):
"""Test setup manual configured MQTT entity."""
platform = siren.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[platform])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform])
config["name"] = "test"
del config["platform"]
await help_test_setup_manual_entity_from_yaml(hass, platform, config)
@ -981,7 +1001,20 @@ async def test_setup_manual_entity_from_yaml(hass):
async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path):
"""Test unloading the config entry."""
domain = siren.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
)
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config):
"""Test a setup with deprecated yaml platform schema."""
domain = siren.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
config["name"] = "test"
assert await async_setup_component(hass, domain, {domain: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert hass.states.get(f"{domain}.test") is not None

View File

@ -5,7 +5,7 @@ from unittest.mock import patch
import pytest
from homeassistant.components import vacuum
from homeassistant.components import mqtt, vacuum
from homeassistant.components.mqtt.const import CONF_COMMAND_TOPIC, CONF_STATE_TOPIC
from homeassistant.components.mqtt.vacuum import CONF_SCHEMA, schema_state as mqttvacuum
from homeassistant.components.mqtt.vacuum.const import MQTT_VACUUM_ATTRIBUTES_BLOCKED
@ -73,7 +73,8 @@ SEND_COMMAND_TOPIC = "vacuum/send_command"
STATE_TOPIC = "vacuum/state"
DEFAULT_CONFIG = {
CONF_PLATFORM: "mqtt",
mqtt.DOMAIN: {
vacuum.DOMAIN: {
CONF_SCHEMA: "state",
CONF_NAME: "mqtttest",
CONF_COMMAND_TOPIC: COMMAND_TOPIC,
@ -82,10 +83,17 @@ DEFAULT_CONFIG = {
mqttvacuum.CONF_SET_FAN_SPEED_TOPIC: "vacuum/set_fan_speed",
mqttvacuum.CONF_FAN_SPEED_LIST: ["min", "medium", "high", "max"],
}
DEFAULT_CONFIG_2 = {
vacuum.DOMAIN: {"platform": "mqtt", "schema": "state", "name": "test"}
}
}
DEFAULT_CONFIG_2 = {mqtt.DOMAIN: {vacuum.DOMAIN: {"schema": "state", "name": "test"}}}
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
DEFAULT_CONFIG_LEGACY = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN])
DEFAULT_CONFIG_LEGACY[vacuum.DOMAIN][CONF_PLATFORM] = mqtt.DOMAIN
DEFAULT_CONFIG_2_LEGACY = deepcopy(DEFAULT_CONFIG_2[mqtt.DOMAIN])
DEFAULT_CONFIG_2_LEGACY[vacuum.DOMAIN][CONF_PLATFORM] = mqtt.DOMAIN
@pytest.fixture(autouse=True)
@ -97,9 +105,7 @@ def vacuum_platform_only():
async def test_default_supported_features(hass, mqtt_mock_entry_with_yaml_config):
"""Test that the correct supported features."""
assert await async_setup_component(
hass, vacuum.DOMAIN, {vacuum.DOMAIN: DEFAULT_CONFIG}
)
assert await async_setup_component(hass, mqtt.DOMAIN, DEFAULT_CONFIG)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
entity = hass.states.get("vacuum.mqtttest")
@ -111,12 +117,14 @@ async def test_default_supported_features(hass, mqtt_mock_entry_with_yaml_config
async def test_all_commands(hass, mqtt_mock_entry_with_yaml_config):
"""Test simple commands send to the vacuum."""
config = deepcopy(DEFAULT_CONFIG)
config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN])
config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings(
mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING
)
assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config})
assert await async_setup_component(
hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}}
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -185,13 +193,15 @@ async def test_commands_without_supported_features(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test commands which are not supported by the vacuum."""
config = deepcopy(DEFAULT_CONFIG)
config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN])
services = mqttvacuum.STRING_TO_SERVICE["status"]
config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings(
services, SERVICE_TO_STRING
)
assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config})
assert await async_setup_component(
hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}}
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
@ -243,12 +253,14 @@ async def test_commands_without_supported_features(
async def test_status(hass, mqtt_mock_entry_with_yaml_config):
"""Test status updates from the vacuum."""
config = deepcopy(DEFAULT_CONFIG)
config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN])
config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings(
mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING
)
assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config})
assert await async_setup_component(
hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}}
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("vacuum.mqtttest")
@ -288,13 +300,15 @@ async def test_status(hass, mqtt_mock_entry_with_yaml_config):
async def test_no_fan_vacuum(hass, mqtt_mock_entry_with_yaml_config):
"""Test status updates from the vacuum when fan is not supported."""
config = deepcopy(DEFAULT_CONFIG)
config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN])
del config[mqttvacuum.CONF_FAN_SPEED_LIST]
config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings(
mqttvacuum.DEFAULT_SERVICES, SERVICE_TO_STRING
)
assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config})
assert await async_setup_component(
hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}}
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -340,12 +354,14 @@ async def test_no_fan_vacuum(hass, mqtt_mock_entry_with_yaml_config):
@pytest.mark.no_fail_on_log_exception
async def test_status_invalid_json(hass, mqtt_mock_entry_with_yaml_config):
"""Test to make sure nothing breaks if the vacuum sends bad JSON."""
config = deepcopy(DEFAULT_CONFIG)
config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN])
config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings(
mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING
)
assert await async_setup_component(hass, vacuum.DOMAIN, {vacuum.DOMAIN: config})
assert await async_setup_component(
hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}}
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@ -359,28 +375,28 @@ async def test_availability_when_connection_lost(
):
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY
)
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY
)
async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY
)
async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by custom payload with defined topic."""
await help_test_custom_availability_payload(
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY
)
@ -389,7 +405,7 @@ async def test_setting_attribute_via_mqtt_json_message(
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY
)
@ -401,7 +417,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
hass,
mqtt_mock_entry_no_yaml_config,
vacuum.DOMAIN,
DEFAULT_CONFIG_2,
DEFAULT_CONFIG_2_LEGACY,
MQTT_VACUUM_ATTRIBUTES_BLOCKED,
)
@ -409,7 +425,7 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY
)
@ -418,7 +434,11 @@ async def test_update_with_json_attrs_not_dict(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
hass, mqtt_mock_entry_with_yaml_config, caplog, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
vacuum.DOMAIN,
DEFAULT_CONFIG_2_LEGACY,
)
@ -427,14 +447,22 @@ async def test_update_with_json_attrs_bad_json(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock_entry_with_yaml_config, caplog, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
vacuum.DOMAIN,
DEFAULT_CONFIG_2_LEGACY,
)
async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass, mqtt_mock_entry_no_yaml_config, caplog, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass,
mqtt_mock_entry_no_yaml_config,
caplog,
vacuum.DOMAIN,
DEFAULT_CONFIG_2_LEGACY,
)
@ -511,42 +539,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT vacuum device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY
)
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT vacuum device registry integration."""
await help_test_entity_device_info_with_identifier(
hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY
)
async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry update."""
await help_test_entity_device_info_update(
hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY
)
async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY
)
async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config):
"""Test MQTT subscriptions are managed when entity_id is updated."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass, mqtt_mock_entry_with_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY
)
async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2
hass, mqtt_mock_entry_no_yaml_config, vacuum.DOMAIN, DEFAULT_CONFIG_2_LEGACY
)
@ -556,7 +584,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
hass,
mqtt_mock_entry_no_yaml_config,
vacuum.DOMAIN,
DEFAULT_CONFIG_2,
DEFAULT_CONFIG_2_LEGACY,
vacuum.SERVICE_START,
command_payload="start",
state_payload="{}",
@ -615,7 +643,7 @@ async def test_publishing_with_custom_encoding(
):
"""Test publishing MQTT payload with different encoding."""
domain = vacuum.DOMAIN
config = deepcopy(DEFAULT_CONFIG)
config = deepcopy(DEFAULT_CONFIG_LEGACY[domain])
config["supported_features"] = [
"battery",
"clean_spot",
@ -646,7 +674,7 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
"""Test reloading the MQTT platform."""
domain = vacuum.DOMAIN
config = DEFAULT_CONFIG
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@ -655,7 +683,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
"""Test reloading the MQTT platform with late entry setup."""
domain = vacuum.DOMAIN
config = DEFAULT_CONFIG
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable_late(hass, caplog, tmp_path, domain, config)
@ -691,7 +719,7 @@ async def test_encoding_subscribable_topics(
mqtt_mock_entry_with_yaml_config,
caplog,
vacuum.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY[vacuum.DOMAIN],
topic,
value,
attribute,
@ -703,8 +731,21 @@ async def test_encoding_subscribable_topics(
async def test_setup_manual_entity_from_yaml(hass):
"""Test setup manual configured MQTT entity."""
platform = vacuum.DOMAIN
config = deepcopy(DEFAULT_CONFIG)
config = deepcopy(DEFAULT_CONFIG_LEGACY[platform])
config["name"] = "test"
del config["platform"]
await help_test_setup_manual_entity_from_yaml(hass, platform, config)
assert hass.states.get(f"{platform}.test") is not None
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config):
"""Test a setup with deprecated yaml platform schema."""
domain = vacuum.DOMAIN
config = deepcopy(DEFAULT_CONFIG_LEGACY[domain])
config["name"] = "test"
assert await async_setup_component(hass, domain, {domain: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert hass.states.get(f"{domain}.test") is not None

View File

@ -4,7 +4,7 @@ from unittest.mock import patch
import pytest
from homeassistant.components import switch
from homeassistant.components import mqtt, switch
from homeassistant.const import (
ATTR_ASSUMED_STATE,
ATTR_DEVICE_CLASS,
@ -51,9 +51,14 @@ from tests.common import async_fire_mqtt_message, mock_restore_cache
from tests.components.switch import common
DEFAULT_CONFIG = {
switch.DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"}
mqtt.DOMAIN: {switch.DOMAIN: {"name": "test", "command_topic": "test-topic"}}
}
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN])
DEFAULT_CONFIG_LEGACY[switch.DOMAIN]["platform"] = mqtt.DOMAIN
@pytest.fixture(autouse=True)
def switch_platform_only():
@ -66,10 +71,10 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi
"""Test the controlling state via topic."""
assert await async_setup_component(
hass,
switch.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
switch.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
@ -77,6 +82,7 @@ async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_confi
"payload_off": 0,
"device_class": "switch",
}
}
},
)
await hass.async_block_till_done()
@ -112,16 +118,17 @@ async def test_sending_mqtt_commands_and_optimistic(
assert await async_setup_component(
hass,
switch.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
switch.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "command-topic",
"payload_on": "beer on",
"payload_off": "beer off",
"qos": "2",
}
}
},
)
await hass.async_block_till_done()
@ -155,13 +162,14 @@ async def test_sending_inital_state_and_optimistic(
"""Test the initial state in optimistic mode."""
assert await async_setup_component(
hass,
switch.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
switch.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "command-topic",
}
}
},
)
await hass.async_block_till_done()
@ -178,10 +186,10 @@ async def test_controlling_state_via_topic_and_json_message(
"""Test the controlling state via topic and JSON message."""
assert await async_setup_component(
hass,
switch.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
switch.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
@ -189,6 +197,7 @@ async def test_controlling_state_via_topic_and_json_message(
"payload_off": "beer off",
"value_template": "{{ value_json.val }}",
}
}
},
)
await hass.async_block_till_done()
@ -218,14 +227,14 @@ async def test_availability_when_connection_lost(
):
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -281,10 +290,10 @@ async def test_custom_state_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test the state payload."""
assert await async_setup_component(
hass,
switch.DOMAIN,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
switch.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "state-topic",
"command_topic": "command-topic",
@ -293,6 +302,7 @@ async def test_custom_state_payload(hass, mqtt_mock_entry_with_yaml_config):
"state_on": "HIGH",
"state_off": "LOW",
}
}
},
)
await hass.async_block_till_done()
@ -318,7 +328,7 @@ async def test_setting_attribute_via_mqtt_json_message(
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -327,14 +337,14 @@ async def test_setting_blocked_attribute_via_mqtt_json_message(
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_blocked_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG, {}
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY, {}
)
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -343,7 +353,11 @@ async def test_update_with_json_attrs_not_dict(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
hass, mqtt_mock_entry_with_yaml_config, caplog, switch.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
switch.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -352,14 +366,22 @@ async def test_update_with_json_attrs_bad_JSON(
):
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock_entry_with_yaml_config, caplog, switch.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
switch.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass, mqtt_mock_entry_no_yaml_config, caplog, switch.DOMAIN, DEFAULT_CONFIG
hass,
mqtt_mock_entry_no_yaml_config,
caplog,
switch.DOMAIN,
DEFAULT_CONFIG_LEGACY,
)
@ -404,8 +426,8 @@ async def test_discovery_update_switch_topic_template(
hass, mqtt_mock_entry_no_yaml_config, caplog
):
"""Test update of discovered switch."""
config1 = copy.deepcopy(DEFAULT_CONFIG[switch.DOMAIN])
config2 = copy.deepcopy(DEFAULT_CONFIG[switch.DOMAIN])
config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN])
config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN])
config1["name"] = "Beer"
config2["name"] = "Milk"
config1["state_topic"] = "switch/state1"
@ -441,8 +463,8 @@ async def test_discovery_update_switch_template(
hass, mqtt_mock_entry_no_yaml_config, caplog
):
"""Test update of discovered switch."""
config1 = copy.deepcopy(DEFAULT_CONFIG[switch.DOMAIN])
config2 = copy.deepcopy(DEFAULT_CONFIG[switch.DOMAIN])
config1 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN])
config2 = copy.deepcopy(DEFAULT_CONFIG_LEGACY[switch.DOMAIN])
config1["name"] = "Beer"
config2["name"] = "Milk"
config1["state_topic"] = "switch/state1"
@ -512,42 +534,42 @@ async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT switch device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT switch device registry integration."""
await help_test_entity_device_info_with_identifier(
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry update."""
await help_test_entity_device_info_update(
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config):
"""Test MQTT subscriptions are managed when entity_id is updated."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
)
async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, switch.DOMAIN, DEFAULT_CONFIG_LEGACY
)
@ -557,7 +579,7 @@ async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
hass,
mqtt_mock_entry_no_yaml_config,
switch.DOMAIN,
DEFAULT_CONFIG,
DEFAULT_CONFIG_LEGACY,
switch.SERVICE_TURN_ON,
)
@ -593,7 +615,7 @@ async def test_publishing_with_custom_encoding(
):
"""Test publishing MQTT payload with different encoding."""
domain = switch.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_publishing_with_custom_encoding(
hass,
@ -612,7 +634,7 @@ async def test_publishing_with_custom_encoding(
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
"""Test reloading the MQTT platform."""
domain = switch.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
@ -621,7 +643,7 @@ async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_pa
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
"""Test reloading the MQTT platform with late entry setup."""
domain = switch.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_reloadable_late(hass, caplog, tmp_path, domain, config)
@ -646,7 +668,7 @@ async def test_encoding_subscribable_topics(
mqtt_mock_entry_with_yaml_config,
caplog,
switch.DOMAIN,
DEFAULT_CONFIG[switch.DOMAIN],
DEFAULT_CONFIG_LEGACY[switch.DOMAIN],
topic,
value,
attribute,
@ -657,7 +679,7 @@ async def test_encoding_subscribable_topics(
async def test_setup_manual_entity_from_yaml(hass):
"""Test setup manual configured MQTT entity."""
platform = switch.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[platform])
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[platform])
config["name"] = "test"
del config["platform"]
await help_test_setup_manual_entity_from_yaml(hass, platform, config)
@ -667,7 +689,20 @@ async def test_setup_manual_entity_from_yaml(hass):
async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path):
"""Test unloading the config entry."""
domain = switch.DOMAIN
config = DEFAULT_CONFIG[domain]
config = DEFAULT_CONFIG_LEGACY[domain]
await help_test_unload_config_entry_with_platform(
hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config
)
# Test deprecated YAML configuration under the platform key
# Scheduled to be removed in HA core 2022.12
async def test_setup_with_legacy_schema(hass, mqtt_mock_entry_with_yaml_config):
"""Test a setup with deprecated yaml platform schema."""
domain = switch.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG_LEGACY[domain])
config["name"] = "test"
assert await async_setup_component(hass, domain, {domain: config})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert hass.states.get(f"{domain}.test") is not None