init home-assistant
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
"""Provide a mock light platform.
|
||||
|
||||
Call init before using it in your tests to ensure clean test data.
|
||||
"""
|
||||
|
||||
from typing import Any, Literal
|
||||
|
||||
from homeassistant.components.light import ColorMode, LightEntity
|
||||
from homeassistant.const import STATE_OFF, STATE_ON
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from tests.common import MockToggleEntity
|
||||
|
||||
ENTITIES = []
|
||||
|
||||
|
||||
def init(empty=False):
|
||||
"""Initialize the platform with entities."""
|
||||
# pylint: disable-next=global-statement
|
||||
global ENTITIES # noqa: PLW0603
|
||||
|
||||
ENTITIES = (
|
||||
[]
|
||||
if empty
|
||||
else [
|
||||
MockLight("Ceiling", STATE_ON),
|
||||
MockLight("Ceiling", STATE_OFF),
|
||||
MockLight(None, STATE_OFF),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
async_add_entities_callback: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Return mock 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):
|
||||
"""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: str | None,
|
||||
state: Literal["on", "off"] | None,
|
||||
supported_color_modes: set[ColorMode] | None = None,
|
||||
) -> None:
|
||||
"""Initialize the mock light."""
|
||||
super().__init__(name, state)
|
||||
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: Any) -> None:
|
||||
"""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]
|
||||
Reference in New Issue
Block a user