Improve color mode handling in MockLight (#109298)

This commit is contained in:
Erik Montnemery 2024-02-02 09:49:32 +01:00 committed by GitHub
parent e3f1997b6f
commit 4229c35fcd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 90 additions and 3 deletions

View File

@ -279,6 +279,9 @@ async def test_brightness(
entity1 = platform.ENTITIES[1] entity1 = platform.ENTITIES[1]
entity1.supported_features = SUPPORT_BRIGHTNESS entity1.supported_features = SUPPORT_BRIGHTNESS
# Set color modes to none to trigger backwards compatibility in LightEntity
entity1.supported_color_modes = None
entity1.color_mode = None
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -350,6 +353,9 @@ async def test_color_hs(hass: HomeAssistant, enable_custom_integrations: None) -
entity1 = platform.ENTITIES[1] entity1 = platform.ENTITIES[1]
entity1.supported_features = SUPPORT_COLOR entity1.supported_features = SUPPORT_COLOR
# Set color modes to none to trigger backwards compatibility in LightEntity
entity1.supported_color_modes = None
entity1.color_mode = None
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -698,6 +704,9 @@ async def test_color_temp(
entity1 = platform.ENTITIES[1] entity1 = platform.ENTITIES[1]
entity1.supported_features = SUPPORT_COLOR_TEMP entity1.supported_features = SUPPORT_COLOR_TEMP
# Set color modes to none to trigger backwards compatibility in LightEntity
entity1.supported_color_modes = None
entity1.color_mode = None
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -838,6 +847,9 @@ async def test_min_max_mireds(
entity1 = platform.ENTITIES[1] entity1 = platform.ENTITIES[1]
entity1.supported_features = SUPPORT_COLOR_TEMP entity1.supported_features = SUPPORT_COLOR_TEMP
# Set color modes to none to trigger backwards compatibility in LightEntity
entity1.supported_color_modes = None
entity1.color_mode = None
entity1._attr_min_color_temp_kelvin = 1 entity1._attr_min_color_temp_kelvin = 1
entity1._attr_max_color_temp_kelvin = 1234567890 entity1._attr_max_color_temp_kelvin = 1234567890
@ -1015,6 +1027,9 @@ async def test_supported_color_modes(
entity2 = platform.ENTITIES[2] entity2 = platform.ENTITIES[2]
entity2.supported_features = SUPPORT_BRIGHTNESS entity2.supported_features = SUPPORT_BRIGHTNESS
# Set color modes to none to trigger backwards compatibility in LightEntity
entity2.supported_color_modes = None
entity2.color_mode = None
assert await async_setup_component( assert await async_setup_component(
hass, hass,

View File

@ -127,6 +127,9 @@ async def test_services(
| light.LightEntityFeature.EFFECT | light.LightEntityFeature.EFFECT
| light.LightEntityFeature.TRANSITION | light.LightEntityFeature.TRANSITION
) )
# Set color modes to none to trigger backwards compatibility in LightEntity
ent2.supported_color_modes = None
ent2.color_mode = None
ent3.supported_features = ( ent3.supported_features = (
light.LightEntityFeature.FLASH | light.LightEntityFeature.TRANSITION light.LightEntityFeature.FLASH | light.LightEntityFeature.TRANSITION
) )
@ -905,9 +908,15 @@ async def test_light_brightness_step(
platform.ENTITIES.append(platform.MockLight("Test_1", STATE_ON)) platform.ENTITIES.append(platform.MockLight("Test_1", STATE_ON))
entity0 = platform.ENTITIES[0] entity0 = platform.ENTITIES[0]
entity0.supported_features = light.SUPPORT_BRIGHTNESS entity0.supported_features = light.SUPPORT_BRIGHTNESS
# Set color modes to none to trigger backwards compatibility in LightEntity
entity0.supported_color_modes = None
entity0.color_mode = None
entity0.brightness = 100 entity0.brightness = 100
entity1 = platform.ENTITIES[1] entity1 = platform.ENTITIES[1]
entity1.supported_features = light.SUPPORT_BRIGHTNESS entity1.supported_features = light.SUPPORT_BRIGHTNESS
# Set color modes to none to trigger backwards compatibility in LightEntity
entity1.supported_color_modes = None
entity1.color_mode = None
entity1.brightness = 50 entity1.brightness = 50
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}}) assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -967,6 +976,9 @@ async def test_light_brightness_pct_conversion(
platform.init() platform.init()
entity = platform.ENTITIES[0] entity = platform.ENTITIES[0]
entity.supported_features = light.SUPPORT_BRIGHTNESS entity.supported_features = light.SUPPORT_BRIGHTNESS
# Set color modes to none to trigger backwards compatibility in LightEntity
entity.supported_color_modes = None
entity.color_mode = None
entity.brightness = 100 entity.brightness = 100
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}}) assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1133,17 +1145,29 @@ async def test_light_backwards_compatibility_supported_color_modes(
entity1 = platform.ENTITIES[1] entity1 = platform.ENTITIES[1]
entity1.supported_features = light.SUPPORT_BRIGHTNESS entity1.supported_features = light.SUPPORT_BRIGHTNESS
# Set color modes to none to trigger backwards compatibility in LightEntity
entity1.supported_color_modes = None
entity1.color_mode = None
entity2 = platform.ENTITIES[2] entity2 = platform.ENTITIES[2]
entity2.supported_features = light.SUPPORT_BRIGHTNESS | light.SUPPORT_COLOR_TEMP entity2.supported_features = light.SUPPORT_BRIGHTNESS | light.SUPPORT_COLOR_TEMP
# Set color modes to none to trigger backwards compatibility in LightEntity
entity2.supported_color_modes = None
entity2.color_mode = None
entity3 = platform.ENTITIES[3] entity3 = platform.ENTITIES[3]
entity3.supported_features = light.SUPPORT_BRIGHTNESS | light.SUPPORT_COLOR entity3.supported_features = light.SUPPORT_BRIGHTNESS | light.SUPPORT_COLOR
# Set color modes to none to trigger backwards compatibility in LightEntity
entity3.supported_color_modes = None
entity3.color_mode = None
entity4 = platform.ENTITIES[4] entity4 = platform.ENTITIES[4]
entity4.supported_features = ( entity4.supported_features = (
light.SUPPORT_BRIGHTNESS | light.SUPPORT_COLOR | light.SUPPORT_COLOR_TEMP light.SUPPORT_BRIGHTNESS | light.SUPPORT_COLOR | light.SUPPORT_COLOR_TEMP
) )
# Set color modes to none to trigger backwards compatibility in LightEntity
entity4.supported_color_modes = None
entity4.color_mode = None
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}}) assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1204,20 +1228,32 @@ async def test_light_backwards_compatibility_color_mode(
entity1 = platform.ENTITIES[1] entity1 = platform.ENTITIES[1]
entity1.supported_features = light.SUPPORT_BRIGHTNESS entity1.supported_features = light.SUPPORT_BRIGHTNESS
# Set color modes to none to trigger backwards compatibility in LightEntity
entity1.supported_color_modes = None
entity1.color_mode = None
entity1.brightness = 100 entity1.brightness = 100
entity2 = platform.ENTITIES[2] entity2 = platform.ENTITIES[2]
entity2.supported_features = light.SUPPORT_BRIGHTNESS | light.SUPPORT_COLOR_TEMP entity2.supported_features = light.SUPPORT_BRIGHTNESS | light.SUPPORT_COLOR_TEMP
# Set color modes to none to trigger backwards compatibility in LightEntity
entity2.supported_color_modes = None
entity2.color_mode = None
entity2.color_temp_kelvin = 10000 entity2.color_temp_kelvin = 10000
entity3 = platform.ENTITIES[3] entity3 = platform.ENTITIES[3]
entity3.supported_features = light.SUPPORT_BRIGHTNESS | light.SUPPORT_COLOR entity3.supported_features = light.SUPPORT_BRIGHTNESS | light.SUPPORT_COLOR
# Set color modes to none to trigger backwards compatibility in LightEntity
entity3.supported_color_modes = None
entity3.color_mode = None
entity3.hs_color = (240, 100) entity3.hs_color = (240, 100)
entity4 = platform.ENTITIES[4] entity4 = platform.ENTITIES[4]
entity4.supported_features = ( entity4.supported_features = (
light.SUPPORT_BRIGHTNESS | light.SUPPORT_COLOR | light.SUPPORT_COLOR_TEMP light.SUPPORT_BRIGHTNESS | light.SUPPORT_COLOR | light.SUPPORT_COLOR_TEMP
) )
# Set color modes to none to trigger backwards compatibility in LightEntity
entity4.supported_color_modes = None
entity4.color_mode = None
entity4.hs_color = (240, 100) entity4.hs_color = (240, 100)
entity4.color_temp_kelvin = 10000 entity4.color_temp_kelvin = 10000
@ -1464,6 +1500,9 @@ async def test_light_service_call_color_conversion(
entity4 = platform.ENTITIES[4] entity4 = platform.ENTITIES[4]
entity4.supported_features = light.SUPPORT_COLOR entity4.supported_features = light.SUPPORT_COLOR
# Set color modes to none to trigger backwards compatibility in LightEntity
entity4.supported_color_modes = None
entity4.color_mode = None
entity5 = platform.ENTITIES[5] entity5 = platform.ENTITIES[5]
entity5.supported_color_modes = {light.ColorMode.RGBW} entity5.supported_color_modes = {light.ColorMode.RGBW}
@ -1905,6 +1944,9 @@ async def test_light_service_call_color_conversion_named_tuple(
entity4 = platform.ENTITIES[4] entity4 = platform.ENTITIES[4]
entity4.supported_features = light.SUPPORT_COLOR entity4.supported_features = light.SUPPORT_COLOR
# Set color modes to none to trigger backwards compatibility in LightEntity
entity4.supported_color_modes = None
entity4.color_mode = None
entity5 = platform.ENTITIES[5] entity5 = platform.ENTITIES[5]
entity5.supported_color_modes = {light.ColorMode.RGBW} entity5.supported_color_modes = {light.ColorMode.RGBW}
@ -2330,6 +2372,9 @@ async def test_light_state_color_conversion(
entity3 = platform.ENTITIES[3] entity3 = platform.ENTITIES[3]
entity3.hs_color = (240, 100) entity3.hs_color = (240, 100)
entity3.supported_features = light.SUPPORT_COLOR entity3.supported_features = light.SUPPORT_COLOR
# Set color modes to none to trigger backwards compatibility in LightEntity
entity3.supported_color_modes = None
entity3.color_mode = None
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}}) assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()

View File

@ -2,7 +2,7 @@
Call init before using it in your tests to ensure clean test data. Call init before using it in your tests to ensure clean test data.
""" """
from homeassistant.components.light import LightEntity from homeassistant.components.light import ColorMode, LightEntity
from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.const import STATE_OFF, STATE_ON
from tests.common import MockToggleEntity from tests.common import MockToggleEntity
@ -32,13 +32,21 @@ async def async_setup_platform(
async_add_entities_callback(ENTITIES) async_add_entities_callback(ENTITIES)
TURN_ON_ARG_TO_COLOR_MODE = {
"hs_color": ColorMode.HS,
"xy_color": ColorMode.XY,
"rgb_color": ColorMode.RGB,
"rgbw_color": ColorMode.RGBW,
"rgbww_color": ColorMode.RGBWW,
"color_temp_kelvin": ColorMode.COLOR_TEMP,
}
class MockLight(MockToggleEntity, LightEntity): class MockLight(MockToggleEntity, LightEntity):
"""Mock light class.""" """Mock light class."""
color_mode = None
_attr_max_color_temp_kelvin = 6500 _attr_max_color_temp_kelvin = 6500
_attr_min_color_temp_kelvin = 2000 _attr_min_color_temp_kelvin = 2000
supported_color_modes = None
supported_features = 0 supported_features = 0
brightness = None brightness = None
@ -49,6 +57,23 @@ class MockLight(MockToggleEntity, LightEntity):
rgbww_color = None rgbww_color = None
xy_color = None xy_color = None
def __init__(
self,
name,
state,
unique_id=None,
supported_color_modes: set[ColorMode] | None = None,
):
"""Initialize the mock light."""
super().__init__(name, state, unique_id)
if supported_color_modes is None:
supported_color_modes = {ColorMode.ONOFF}
self._attr_supported_color_modes = supported_color_modes
color_mode = ColorMode.UNKNOWN
if len(supported_color_modes) == 1:
color_mode = next(iter(supported_color_modes))
self._attr_color_mode = color_mode
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Turn the entity on.""" """Turn the entity on."""
super().turn_on(**kwargs) super().turn_on(**kwargs)
@ -65,3 +90,5 @@ class MockLight(MockToggleEntity, LightEntity):
setattr(self, key, value) setattr(self, key, value)
if key == "white": if key == "white":
setattr(self, "brightness", value) setattr(self, "brightness", value)
if key in TURN_ON_ARG_TO_COLOR_MODE:
self._attr_color_mode = TURN_ON_ARG_TO_COLOR_MODE[key]