Set deprecated supported_features for MQTT JSON light (#49167)
* Set deprecated supported_features for MQTT json light * Update homeassistant/components/light/__init__.py Co-authored-by: Franck Nijhof <git@frenck.dev> Co-authored-by: Franck Nijhof <git@frenck.dev>pull/49263/head
parent
1b5148a3af
commit
ec56ae2cbc
homeassistant/components
light
mqtt/light
tests/components/mqtt
|
@ -751,3 +751,20 @@ class Light(LightEntity):
|
|||
"Light is deprecated, modify %s to extend LightEntity",
|
||||
cls.__name__,
|
||||
)
|
||||
|
||||
|
||||
def legacy_supported_features(
|
||||
supported_features: int, supported_color_modes: list[str] | None
|
||||
) -> int:
|
||||
"""Calculate supported features with backwards compatibility."""
|
||||
# Backwards compatibility for supported_color_modes added in 2021.4
|
||||
if supported_color_modes is None:
|
||||
return supported_features
|
||||
if any(mode in supported_color_modes for mode in COLOR_MODES_COLOR):
|
||||
supported_features |= SUPPORT_COLOR
|
||||
if any(mode in supported_color_modes for mode in COLOR_MODES_BRIGHTNESS):
|
||||
supported_features |= SUPPORT_BRIGHTNESS
|
||||
if COLOR_MODE_COLOR_TEMP in supported_color_modes:
|
||||
supported_features |= SUPPORT_COLOR_TEMP
|
||||
|
||||
return supported_features
|
||||
|
|
|
@ -35,6 +35,7 @@ from homeassistant.components.light import (
|
|||
SUPPORT_WHITE_VALUE,
|
||||
VALID_COLOR_MODES,
|
||||
LightEntity,
|
||||
legacy_supported_features,
|
||||
valid_supported_color_modes,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
|
@ -458,7 +459,9 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
|
|||
@property
|
||||
def supported_features(self):
|
||||
"""Flag supported features."""
|
||||
return self._supported_features
|
||||
return legacy_supported_features(
|
||||
self._supported_features, self._config.get(CONF_SUPPORTED_COLOR_MODES)
|
||||
)
|
||||
|
||||
def _set_flash_and_transition(self, message, **kwargs):
|
||||
if ATTR_TRANSITION in kwargs:
|
||||
|
|
|
@ -234,10 +234,10 @@ async def test_rgb_light(hass, mqtt_mock):
|
|||
|
||||
state = hass.states.get("light.test")
|
||||
expected_features = (
|
||||
light.SUPPORT_TRANSITION
|
||||
light.SUPPORT_BRIGHTNESS
|
||||
| light.SUPPORT_COLOR
|
||||
| light.SUPPORT_FLASH
|
||||
| light.SUPPORT_BRIGHTNESS
|
||||
| light.SUPPORT_TRANSITION
|
||||
)
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
|
||||
|
||||
|
@ -261,7 +261,8 @@ async def test_no_color_brightness_color_temp_white_val_if_no_topics(hass, mqtt_
|
|||
|
||||
state = hass.states.get("light.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == 40
|
||||
expected_features = light.SUPPORT_FLASH | light.SUPPORT_TRANSITION
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
|
||||
assert state.attributes.get("rgb_color") is None
|
||||
assert state.attributes.get("brightness") is None
|
||||
assert state.attributes.get("color_temp") is None
|
||||
|
@ -310,7 +311,16 @@ async def test_controlling_state_via_topic(hass, mqtt_mock):
|
|||
|
||||
state = hass.states.get("light.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == 191
|
||||
expected_features = (
|
||||
light.SUPPORT_BRIGHTNESS
|
||||
| light.SUPPORT_COLOR
|
||||
| light.SUPPORT_COLOR_TEMP
|
||||
| light.SUPPORT_EFFECT
|
||||
| light.SUPPORT_FLASH
|
||||
| light.SUPPORT_TRANSITION
|
||||
| light.SUPPORT_WHITE_VALUE
|
||||
)
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
|
||||
assert state.attributes.get("rgb_color") is None
|
||||
assert state.attributes.get("brightness") is None
|
||||
assert state.attributes.get("color_temp") is None
|
||||
|
@ -429,7 +439,15 @@ async def test_controlling_state_via_topic2(hass, mqtt_mock, caplog):
|
|||
|
||||
state = hass.states.get("light.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == 44
|
||||
expected_features = (
|
||||
light.SUPPORT_BRIGHTNESS
|
||||
| light.SUPPORT_COLOR
|
||||
| light.SUPPORT_COLOR_TEMP
|
||||
| light.SUPPORT_EFFECT
|
||||
| light.SUPPORT_FLASH
|
||||
| light.SUPPORT_TRANSITION
|
||||
)
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
|
||||
assert state.attributes.get("brightness") is None
|
||||
assert state.attributes.get("color_mode") is None
|
||||
assert state.attributes.get("color_temp") is None
|
||||
|
@ -610,7 +628,16 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
|
|||
assert state.attributes.get("effect") == "random"
|
||||
assert state.attributes.get("color_temp") == 100
|
||||
assert state.attributes.get("white_value") == 50
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == 191
|
||||
expected_features = (
|
||||
light.SUPPORT_BRIGHTNESS
|
||||
| light.SUPPORT_COLOR
|
||||
| light.SUPPORT_COLOR_TEMP
|
||||
| light.SUPPORT_EFFECT
|
||||
| light.SUPPORT_FLASH
|
||||
| light.SUPPORT_TRANSITION
|
||||
| light.SUPPORT_WHITE_VALUE
|
||||
)
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
|
||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
|
||||
await common.async_turn_on(hass, "light.test")
|
||||
|
@ -738,7 +765,15 @@ async def test_sending_mqtt_commands_and_optimistic2(hass, mqtt_mock):
|
|||
|
||||
state = hass.states.get("light.test")
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == 44
|
||||
expected_features = (
|
||||
light.SUPPORT_BRIGHTNESS
|
||||
| light.SUPPORT_COLOR
|
||||
| light.SUPPORT_COLOR_TEMP
|
||||
| light.SUPPORT_EFFECT
|
||||
| light.SUPPORT_FLASH
|
||||
| light.SUPPORT_TRANSITION
|
||||
)
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
|
||||
assert state.attributes.get("brightness") == 95
|
||||
assert state.attributes.get("color_mode") == "rgb"
|
||||
assert state.attributes.get("color_temp") is None
|
||||
|
@ -1313,7 +1348,10 @@ async def test_effect(hass, mqtt_mock):
|
|||
|
||||
state = hass.states.get("light.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == 44
|
||||
expected_features = (
|
||||
light.SUPPORT_EFFECT | light.SUPPORT_FLASH | light.SUPPORT_TRANSITION
|
||||
)
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
|
||||
|
||||
await common.async_turn_on(hass, "light.test")
|
||||
|
||||
|
@ -1373,7 +1411,8 @@ async def test_flash_short_and_long(hass, mqtt_mock):
|
|||
|
||||
state = hass.states.get("light.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == 40
|
||||
expected_features = light.SUPPORT_FLASH | light.SUPPORT_TRANSITION
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
|
||||
|
||||
await common.async_turn_on(hass, "light.test", flash="short")
|
||||
|
||||
|
@ -1431,8 +1470,8 @@ async def test_transition(hass, mqtt_mock):
|
|||
|
||||
state = hass.states.get("light.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == 40
|
||||
|
||||
expected_features = light.SUPPORT_FLASH | light.SUPPORT_TRANSITION
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
|
||||
await common.async_turn_on(hass, "light.test", transition=15)
|
||||
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
|
@ -1523,7 +1562,15 @@ async def test_invalid_values(hass, mqtt_mock):
|
|||
|
||||
state = hass.states.get("light.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == 187
|
||||
expected_features = (
|
||||
light.SUPPORT_BRIGHTNESS
|
||||
| light.SUPPORT_COLOR
|
||||
| light.SUPPORT_COLOR_TEMP
|
||||
| light.SUPPORT_FLASH
|
||||
| light.SUPPORT_TRANSITION
|
||||
| light.SUPPORT_WHITE_VALUE
|
||||
)
|
||||
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
|
||||
assert state.attributes.get("rgb_color") is None
|
||||
assert state.attributes.get("brightness") is None
|
||||
assert state.attributes.get("white_value") is None
|
||||
|
|
Loading…
Reference in New Issue