core/tests/components/light/common.py

313 lines
7.6 KiB
Python
Raw Normal View History

"""Collection of helper methods.
All containing methods are legacy helpers that should not be used by new
components. Instead call the service directly.
"""
from collections.abc import Callable
from homeassistant.components.light import (
2019-07-31 19:25:30 +00:00
ATTR_BRIGHTNESS,
ATTR_BRIGHTNESS_PCT,
ATTR_COLOR_NAME,
ATTR_COLOR_TEMP,
ATTR_EFFECT,
ATTR_FLASH,
ATTR_HS_COLOR,
ATTR_KELVIN,
ATTR_PROFILE,
ATTR_RGB_COLOR,
ATTR_RGBW_COLOR,
ATTR_RGBWW_COLOR,
2019-07-31 19:25:30 +00:00
ATTR_TRANSITION,
ATTR_WHITE,
2019-07-31 19:25:30 +00:00
ATTR_XY_COLOR,
DOMAIN,
ColorMode,
LightEntity,
2019-07-31 19:25:30 +00:00
)
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
ATTR_ENTITY_ID,
ENTITY_MATCH_ALL,
2019-07-31 19:25:30 +00:00
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.loader import bind_hass
from tests.common import MockPlatform, MockToggleEntity, mock_platform
@bind_hass
2019-07-31 19:25:30 +00:00
def turn_on(
hass,
entity_id=ENTITY_MATCH_ALL,
2019-07-31 19:25:30 +00:00
transition=None,
brightness=None,
brightness_pct=None,
rgb_color=None,
rgbw_color=None,
rgbww_color=None,
2019-07-31 19:25:30 +00:00
xy_color=None,
hs_color=None,
color_temp=None,
kelvin=None,
profile=None,
flash=None,
effect=None,
color_name=None,
white=None,
2019-07-31 19:25:30 +00:00
):
"""Turn all or specified light on."""
hass.add_job(
2019-07-31 19:25:30 +00:00
async_turn_on,
hass,
entity_id,
transition,
brightness,
brightness_pct,
rgb_color,
rgbw_color,
rgbww_color,
2019-07-31 19:25:30 +00:00
xy_color,
hs_color,
color_temp,
kelvin,
profile,
flash,
effect,
color_name,
white,
2019-07-31 19:25:30 +00:00
)
async def async_turn_on(
hass,
entity_id=ENTITY_MATCH_ALL,
2019-07-31 19:25:30 +00:00
transition=None,
brightness=None,
brightness_pct=None,
rgb_color=None,
rgbw_color=None,
rgbww_color=None,
2019-07-31 19:25:30 +00:00
xy_color=None,
hs_color=None,
color_temp=None,
kelvin=None,
profile=None,
flash=None,
effect=None,
color_name=None,
white=None,
2019-07-31 19:25:30 +00:00
):
"""Turn all or specified light on."""
data = {
2019-07-31 19:25:30 +00:00
key: value
for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_PROFILE, profile),
(ATTR_TRANSITION, transition),
(ATTR_BRIGHTNESS, brightness),
(ATTR_BRIGHTNESS_PCT, brightness_pct),
(ATTR_RGB_COLOR, rgb_color),
(ATTR_RGBW_COLOR, rgbw_color),
(ATTR_RGBWW_COLOR, rgbww_color),
(ATTR_XY_COLOR, xy_color),
(ATTR_HS_COLOR, hs_color),
(ATTR_COLOR_TEMP, color_temp),
(ATTR_KELVIN, kelvin),
(ATTR_FLASH, flash),
(ATTR_EFFECT, effect),
(ATTR_COLOR_NAME, color_name),
(ATTR_WHITE, white),
2019-07-31 19:25:30 +00:00
]
if value is not None
}
2019-07-31 19:25:30 +00:00
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data, blocking=True)
@bind_hass
def turn_off(hass, entity_id=ENTITY_MATCH_ALL, transition=None, flash=None):
"""Turn all or specified light off."""
hass.add_job(async_turn_off, hass, entity_id, transition, flash)
async def async_turn_off(hass, entity_id=ENTITY_MATCH_ALL, transition=None, flash=None):
"""Turn all or specified light off."""
data = {
2019-07-31 19:25:30 +00:00
key: value
for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_TRANSITION, transition),
(ATTR_FLASH, flash),
]
2019-07-31 19:25:30 +00:00
if value is not None
}
2019-07-31 19:25:30 +00:00
await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True)
@bind_hass
def toggle(
hass,
entity_id=ENTITY_MATCH_ALL,
transition=None,
brightness=None,
brightness_pct=None,
rgb_color=None,
xy_color=None,
hs_color=None,
color_temp=None,
kelvin=None,
profile=None,
flash=None,
effect=None,
color_name=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,
entity_id=ENTITY_MATCH_ALL,
transition=None,
brightness=None,
brightness_pct=None,
rgb_color=None,
xy_color=None,
hs_color=None,
color_temp=None,
kelvin=None,
profile=None,
flash=None,
effect=None,
color_name=None,
):
"""Turn all or specified light on."""
data = {
2019-07-31 19:25:30 +00:00
key: value
for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_PROFILE, profile),
(ATTR_TRANSITION, transition),
(ATTR_BRIGHTNESS, brightness),
(ATTR_BRIGHTNESS_PCT, brightness_pct),
(ATTR_RGB_COLOR, rgb_color),
(ATTR_XY_COLOR, xy_color),
(ATTR_HS_COLOR, hs_color),
(ATTR_COLOR_TEMP, color_temp),
(ATTR_KELVIN, kelvin),
(ATTR_FLASH, flash),
(ATTR_EFFECT, effect),
(ATTR_COLOR_NAME, color_name),
]
2019-07-31 19:25:30 +00:00
if value is not None
}
2019-07-31 19:25:30 +00:00
await hass.services.async_call(DOMAIN, SERVICE_TOGGLE, data, blocking=True)
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):
"""Mock light class."""
_attr_max_color_temp_kelvin = 6500
_attr_min_color_temp_kelvin = 2000
supported_features = 0
brightness = None
color_temp_kelvin = None
hs_color = None
rgb_color = None
rgbw_color = None
rgbww_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):
"""Turn the entity on."""
super().turn_on(**kwargs)
for key, value in kwargs.items():
if key in [
"brightness",
"hs_color",
"xy_color",
"rgb_color",
"rgbw_color",
"rgbww_color",
"color_temp_kelvin",
]:
setattr(self, key, value)
if key == "white":
setattr(self, "brightness", value)
if key in TURN_ON_ARG_TO_COLOR_MODE:
self._attr_color_mode = TURN_ON_ARG_TO_COLOR_MODE[key]
SetupLightPlatformCallable = Callable[[HomeAssistant, list[MockLight]], None]
def setup_light_platform(hass: HomeAssistant, entities: list[MockLight]) -> None:
"""Set up the mock light entity platform."""
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up test light platform."""
async_add_entities(entities)
mock_platform(
hass,
f"test.{DOMAIN}",
MockPlatform(async_setup_platform=async_setup_platform),
)