Adjust light test helpers to use Kelvin, and cleanup unused helpers (#133048)

Cleanup light test helper methods
pull/133063/head
epenet 2024-12-12 16:49:25 +01:00 committed by GitHub
parent 33c799b2d0
commit 2ce2765e67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 28 additions and 114 deletions

View File

@ -132,6 +132,7 @@ tests: &tests
- tests/components/conftest.py
- tests/components/diagnostics/**
- tests/components/history/**
- tests/components/light/common.py
- tests/components/logbook/**
- tests/components/recorder/**
- tests/components/repairs/**

View File

@ -10,11 +10,10 @@ from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_BRIGHTNESS_PCT,
ATTR_COLOR_NAME,
ATTR_COLOR_TEMP,
ATTR_COLOR_TEMP_KELVIN,
ATTR_EFFECT,
ATTR_FLASH,
ATTR_HS_COLOR,
ATTR_KELVIN,
ATTR_PROFILE,
ATTR_RGB_COLOR,
ATTR_RGBW_COLOR,
@ -35,54 +34,10 @@ from homeassistant.const import (
SERVICE_TURN_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.loader import bind_hass
from tests.common import MockToggleEntity
@bind_hass
def turn_on(
hass: HomeAssistant,
entity_id: str = ENTITY_MATCH_ALL,
transition: float | None = None,
brightness: int | None = None,
brightness_pct: float | None = None,
rgb_color: tuple[int, int, int] | None = None,
rgbw_color: tuple[int, int, int, int] | None = None,
rgbww_color: tuple[int, int, int, int, int] | None = None,
xy_color: tuple[float, float] | None = None,
hs_color: tuple[float, float] | None = None,
color_temp: int | None = None,
kelvin: int | None = None,
profile: str | None = None,
flash: str | None = None,
effect: str | None = None,
color_name: str | None = None,
white: bool | None = None,
) -> None:
"""Turn all or specified light on."""
hass.add_job(
async_turn_on,
hass,
entity_id,
transition,
brightness,
brightness_pct,
rgb_color,
rgbw_color,
rgbww_color,
xy_color,
hs_color,
color_temp,
kelvin,
profile,
flash,
effect,
color_name,
white,
)
async def async_turn_on(
hass: HomeAssistant,
entity_id: str = ENTITY_MATCH_ALL,
@ -94,8 +49,7 @@ async def async_turn_on(
rgbww_color: tuple[int, int, int, int, int] | None = None,
xy_color: tuple[float, float] | None = None,
hs_color: tuple[float, float] | None = None,
color_temp: int | None = None,
kelvin: int | None = None,
color_temp_kelvin: int | None = None,
profile: str | None = None,
flash: str | None = None,
effect: str | None = None,
@ -116,8 +70,7 @@ async def async_turn_on(
(ATTR_RGBWW_COLOR, rgbww_color),
(ATTR_XY_COLOR, xy_color),
(ATTR_HS_COLOR, hs_color),
(ATTR_COLOR_TEMP, color_temp),
(ATTR_KELVIN, kelvin),
(ATTR_COLOR_TEMP_KELVIN, color_temp_kelvin),
(ATTR_FLASH, flash),
(ATTR_EFFECT, effect),
(ATTR_COLOR_NAME, color_name),
@ -129,17 +82,6 @@ async def async_turn_on(
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data, blocking=True)
@bind_hass
def turn_off(
hass: HomeAssistant,
entity_id: str = ENTITY_MATCH_ALL,
transition: float | None = None,
flash: str | None = None,
) -> None:
"""Turn all or specified light off."""
hass.add_job(async_turn_off, hass, entity_id, transition, flash)
async def async_turn_off(
hass: HomeAssistant,
entity_id: str = ENTITY_MATCH_ALL,
@ -160,43 +102,6 @@ async def async_turn_off(
await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True)
@bind_hass
def toggle(
hass: HomeAssistant,
entity_id: str = ENTITY_MATCH_ALL,
transition: float | None = None,
brightness: int | None = None,
brightness_pct: float | None = None,
rgb_color: tuple[int, int, int] | None = None,
xy_color: tuple[float, float] | None = None,
hs_color: tuple[float, float] | None = None,
color_temp: int | None = None,
kelvin: int | None = None,
profile: str | None = None,
flash: str | None = None,
effect: str | None = None,
color_name: str | None = None,
) -> None:
"""Toggle all or specified light."""
hass.add_job(
async_toggle,
hass,
entity_id,
transition,
brightness,
brightness_pct,
rgb_color,
xy_color,
hs_color,
color_temp,
kelvin,
profile,
flash,
effect,
color_name,
)
async def async_toggle(
hass: HomeAssistant,
entity_id: str = ENTITY_MATCH_ALL,
@ -206,8 +111,7 @@ async def async_toggle(
rgb_color: tuple[int, int, int] | None = None,
xy_color: tuple[float, float] | None = None,
hs_color: tuple[float, float] | None = None,
color_temp: int | None = None,
kelvin: int | None = None,
color_temp_kelvin: int | None = None,
profile: str | None = None,
flash: str | None = None,
effect: str | None = None,
@ -225,8 +129,7 @@ async def async_toggle(
(ATTR_RGB_COLOR, rgb_color),
(ATTR_XY_COLOR, xy_color),
(ATTR_HS_COLOR, hs_color),
(ATTR_COLOR_TEMP, color_temp),
(ATTR_KELVIN, kelvin),
(ATTR_COLOR_TEMP_KELVIN, color_temp_kelvin),
(ATTR_FLASH, flash),
(ATTR_EFFECT, effect),
(ATTR_COLOR_NAME, color_name),

View File

@ -1148,7 +1148,7 @@ async def test_sending_mqtt_commands_and_optimistic(
assert state.attributes.get(light.ATTR_COLOR_MODE) == "xy"
assert state.attributes.get(light.ATTR_SUPPORTED_COLOR_MODES) == color_modes
await common.async_turn_on(hass, "light.test", color_temp=125)
await common.async_turn_on(hass, "light.test", color_temp_kelvin=8000)
mqtt_mock.async_publish.assert_has_calls(
[
call("test_light_rgb/color_temp/set", "125", 2, False),
@ -1321,7 +1321,7 @@ async def test_sending_mqtt_color_temp_command_with_template(
state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", color_temp=100)
await common.async_turn_on(hass, "light.test", color_temp_kelvin=10000)
mqtt_mock.async_publish.assert_has_calls(
[

View File

@ -423,7 +423,9 @@ async def test_single_color_mode(
state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", brightness=50, color_temp=192)
await common.async_turn_on(
hass, "light.test", brightness=50, color_temp_kelvin=5208
)
async_fire_mqtt_message(
hass,
@ -458,7 +460,9 @@ async def test_turn_on_with_unknown_color_mode_optimistic(
assert state.state == STATE_ON
# Turn on the light with brightness or color_temp attributes
await common.async_turn_on(hass, "light.test", brightness=50, color_temp=192)
await common.async_turn_on(
hass, "light.test", brightness=50, color_temp_kelvin=5208
)
state = hass.states.get("light.test")
assert state.attributes.get("color_mode") == light.ColorMode.COLOR_TEMP
assert state.attributes.get("brightness") == 50
@ -1083,7 +1087,7 @@ async def test_sending_mqtt_commands_and_optimistic(
state = hass.states.get("light.test")
assert state.state == STATE_ON
await common.async_turn_on(hass, "light.test", color_temp=90)
await common.async_turn_on(hass, "light.test", color_temp_kelvin=11111)
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set",
@ -1244,7 +1248,7 @@ async def test_sending_mqtt_commands_and_optimistic2(
assert state.state == STATE_ON
# Turn the light on with color temperature
await common.async_turn_on(hass, "light.test", color_temp=90)
await common.async_turn_on(hass, "light.test", color_temp_kelvin=11111)
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set",
JsonValidator('{"state":"ON","color_temp":90}'),

View File

@ -205,7 +205,9 @@ async def test_single_color_mode(
state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
await common.async_turn_on(hass, "light.test", brightness=50, color_temp=192)
await common.async_turn_on(
hass, "light.test", brightness=50, color_temp_kelvin=5208
)
async_fire_mqtt_message(hass, "test_light", "on,50,192")
color_modes = [light.ColorMode.COLOR_TEMP]
state = hass.states.get("light.test")
@ -463,7 +465,7 @@ async def test_sending_mqtt_commands_and_optimistic(
assert state.state == STATE_ON
# Set color_temp
await common.async_turn_on(hass, "light.test", color_temp=70)
await common.async_turn_on(hass, "light.test", color_temp_kelvin=14285)
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set", "on,,70,--,-", 2, False
)
@ -594,7 +596,7 @@ async def test_sending_mqtt_commands_non_optimistic_brightness_template(
assert state.state == STATE_UNKNOWN
# Set color_temp
await common.async_turn_on(hass, "light.test", color_temp=70)
await common.async_turn_on(hass, "light.test", color_temp_kelvin=14285)
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set", "on,,70,--,-", 0, False
)

View File

@ -1108,7 +1108,7 @@ async def test_sending_mqtt_commands_rgbww(
)
mqtt_mock.async_publish.reset_mock()
await common.async_turn_on(hass, "light.tasmota_test", color_temp=200)
await common.async_turn_on(hass, "light.tasmota_test", color_temp_kelvin=5000)
mqtt_mock.async_publish.assert_called_once_with(
"tasmota_49A3BC/cmnd/Backlog",
"NoDelay;Power1 ON;NoDelay;CT 200",
@ -1350,7 +1350,9 @@ async def test_transition(
assert state.attributes.get("color_temp") == 153
# Set color_temp of the light from 153 to 500 @ 50%: Speed should be 6*2*2=24
await common.async_turn_on(hass, "light.tasmota_test", color_temp=500, transition=6)
await common.async_turn_on(
hass, "light.tasmota_test", color_temp_kelvin=2000, transition=6
)
mqtt_mock.async_publish.assert_called_once_with(
"tasmota_49A3BC/cmnd/Backlog",
"NoDelay;Fade2 1;NoDelay;Speed2 24;NoDelay;Power1 ON;NoDelay;CT 500",
@ -1369,7 +1371,9 @@ async def test_transition(
assert state.attributes.get("color_temp") == 500
# Set color_temp of the light from 500 to 326 @ 50%: Speed should be 6*2*2*2=48->40
await common.async_turn_on(hass, "light.tasmota_test", color_temp=326, transition=6)
await common.async_turn_on(
hass, "light.tasmota_test", color_temp_kelvin=3067, transition=6
)
mqtt_mock.async_publish.assert_called_once_with(
"tasmota_49A3BC/cmnd/Backlog",
"NoDelay;Fade2 1;NoDelay;Speed2 40;NoDelay;Power1 ON;NoDelay;CT 326",