Deprecate old config options for MQTT json light (#111676)

* add deprecaction

* Deprecate old config options for mqtt json light

* Do not deprecate brightness flag

* Enable brightness support by default

* Keep `false` as default for brightness flag

* Add warnings and register issue

* log warning and register on use of color_mode flag

* Remove redundant cv.deprecated logging + follow up comments
This commit is contained in:
Jan Bouwhuis
2024-03-05 08:49:05 +01:00
committed by GitHub
parent 4f9d8d3048
commit 0c2cf881ac
3 changed files with 287 additions and 34 deletions

View File

@ -98,6 +98,7 @@ from homeassistant.const import (
Platform,
)
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers.json import json_dumps
from homeassistant.util.json import JsonValueType, json_loads
from .test_common import (
@ -149,7 +150,6 @@ COLOR_MODES_CONFIG = {
mqtt.DOMAIN: {
light.DOMAIN: {
"brightness": True,
"color_mode": True,
"effect": True,
"command_topic": "test_light_rgb/set",
"name": "test",
@ -217,7 +217,157 @@ async def test_fail_setup_if_color_mode_deprecated(
) -> None:
"""Test if setup fails if color mode is combined with deprecated config keys."""
assert await mqtt_mock_entry()
assert "color_mode must not be combined with any of" in caplog.text
assert "supported_color_modes must not be combined with any of" in caplog.text
@pytest.mark.parametrize(
("hass_config", "color_modes"),
[
(
help_custom_config(light.DOMAIN, DEFAULT_CONFIG, ({"color_temp": True},)),
("color_temp",),
),
(help_custom_config(light.DOMAIN, DEFAULT_CONFIG, ({"hs": True},)), ("hs",)),
(help_custom_config(light.DOMAIN, DEFAULT_CONFIG, ({"rgb": True},)), ("rgb",)),
(help_custom_config(light.DOMAIN, DEFAULT_CONFIG, ({"xy": True},)), ("xy",)),
(
help_custom_config(
light.DOMAIN, DEFAULT_CONFIG, ({"color_temp": True, "rgb": True},)
),
("color_temp, rgb", "rgb, color_temp"),
),
],
ids=["color_temp", "hs", "rgb", "xy", "color_temp, rgb"],
)
async def test_warning_if_color_mode_flags_are_used(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
color_modes: tuple[str,],
) -> None:
"""Test warnings deprecated config keys without supported color modes defined."""
with patch(
"homeassistant.components.mqtt.light.schema_json.async_create_issue"
) as mock_async_create_issue:
assert await mqtt_mock_entry()
assert any(
(
f"Deprecated flags [{color_modes_case}] used in MQTT JSON light config "
"for handling color mode, please use `supported_color_modes` instead."
in caplog.text
)
for color_modes_case in color_modes
)
mock_async_create_issue.assert_called_once()
@pytest.mark.parametrize(
("config", "color_modes"),
[
(
help_custom_config(light.DOMAIN, DEFAULT_CONFIG, ({"color_temp": True},)),
("color_temp",),
),
(help_custom_config(light.DOMAIN, DEFAULT_CONFIG, ({"hs": True},)), ("hs",)),
(help_custom_config(light.DOMAIN, DEFAULT_CONFIG, ({"rgb": True},)), ("rgb",)),
(help_custom_config(light.DOMAIN, DEFAULT_CONFIG, ({"xy": True},)), ("xy",)),
(
help_custom_config(
light.DOMAIN, DEFAULT_CONFIG, ({"color_temp": True, "rgb": True},)
),
("color_temp, rgb", "rgb, color_temp"),
),
],
ids=["color_temp", "hs", "rgb", "xy", "color_temp, rgb"],
)
async def test_warning_on_discovery_if_color_mode_flags_are_used(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
config: dict[str, Any],
color_modes: tuple[str,],
) -> None:
"""Test warnings deprecated config keys with discovery."""
with patch(
"homeassistant.components.mqtt.light.schema_json.async_create_issue"
) as mock_async_create_issue:
assert await mqtt_mock_entry()
config_payload = json_dumps(config[mqtt.DOMAIN][light.DOMAIN][0])
async_fire_mqtt_message(
hass,
"homeassistant/light/bla/config",
config_payload,
)
await hass.async_block_till_done()
assert any(
(
f"Deprecated flags [{color_modes_case}] used in MQTT JSON light config "
"for handling color mode, please "
"use `supported_color_modes` instead" in caplog.text
)
for color_modes_case in color_modes
)
mock_async_create_issue.assert_not_called()
@pytest.mark.parametrize(
"hass_config",
[
help_custom_config(
light.DOMAIN,
DEFAULT_CONFIG,
({"color_mode": True, "supported_color_modes": ["color_temp"]},),
),
],
ids=["color_temp"],
)
async def test_warning_if_color_mode_option_flag_is_used(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test warning deprecated color_mode option flag is used."""
with patch(
"homeassistant.components.mqtt.light.schema_json.async_create_issue"
) as mock_async_create_issue:
assert await mqtt_mock_entry()
assert "Deprecated flag `color_mode` used in MQTT JSON light config" in caplog.text
mock_async_create_issue.assert_called_once()
@pytest.mark.parametrize(
"config",
[
help_custom_config(
light.DOMAIN,
DEFAULT_CONFIG,
({"color_mode": True, "supported_color_modes": ["color_temp"]},),
),
],
ids=["color_temp"],
)
async def test_warning_on_discovery_if_color_mode_option_flag_is_used(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
config: dict[str, Any],
) -> None:
"""Test warning deprecated color_mode option flag is used."""
with patch(
"homeassistant.components.mqtt.light.schema_json.async_create_issue"
) as mock_async_create_issue:
assert await mqtt_mock_entry()
config_payload = json_dumps(config[mqtt.DOMAIN][light.DOMAIN][0])
async_fire_mqtt_message(
hass,
"homeassistant/light/bla/config",
config_payload,
)
await hass.async_block_till_done()
assert "Deprecated flag `color_mode` used in MQTT JSON light config" in caplog.text
mock_async_create_issue.assert_not_called()
@pytest.mark.parametrize(