Improve calculating supported features in template light (#139339)

This commit is contained in:
Jan Bouwhuis 2025-02-26 16:09:20 +01:00 committed by GitHub
parent e403bee95b
commit fdf69fcd7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 1 deletions

View File

@ -1013,7 +1013,7 @@ class LightTemplate(TemplateEntity, LightEntity):
if render in (None, "None", ""):
self._supports_transition = False
return
self._attr_supported_features &= LightEntityFeature.EFFECT
self._attr_supported_features &= ~LightEntityFeature.TRANSITION
self._supports_transition = bool(render)
if self._supports_transition:
self._attr_supported_features |= LightEntityFeature.TRANSITION

View File

@ -1847,6 +1847,60 @@ async def test_supports_transition_template(
) != expected_value
@pytest.mark.parametrize("count", [1])
async def test_supports_transition_template_updates(
hass: HomeAssistant, count: int
) -> None:
"""Test the template for the supports transition dynamically."""
light_config = {
"test_template_light": {
"value_template": "{{ 1 == 1 }}",
"turn_on": {"service": "light.turn_on", "entity_id": "light.test_state"},
"turn_off": {"service": "light.turn_off", "entity_id": "light.test_state"},
"set_temperature": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"color_temp": "{{color_temp}}",
},
},
"set_effect": {
"service": "test.automation",
"data_template": {
"entity_id": "test.test_state",
"effect": "{{effect}}",
},
},
"effect_list_template": "{{ ['Disco', 'Police'] }}",
"effect_template": "{{ None }}",
"supports_transition_template": "{{ states('sensor.test') }}",
}
}
await async_setup_light(hass, count, light_config)
state = hass.states.get("light.test_template_light")
assert state is not None
hass.states.async_set("sensor.test", 0)
await hass.async_block_till_done()
state = hass.states.get("light.test_template_light")
supported_features = state.attributes.get("supported_features")
assert supported_features == LightEntityFeature.EFFECT
hass.states.async_set("sensor.test", 1)
await hass.async_block_till_done()
state = hass.states.get("light.test_template_light")
supported_features = state.attributes.get("supported_features")
assert (
supported_features == LightEntityFeature.TRANSITION | LightEntityFeature.EFFECT
)
hass.states.async_set("sensor.test", 0)
await hass.async_block_till_done()
state = hass.states.get("light.test_template_light")
supported_features = state.attributes.get("supported_features")
assert supported_features == LightEntityFeature.EFFECT
@pytest.mark.parametrize("count", [1])
@pytest.mark.parametrize(
"light_config",