From a7523777ba61246e20bdc1ad9065415cf9ea9f13 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 6 Apr 2021 12:39:29 +0200 Subject: [PATCH] Flag brightness support for MQTT RGB lights (#48718) --- .../components/mqtt/light/schema_json.py | 4 ++- .../components/mqtt/light/schema_template.py | 2 +- tests/components/mqtt/test_light.py | 29 ++++++++++++++++- tests/components/mqtt/test_light_json.py | 27 ++++++++++++++++ tests/components/mqtt/test_light_template.py | 31 +++++++++++++++++++ 5 files changed, 90 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/mqtt/light/schema_json.py b/homeassistant/components/mqtt/light/schema_json.py index 4d56435643a..8be3708bd61 100644 --- a/homeassistant/components/mqtt/light/schema_json.py +++ b/homeassistant/components/mqtt/light/schema_json.py @@ -197,7 +197,9 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity): self._supported_features |= config[CONF_BRIGHTNESS] and SUPPORT_BRIGHTNESS self._supported_features |= config[CONF_COLOR_TEMP] and SUPPORT_COLOR_TEMP self._supported_features |= config[CONF_HS] and SUPPORT_COLOR - self._supported_features |= config[CONF_RGB] and SUPPORT_COLOR + self._supported_features |= config[CONF_RGB] and ( + SUPPORT_COLOR | SUPPORT_BRIGHTNESS + ) self._supported_features |= config[CONF_WHITE_VALUE] and SUPPORT_WHITE_VALUE self._supported_features |= config[CONF_XY] and SUPPORT_COLOR diff --git a/homeassistant/components/mqtt/light/schema_template.py b/homeassistant/components/mqtt/light/schema_template.py index 118746f2229..7c0266265db 100644 --- a/homeassistant/components/mqtt/light/schema_template.py +++ b/homeassistant/components/mqtt/light/schema_template.py @@ -417,7 +417,7 @@ class MqttLightTemplate(MqttEntity, LightEntity, RestoreEntity): and self._templates[CONF_GREEN_TEMPLATE] is not None and self._templates[CONF_BLUE_TEMPLATE] is not None ): - features = features | SUPPORT_COLOR + features = features | SUPPORT_COLOR | SUPPORT_BRIGHTNESS if self._config.get(CONF_EFFECT_LIST) is not None: features = features | SUPPORT_EFFECT if self._templates[CONF_COLOR_TEMP_TEMPLATE] is not None: diff --git a/tests/components/mqtt/test_light.py b/tests/components/mqtt/test_light.py index 00ff8b28b77..e995b373d03 100644 --- a/tests/components/mqtt/test_light.py +++ b/tests/components/mqtt/test_light.py @@ -161,7 +161,13 @@ import pytest from homeassistant import config as hass_config from homeassistant.components import light -from homeassistant.const import ATTR_ASSUMED_STATE, SERVICE_RELOAD, STATE_OFF, STATE_ON +from homeassistant.const import ( + ATTR_ASSUMED_STATE, + ATTR_SUPPORTED_FEATURES, + SERVICE_RELOAD, + STATE_OFF, + STATE_ON, +) import homeassistant.core as ha from homeassistant.setup import async_setup_component @@ -206,6 +212,27 @@ async def test_fail_setup_if_no_command_topic(hass, mqtt_mock): assert hass.states.get("light.test") is None +async def test_rgb_light(hass, mqtt_mock): + """Test RGB light flags brightness support.""" + assert await async_setup_component( + hass, + light.DOMAIN, + { + light.DOMAIN: { + "platform": "mqtt", + "name": "test", + "command_topic": "test_light_rgb/set", + "rgb_command_topic": "test_light_rgb/rgb/set", + } + }, + ) + await hass.async_block_till_done() + + state = hass.states.get("light.test") + expected_features = light.SUPPORT_COLOR | light.SUPPORT_BRIGHTNESS + assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features + + async def test_no_color_brightness_color_temp_hs_white_xy_if_no_topics(hass, mqtt_mock): """Test if there is no color and brightness if no topic.""" assert await async_setup_component( diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index 7834e1d2678..7856eb84c07 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -188,6 +188,33 @@ async def test_fail_setup_if_color_mode_deprecated(hass, mqtt_mock, deprecated): assert hass.states.get("light.test") is None +async def test_rgb_light(hass, mqtt_mock): + """Test RGB light flags brightness support.""" + assert await async_setup_component( + hass, + light.DOMAIN, + { + light.DOMAIN: { + "platform": "mqtt", + "schema": "json", + "name": "test", + "command_topic": "test_light_rgb/set", + "rgb": True, + } + }, + ) + await hass.async_block_till_done() + + state = hass.states.get("light.test") + expected_features = ( + light.SUPPORT_TRANSITION + | light.SUPPORT_COLOR + | light.SUPPORT_FLASH + | light.SUPPORT_BRIGHTNESS + ) + assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features + + async def test_no_color_brightness_color_temp_white_val_if_no_topics(hass, mqtt_mock): """Test for no RGB, brightness, color temp, effect, white val or XY.""" assert await async_setup_component( diff --git a/tests/components/mqtt/test_light_template.py b/tests/components/mqtt/test_light_template.py index 3bbf14ca668..2e726d40ef1 100644 --- a/tests/components/mqtt/test_light_template.py +++ b/tests/components/mqtt/test_light_template.py @@ -141,6 +141,37 @@ async def test_setup_fails(hass, mqtt_mock): assert hass.states.get("light.test") is None +async def test_rgb_light(hass, mqtt_mock): + """Test RGB light flags brightness support.""" + assert await async_setup_component( + hass, + light.DOMAIN, + { + light.DOMAIN: { + "platform": "mqtt", + "schema": "template", + "name": "test", + "command_topic": "test_light_rgb/set", + "command_on_template": "on", + "command_off_template": "off", + "red_template": '{{ value.split(",")[4].' 'split("-")[0] }}', + "green_template": '{{ value.split(",")[4].' 'split("-")[1] }}', + "blue_template": '{{ value.split(",")[4].' 'split("-")[2] }}', + } + }, + ) + await hass.async_block_till_done() + + state = hass.states.get("light.test") + expected_features = ( + light.SUPPORT_TRANSITION + | light.SUPPORT_COLOR + | light.SUPPORT_FLASH + | light.SUPPORT_BRIGHTNESS + ) + assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features + + async def test_state_change_via_topic(hass, mqtt_mock): """Test state change via topic.""" with assert_setup_component(1, light.DOMAIN):