init home-assistant
This commit is contained in:
1
home-assistant/tests/components/light/__init__.py
Normal file
1
home-assistant/tests/components/light/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""The tests for Light platforms."""
|
||||
202
home-assistant/tests/components/light/common.py
Normal file
202
home-assistant/tests/components/light/common.py
Normal file
@@ -0,0 +1,202 @@
|
||||
"""Collection of helper methods.
|
||||
|
||||
All containing methods are legacy helpers that should not be used by new
|
||||
components. Instead call the service directly.
|
||||
"""
|
||||
|
||||
from typing import Any, Literal
|
||||
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS,
|
||||
ATTR_BRIGHTNESS_PCT,
|
||||
ATTR_COLOR_NAME,
|
||||
ATTR_COLOR_TEMP_KELVIN,
|
||||
ATTR_EFFECT,
|
||||
ATTR_FLASH,
|
||||
ATTR_HS_COLOR,
|
||||
ATTR_PROFILE,
|
||||
ATTR_RGB_COLOR,
|
||||
ATTR_RGBW_COLOR,
|
||||
ATTR_RGBWW_COLOR,
|
||||
ATTR_TRANSITION,
|
||||
ATTR_WHITE,
|
||||
ATTR_XY_COLOR,
|
||||
DEFAULT_MAX_KELVIN,
|
||||
DEFAULT_MIN_KELVIN,
|
||||
DOMAIN,
|
||||
ColorMode,
|
||||
LightEntity,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ENTITY_MATCH_ALL,
|
||||
SERVICE_TOGGLE,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockToggleEntity
|
||||
|
||||
|
||||
async def async_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_kelvin: int | None = None,
|
||||
profile: str | None = None,
|
||||
flash: str | None = None,
|
||||
effect: str | None = None,
|
||||
color_name: str | None = None,
|
||||
white: int | None = None,
|
||||
) -> None:
|
||||
"""Turn all or specified light on."""
|
||||
data = {
|
||||
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_KELVIN, color_temp_kelvin),
|
||||
(ATTR_FLASH, flash),
|
||||
(ATTR_EFFECT, effect),
|
||||
(ATTR_COLOR_NAME, color_name),
|
||||
(ATTR_WHITE, white),
|
||||
)
|
||||
if value is not None
|
||||
}
|
||||
|
||||
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data, blocking=True)
|
||||
|
||||
|
||||
async def async_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."""
|
||||
data = {
|
||||
key: value
|
||||
for key, value in (
|
||||
(ATTR_ENTITY_ID, entity_id),
|
||||
(ATTR_TRANSITION, transition),
|
||||
(ATTR_FLASH, flash),
|
||||
)
|
||||
if value is not None
|
||||
}
|
||||
|
||||
await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True)
|
||||
|
||||
|
||||
async def async_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_kelvin: int | None = None,
|
||||
profile: str | None = None,
|
||||
flash: str | None = None,
|
||||
effect: str | None = None,
|
||||
color_name: str | None = None,
|
||||
) -> None:
|
||||
"""Turn all or specified light on."""
|
||||
data = {
|
||||
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_KELVIN, color_temp_kelvin),
|
||||
(ATTR_FLASH, flash),
|
||||
(ATTR_EFFECT, effect),
|
||||
(ATTR_COLOR_NAME, color_name),
|
||||
)
|
||||
if value is not None
|
||||
}
|
||||
|
||||
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 = DEFAULT_MAX_KELVIN
|
||||
_attr_min_color_temp_kelvin = DEFAULT_MIN_KELVIN
|
||||
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]
|
||||
26
home-assistant/tests/components/light/conftest.py
Normal file
26
home-assistant/tests/components/light/conftest.py
Normal file
@@ -0,0 +1,26 @@
|
||||
"""Light conftest."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.light import Profiles
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_light_profiles():
|
||||
"""Mock loading of profiles."""
|
||||
data = {}
|
||||
|
||||
def mock_profiles_class(hass: HomeAssistant) -> Profiles:
|
||||
profiles = Profiles(hass)
|
||||
profiles.data = data
|
||||
profiles.async_initialize = AsyncMock()
|
||||
return profiles
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.light.Profiles",
|
||||
side_effect=mock_profiles_class,
|
||||
):
|
||||
yield data
|
||||
668
home-assistant/tests/components/light/test_device_action.py
Normal file
668
home-assistant/tests/components/light/test_device_action.py
Normal file
@@ -0,0 +1,668 @@
|
||||
"""The test for light device automation."""
|
||||
|
||||
import pytest
|
||||
from pytest_unordered import unordered
|
||||
|
||||
from homeassistant.components import automation
|
||||
from homeassistant.components.device_automation import DeviceAutomationType
|
||||
from homeassistant.components.light import (
|
||||
ATTR_SUPPORTED_COLOR_MODES,
|
||||
DOMAIN,
|
||||
FLASH_LONG,
|
||||
FLASH_SHORT,
|
||||
ColorMode,
|
||||
LightEntityFeature,
|
||||
)
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.entity_registry import RegistryEntryHider
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import (
|
||||
MockConfigEntry,
|
||||
async_get_device_automation_capabilities,
|
||||
async_get_device_automations,
|
||||
async_mock_service,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
|
||||
def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
|
||||
"""Stub copying the blueprints to the config folder."""
|
||||
|
||||
|
||||
async def test_get_actions(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test we get the expected actions from a light."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"5678",
|
||||
device_id=device_entry.id,
|
||||
supported_features=LightEntityFeature.FLASH,
|
||||
capabilities={"supported_color_modes": ["brightness"]},
|
||||
)
|
||||
expected_actions = [
|
||||
{
|
||||
"domain": DOMAIN,
|
||||
"type": action,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": False},
|
||||
}
|
||||
for action in (
|
||||
"brightness_decrease",
|
||||
"brightness_increase",
|
||||
"flash",
|
||||
"turn_off",
|
||||
"turn_on",
|
||||
"toggle",
|
||||
)
|
||||
]
|
||||
actions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.ACTION, device_entry.id
|
||||
)
|
||||
assert actions == unordered(expected_actions)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("hidden_by", "entity_category"),
|
||||
[
|
||||
(RegistryEntryHider.INTEGRATION, None),
|
||||
(RegistryEntryHider.USER, None),
|
||||
(None, EntityCategory.CONFIG),
|
||||
(None, EntityCategory.DIAGNOSTIC),
|
||||
],
|
||||
)
|
||||
async def test_get_actions_hidden_auxiliary(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
hidden_by,
|
||||
entity_category,
|
||||
) -> None:
|
||||
"""Test we get the expected actions from a hidden or auxiliary entity."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"5678",
|
||||
device_id=device_entry.id,
|
||||
entity_category=entity_category,
|
||||
hidden_by=hidden_by,
|
||||
supported_features=0,
|
||||
capabilities={"supported_color_modes": ["onoff"]},
|
||||
)
|
||||
expected_actions = []
|
||||
expected_actions += [
|
||||
{
|
||||
"domain": DOMAIN,
|
||||
"type": action,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": True},
|
||||
}
|
||||
for action in ("turn_on", "turn_off", "toggle")
|
||||
]
|
||||
actions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.ACTION, device_entry.id
|
||||
)
|
||||
assert actions == unordered(expected_actions)
|
||||
|
||||
|
||||
async def test_get_action_capabilities(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a light action."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
# Test with entity without optional capabilities
|
||||
entity_id = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"5678",
|
||||
device_id=device_entry.id,
|
||||
).entity_id
|
||||
actions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.ACTION, device_entry.id
|
||||
)
|
||||
assert len(actions) == 3
|
||||
action_types = {action["type"] for action in actions}
|
||||
assert action_types == {"turn_on", "toggle", "turn_off"}
|
||||
for action in actions:
|
||||
capabilities = await async_get_device_automation_capabilities(
|
||||
hass, DeviceAutomationType.ACTION, action
|
||||
)
|
||||
assert capabilities == {"extra_fields": []}
|
||||
|
||||
# Test without entity
|
||||
entity_registry.async_remove(entity_id)
|
||||
for action in actions:
|
||||
capabilities = await async_get_device_automation_capabilities(
|
||||
hass, DeviceAutomationType.ACTION, action
|
||||
)
|
||||
assert capabilities in ({"extra_fields": []}, {})
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
"set_state",
|
||||
"expected_actions",
|
||||
"supported_features_reg",
|
||||
"supported_features_state",
|
||||
"capabilities_reg",
|
||||
"attributes_state",
|
||||
"expected_capabilities",
|
||||
),
|
||||
[
|
||||
(
|
||||
False,
|
||||
{
|
||||
"turn_on",
|
||||
"toggle",
|
||||
"turn_off",
|
||||
"brightness_increase",
|
||||
"brightness_decrease",
|
||||
},
|
||||
0,
|
||||
0,
|
||||
{ATTR_SUPPORTED_COLOR_MODES: [ColorMode.BRIGHTNESS]},
|
||||
{},
|
||||
{
|
||||
"turn_on": [
|
||||
{
|
||||
"name": "brightness_pct",
|
||||
"optional": True,
|
||||
"type": "float",
|
||||
"valueMax": 100,
|
||||
"valueMin": 0,
|
||||
}
|
||||
]
|
||||
},
|
||||
),
|
||||
(
|
||||
True,
|
||||
{
|
||||
"turn_on",
|
||||
"toggle",
|
||||
"turn_off",
|
||||
"brightness_increase",
|
||||
"brightness_decrease",
|
||||
},
|
||||
0,
|
||||
0,
|
||||
None,
|
||||
{ATTR_SUPPORTED_COLOR_MODES: [ColorMode.BRIGHTNESS]},
|
||||
{
|
||||
"turn_on": [
|
||||
{
|
||||
"name": "brightness_pct",
|
||||
"optional": True,
|
||||
"type": "float",
|
||||
"valueMax": 100,
|
||||
"valueMin": 0,
|
||||
}
|
||||
]
|
||||
},
|
||||
),
|
||||
(
|
||||
False,
|
||||
{"turn_on", "toggle", "turn_off", "flash"},
|
||||
LightEntityFeature.FLASH,
|
||||
0,
|
||||
None,
|
||||
{},
|
||||
{
|
||||
"turn_on": [
|
||||
{
|
||||
"name": "flash",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [("short", "short"), ("long", "long")],
|
||||
}
|
||||
]
|
||||
},
|
||||
),
|
||||
(
|
||||
True,
|
||||
{"turn_on", "toggle", "turn_off", "flash"},
|
||||
0,
|
||||
LightEntityFeature.FLASH,
|
||||
None,
|
||||
{},
|
||||
{
|
||||
"turn_on": [
|
||||
{
|
||||
"name": "flash",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [("short", "short"), ("long", "long")],
|
||||
}
|
||||
]
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_get_action_capabilities_features(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
set_state,
|
||||
expected_actions,
|
||||
supported_features_reg,
|
||||
supported_features_state,
|
||||
capabilities_reg,
|
||||
attributes_state,
|
||||
expected_capabilities,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a light action."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_id = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"5678",
|
||||
device_id=device_entry.id,
|
||||
supported_features=supported_features_reg,
|
||||
capabilities=capabilities_reg,
|
||||
).entity_id
|
||||
if set_state:
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
None,
|
||||
{"supported_features": supported_features_state, **attributes_state},
|
||||
)
|
||||
|
||||
actions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.ACTION, device_entry.id
|
||||
)
|
||||
assert len(actions) == len(expected_actions)
|
||||
action_types = {action["type"] for action in actions}
|
||||
assert action_types == expected_actions
|
||||
for action in actions:
|
||||
capabilities = await async_get_device_automation_capabilities(
|
||||
hass, DeviceAutomationType.ACTION, action
|
||||
)
|
||||
expected = {"extra_fields": expected_capabilities.get(action["type"], [])}
|
||||
assert capabilities == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
"set_state",
|
||||
"expected_actions",
|
||||
"supported_features_reg",
|
||||
"supported_features_state",
|
||||
"capabilities_reg",
|
||||
"attributes_state",
|
||||
"expected_capabilities",
|
||||
),
|
||||
[
|
||||
(
|
||||
False,
|
||||
{
|
||||
"turn_on",
|
||||
"toggle",
|
||||
"turn_off",
|
||||
"brightness_increase",
|
||||
"brightness_decrease",
|
||||
},
|
||||
0,
|
||||
0,
|
||||
{ATTR_SUPPORTED_COLOR_MODES: [ColorMode.BRIGHTNESS]},
|
||||
{},
|
||||
{
|
||||
"turn_on": [
|
||||
{
|
||||
"name": "brightness_pct",
|
||||
"optional": True,
|
||||
"type": "float",
|
||||
"valueMax": 100,
|
||||
"valueMin": 0,
|
||||
}
|
||||
]
|
||||
},
|
||||
),
|
||||
(
|
||||
True,
|
||||
{
|
||||
"turn_on",
|
||||
"toggle",
|
||||
"turn_off",
|
||||
"brightness_increase",
|
||||
"brightness_decrease",
|
||||
},
|
||||
0,
|
||||
0,
|
||||
None,
|
||||
{ATTR_SUPPORTED_COLOR_MODES: [ColorMode.BRIGHTNESS]},
|
||||
{
|
||||
"turn_on": [
|
||||
{
|
||||
"name": "brightness_pct",
|
||||
"optional": True,
|
||||
"type": "float",
|
||||
"valueMax": 100,
|
||||
"valueMin": 0,
|
||||
}
|
||||
]
|
||||
},
|
||||
),
|
||||
(
|
||||
False,
|
||||
{"turn_on", "toggle", "turn_off", "flash"},
|
||||
LightEntityFeature.FLASH,
|
||||
0,
|
||||
None,
|
||||
{},
|
||||
{
|
||||
"turn_on": [
|
||||
{
|
||||
"name": "flash",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [("short", "short"), ("long", "long")],
|
||||
}
|
||||
]
|
||||
},
|
||||
),
|
||||
(
|
||||
True,
|
||||
{"turn_on", "toggle", "turn_off", "flash"},
|
||||
0,
|
||||
LightEntityFeature.FLASH,
|
||||
None,
|
||||
{},
|
||||
{
|
||||
"turn_on": [
|
||||
{
|
||||
"name": "flash",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [("short", "short"), ("long", "long")],
|
||||
}
|
||||
]
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_get_action_capabilities_features_legacy(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
set_state,
|
||||
expected_actions,
|
||||
supported_features_reg,
|
||||
supported_features_state,
|
||||
capabilities_reg,
|
||||
attributes_state,
|
||||
expected_capabilities,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a light action."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_id = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"5678",
|
||||
device_id=device_entry.id,
|
||||
supported_features=supported_features_reg,
|
||||
capabilities=capabilities_reg,
|
||||
).entity_id
|
||||
if set_state:
|
||||
hass.states.async_set(
|
||||
entity_id,
|
||||
None,
|
||||
{"supported_features": supported_features_state, **attributes_state},
|
||||
)
|
||||
|
||||
actions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.ACTION, device_entry.id
|
||||
)
|
||||
assert len(actions) == len(expected_actions)
|
||||
action_types = {action["type"] for action in actions}
|
||||
assert action_types == expected_actions
|
||||
for action in actions:
|
||||
action["entity_id"] = entity_registry.async_get(action["entity_id"]).entity_id
|
||||
capabilities = await async_get_device_automation_capabilities(
|
||||
hass, DeviceAutomationType.ACTION, action
|
||||
)
|
||||
expected = {"extra_fields": expected_capabilities.get(action["type"], [])}
|
||||
assert capabilities == expected
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
async def test_action(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test for turn_on and turn_off actions."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_off"},
|
||||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "turn_off",
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_on"},
|
||||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "turn_on",
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_toggle"},
|
||||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "toggle",
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_flash_short"},
|
||||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "flash",
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_flash_long"},
|
||||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "flash",
|
||||
"flash": "long",
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "event",
|
||||
"event_type": "test_brightness_increase",
|
||||
},
|
||||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "brightness_increase",
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "event",
|
||||
"event_type": "test_brightness_decrease",
|
||||
},
|
||||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "brightness_decrease",
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_brightness"},
|
||||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "turn_on",
|
||||
"brightness_pct": 75,
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
turn_on_calls = async_mock_service(hass, DOMAIN, "turn_on")
|
||||
turn_off_calls = async_mock_service(hass, DOMAIN, "turn_off")
|
||||
toggle_calls = async_mock_service(hass, DOMAIN, "toggle")
|
||||
|
||||
hass.bus.async_fire("test_toggle")
|
||||
await hass.async_block_till_done()
|
||||
assert len(toggle_calls) == 1
|
||||
assert toggle_calls[-1].data == {"entity_id": entry.entity_id}
|
||||
|
||||
hass.bus.async_fire("test_off")
|
||||
await hass.async_block_till_done()
|
||||
assert len(turn_off_calls) == 1
|
||||
assert turn_off_calls[-1].data == {"entity_id": entry.entity_id}
|
||||
|
||||
hass.bus.async_fire("test_brightness_increase")
|
||||
await hass.async_block_till_done()
|
||||
assert len(turn_on_calls) == 1
|
||||
assert turn_on_calls[-1].data == {
|
||||
"entity_id": entry.entity_id,
|
||||
"brightness_step_pct": 10,
|
||||
}
|
||||
|
||||
hass.bus.async_fire("test_brightness_decrease")
|
||||
await hass.async_block_till_done()
|
||||
assert len(turn_on_calls) == 2
|
||||
assert turn_on_calls[-1].data == {
|
||||
"entity_id": entry.entity_id,
|
||||
"brightness_step_pct": -10,
|
||||
}
|
||||
|
||||
hass.bus.async_fire("test_brightness")
|
||||
await hass.async_block_till_done()
|
||||
assert len(turn_on_calls) == 3
|
||||
assert turn_on_calls[-1].data == {
|
||||
"entity_id": entry.entity_id,
|
||||
"brightness_pct": 75,
|
||||
}
|
||||
|
||||
hass.bus.async_fire("test_on")
|
||||
await hass.async_block_till_done()
|
||||
assert len(turn_on_calls) == 4
|
||||
assert turn_on_calls[-1].data == {"entity_id": entry.entity_id}
|
||||
|
||||
hass.bus.async_fire("test_flash_short")
|
||||
await hass.async_block_till_done()
|
||||
assert len(turn_on_calls) == 5
|
||||
assert turn_on_calls[-1].data == {
|
||||
"entity_id": entry.entity_id,
|
||||
"flash": FLASH_SHORT,
|
||||
}
|
||||
|
||||
hass.bus.async_fire("test_flash_long")
|
||||
await hass.async_block_till_done()
|
||||
assert len(turn_on_calls) == 6
|
||||
assert turn_on_calls[-1].data == {"entity_id": entry.entity_id, "flash": FLASH_LONG}
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
async def test_action_legacy(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test for turn_on and turn_off actions."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_off"},
|
||||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.entity_id,
|
||||
"type": "turn_off",
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
turn_off_calls = async_mock_service(hass, DOMAIN, "turn_off")
|
||||
|
||||
hass.bus.async_fire("test_off")
|
||||
await hass.async_block_till_done()
|
||||
assert len(turn_off_calls) == 1
|
||||
assert turn_off_calls[-1].data == {"entity_id": entry.entity_id}
|
||||
403
home-assistant/tests/components/light/test_device_condition.py
Normal file
403
home-assistant/tests/components/light/test_device_condition.py
Normal file
@@ -0,0 +1,403 @@
|
||||
"""The test for light device automation."""
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from freezegun import freeze_time
|
||||
import pytest
|
||||
from pytest_unordered import unordered
|
||||
|
||||
from homeassistant.components import automation
|
||||
from homeassistant.components.device_automation import DeviceAutomationType
|
||||
from homeassistant.components.light import DOMAIN
|
||||
from homeassistant.const import CONF_PLATFORM, STATE_OFF, STATE_ON, EntityCategory
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.entity_registry import RegistryEntryHider
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from .common import MockLight
|
||||
|
||||
from tests.common import (
|
||||
MockConfigEntry,
|
||||
async_get_device_automation_capabilities,
|
||||
async_get_device_automations,
|
||||
setup_test_component_platform,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
|
||||
def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
|
||||
"""Stub copying the blueprints to the config folder."""
|
||||
|
||||
|
||||
async def test_get_conditions(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test we get the expected conditions from a light."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
expected_conditions = [
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": condition,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": False},
|
||||
}
|
||||
for condition in ("is_off", "is_on")
|
||||
]
|
||||
conditions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.CONDITION, device_entry.id
|
||||
)
|
||||
assert conditions == expected_conditions
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("hidden_by", "entity_category"),
|
||||
[
|
||||
(RegistryEntryHider.INTEGRATION, None),
|
||||
(RegistryEntryHider.USER, None),
|
||||
(None, EntityCategory.CONFIG),
|
||||
(None, EntityCategory.DIAGNOSTIC),
|
||||
],
|
||||
)
|
||||
async def test_get_conditions_hidden_auxiliary(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
hidden_by,
|
||||
entity_category,
|
||||
) -> None:
|
||||
"""Test we get the expected conditions from a hidden or auxiliary entity."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"5678",
|
||||
device_id=device_entry.id,
|
||||
entity_category=entity_category,
|
||||
hidden_by=hidden_by,
|
||||
)
|
||||
expected_conditions = [
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": condition,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": True},
|
||||
}
|
||||
for condition in ("is_off", "is_on")
|
||||
]
|
||||
conditions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.CONDITION, device_entry.id
|
||||
)
|
||||
assert conditions == unordered(expected_conditions)
|
||||
|
||||
|
||||
async def test_get_condition_capabilities(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a light condition."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
expected_capabilities = {
|
||||
"extra_fields": [
|
||||
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
|
||||
]
|
||||
}
|
||||
conditions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.CONDITION, device_entry.id
|
||||
)
|
||||
for condition in conditions:
|
||||
capabilities = await async_get_device_automation_capabilities(
|
||||
hass, DeviceAutomationType.CONDITION, condition
|
||||
)
|
||||
assert capabilities == expected_capabilities
|
||||
|
||||
|
||||
async def test_get_condition_capabilities_legacy(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a light condition."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
expected_capabilities = {
|
||||
"extra_fields": [
|
||||
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
|
||||
]
|
||||
}
|
||||
conditions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.CONDITION, device_entry.id
|
||||
)
|
||||
for condition in conditions:
|
||||
condition["entity_id"] = entity_registry.async_get(
|
||||
condition["entity_id"]
|
||||
).entity_id
|
||||
capabilities = await async_get_device_automation_capabilities(
|
||||
hass, DeviceAutomationType.CONDITION, condition
|
||||
)
|
||||
assert capabilities == expected_capabilities
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
async def test_if_state(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
service_calls: list[ServiceCall],
|
||||
) -> None:
|
||||
"""Test for turn_on and turn_off conditions."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_ON)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_event1"},
|
||||
"condition": [
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "is_on",
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": (
|
||||
"is_on {{ trigger.platform }}"
|
||||
" - {{ trigger.event.event_type }}"
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_event2"},
|
||||
"condition": [
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "is_off",
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": (
|
||||
"is_off {{ trigger.platform }}"
|
||||
" - {{ trigger.event.event_type }}"
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
hass.bus.async_fire("test_event1")
|
||||
hass.bus.async_fire("test_event2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
assert service_calls[0].data["some"] == "is_on event - test_event1"
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_OFF)
|
||||
hass.bus.async_fire("test_event1")
|
||||
hass.bus.async_fire("test_event2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 2
|
||||
assert service_calls[1].data["some"] == "is_off event - test_event2"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
async def test_if_state_legacy(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
service_calls: list[ServiceCall],
|
||||
) -> None:
|
||||
"""Test for turn_on and turn_off conditions."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_ON)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_event1"},
|
||||
"condition": [
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.entity_id,
|
||||
"type": "is_on",
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": (
|
||||
"is_on {{ trigger.platform }}"
|
||||
" - {{ trigger.event.event_type }}"
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
hass.bus.async_fire("test_event1")
|
||||
hass.bus.async_fire("test_event2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
assert service_calls[0].data["some"] == "is_on event - test_event1"
|
||||
|
||||
|
||||
async def test_if_fires_on_for_condition(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
service_calls: list[ServiceCall],
|
||||
mock_light_entities: list[MockLight],
|
||||
) -> None:
|
||||
"""Test for firing if condition is on with delay."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_ON)
|
||||
|
||||
point1 = dt_util.utcnow()
|
||||
point2 = point1 + timedelta(seconds=10)
|
||||
point3 = point2 + timedelta(seconds=10)
|
||||
|
||||
setup_test_component_platform(hass, DOMAIN, mock_light_entities)
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with freeze_time(point1) as freezer:
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_event1"},
|
||||
"condition": {
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "is_off",
|
||||
"for": {"seconds": 5},
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": (
|
||||
"is_off {{ trigger.platform }}"
|
||||
" - {{ trigger.event.event_type }}"
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
hass.bus.async_fire("test_event1")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Time travel 10 secs into the future
|
||||
freezer.move_to(point2)
|
||||
hass.bus.async_fire("test_event1")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_OFF)
|
||||
hass.bus.async_fire("test_event1")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
# Time travel 20 secs into the future
|
||||
freezer.move_to(point3)
|
||||
hass.bus.async_fire("test_event1")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
assert service_calls[0].data["some"] == "is_off event - test_event1"
|
||||
385
home-assistant/tests/components/light/test_device_trigger.py
Normal file
385
home-assistant/tests/components/light/test_device_trigger.py
Normal file
@@ -0,0 +1,385 @@
|
||||
"""The test for light device automation."""
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
from pytest_unordered import unordered
|
||||
|
||||
from homeassistant.components import automation
|
||||
from homeassistant.components.device_automation import DeviceAutomationType
|
||||
from homeassistant.components.light import DOMAIN
|
||||
from homeassistant.const import STATE_OFF, STATE_ON, EntityCategory
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.entity_registry import RegistryEntryHider
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import (
|
||||
MockConfigEntry,
|
||||
async_fire_time_changed,
|
||||
async_get_device_automation_capabilities,
|
||||
async_get_device_automations,
|
||||
)
|
||||
|
||||
DATA_TEMPLATE_ATTRIBUTES = (
|
||||
"{{ trigger.platform }}"
|
||||
" - {{ trigger.entity_id }}"
|
||||
" - {{ trigger.from_state.state }}"
|
||||
" - {{ trigger.to_state.state }}"
|
||||
" - {{ trigger.for }}"
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
|
||||
def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
|
||||
"""Stub copying the blueprints to the config folder."""
|
||||
|
||||
|
||||
async def test_get_triggers(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test we get the expected triggers from a light."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
expected_triggers = [
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": trigger,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": False},
|
||||
}
|
||||
for trigger in ("changed_states", "turned_off", "turned_on")
|
||||
]
|
||||
triggers = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.TRIGGER, device_entry.id
|
||||
)
|
||||
assert triggers == unordered(expected_triggers)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("hidden_by", "entity_category"),
|
||||
[
|
||||
(RegistryEntryHider.INTEGRATION, None),
|
||||
(RegistryEntryHider.USER, None),
|
||||
(None, EntityCategory.CONFIG),
|
||||
(None, EntityCategory.DIAGNOSTIC),
|
||||
],
|
||||
)
|
||||
async def test_get_triggers_hidden_auxiliary(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
hidden_by,
|
||||
entity_category,
|
||||
) -> None:
|
||||
"""Test we get the expected triggers from a hidden or auxiliary entity."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"5678",
|
||||
device_id=device_entry.id,
|
||||
entity_category=entity_category,
|
||||
hidden_by=hidden_by,
|
||||
)
|
||||
expected_triggers = [
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": trigger,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": True},
|
||||
}
|
||||
for trigger in ("changed_states", "turned_off", "turned_on")
|
||||
]
|
||||
triggers = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.TRIGGER, device_entry.id
|
||||
)
|
||||
assert triggers == unordered(expected_triggers)
|
||||
|
||||
|
||||
async def test_get_trigger_capabilities(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a light trigger."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
expected_capabilities = {
|
||||
"extra_fields": [
|
||||
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
|
||||
]
|
||||
}
|
||||
triggers = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.TRIGGER, device_entry.id
|
||||
)
|
||||
for trigger in triggers:
|
||||
capabilities = await async_get_device_automation_capabilities(
|
||||
hass, DeviceAutomationType.TRIGGER, trigger
|
||||
)
|
||||
assert capabilities == expected_capabilities
|
||||
|
||||
|
||||
async def test_get_trigger_capabilities_legacy(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a light trigger."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
expected_capabilities = {
|
||||
"extra_fields": [
|
||||
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
|
||||
]
|
||||
}
|
||||
triggers = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.TRIGGER, device_entry.id
|
||||
)
|
||||
for trigger in triggers:
|
||||
trigger["entity_id"] = entity_registry.async_get(trigger["entity_id"]).entity_id
|
||||
capabilities = await async_get_device_automation_capabilities(
|
||||
hass, DeviceAutomationType.TRIGGER, trigger
|
||||
)
|
||||
assert capabilities == expected_capabilities
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
async def test_if_fires_on_state_change(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
service_calls: list[ServiceCall],
|
||||
) -> None:
|
||||
"""Test for turn_on and turn_off triggers firing."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_ON)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "turned_on",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "turn_on " + DATA_TEMPLATE_ATTRIBUTES
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "turned_off",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "turn_off " + DATA_TEMPLATE_ATTRIBUTES
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "changed_states",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "turn_on_or_off " + DATA_TEMPLATE_ATTRIBUTES
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_OFF)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 2
|
||||
assert {service_calls[0].data["some"], service_calls[1].data["some"]} == {
|
||||
f"turn_off device - {entry.entity_id} - on - off - None",
|
||||
f"turn_on_or_off device - {entry.entity_id} - on - off - None",
|
||||
}
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_ON)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 4
|
||||
assert {service_calls[2].data["some"], service_calls[3].data["some"]} == {
|
||||
f"turn_on device - {entry.entity_id} - off - on - None",
|
||||
f"turn_on_or_off device - {entry.entity_id} - off - on - None",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
async def test_if_fires_on_state_change_legacy(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
service_calls: list[ServiceCall],
|
||||
) -> None:
|
||||
"""Test for turn_on and turn_off triggers firing."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_ON)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.entity_id,
|
||||
"type": "turned_off",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "turn_on " + DATA_TEMPLATE_ATTRIBUTES
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_OFF)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
assert (
|
||||
service_calls[0].data["some"]
|
||||
== f"turn_on device - {entry.entity_id} - on - off - None"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
async def test_if_fires_on_state_change_with_for(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
service_calls: list[ServiceCall],
|
||||
) -> None:
|
||||
"""Test for triggers firing with delay."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_ON)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "turned_off",
|
||||
"for": {"seconds": 5},
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "turn_off " + DATA_TEMPLATE_ATTRIBUTES
|
||||
},
|
||||
},
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
hass.states.async_set(entry.entity_id, STATE_OFF)
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
await hass.async_block_till_done()
|
||||
assert (
|
||||
service_calls[0].data["some"]
|
||||
== f"turn_off device - {entry.entity_id} - on - off - 0:00:05"
|
||||
)
|
||||
3001
home-assistant/tests/components/light/test_init.py
Normal file
3001
home-assistant/tests/components/light/test_init.py
Normal file
File diff suppressed because it is too large
Load Diff
91
home-assistant/tests/components/light/test_intent.py
Normal file
91
home-assistant/tests/components/light/test_intent.py
Normal file
@@ -0,0 +1,91 @@
|
||||
"""Tests for the light intents."""
|
||||
|
||||
from homeassistant.components import light
|
||||
from homeassistant.components.light import ATTR_SUPPORTED_COLOR_MODES, ColorMode, intent
|
||||
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.intent import async_handle
|
||||
|
||||
from tests.common import async_mock_service
|
||||
|
||||
|
||||
async def test_intent_set_color(hass: HomeAssistant) -> None:
|
||||
"""Test the set color intent."""
|
||||
hass.states.async_set(
|
||||
"light.hello_2", "off", {ATTR_SUPPORTED_COLOR_MODES: [ColorMode.HS]}
|
||||
)
|
||||
hass.states.async_set("switch.hello", "off")
|
||||
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
|
||||
await intent.async_setup_intents(hass)
|
||||
|
||||
await async_handle(
|
||||
hass,
|
||||
"test",
|
||||
intent.INTENT_SET,
|
||||
{"name": {"value": "Hello 2"}, "color": {"value": "blue"}},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
call = calls[0]
|
||||
assert call.domain == light.DOMAIN
|
||||
assert call.service == SERVICE_TURN_ON
|
||||
assert call.data.get(ATTR_ENTITY_ID) == "light.hello_2"
|
||||
assert call.data.get(light.ATTR_RGB_COLOR) == (0, 0, 255)
|
||||
|
||||
|
||||
async def test_intent_set_color_and_brightness(hass: HomeAssistant) -> None:
|
||||
"""Test the set color intent."""
|
||||
hass.states.async_set(
|
||||
"light.hello_2", "off", {ATTR_SUPPORTED_COLOR_MODES: [ColorMode.HS]}
|
||||
)
|
||||
hass.states.async_set("switch.hello", "off")
|
||||
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
|
||||
await intent.async_setup_intents(hass)
|
||||
|
||||
await async_handle(
|
||||
hass,
|
||||
"test",
|
||||
intent.INTENT_SET,
|
||||
{
|
||||
"name": {"value": "Hello 2"},
|
||||
"color": {"value": "blue"},
|
||||
"brightness": {"value": "20"},
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
call = calls[0]
|
||||
assert call.domain == light.DOMAIN
|
||||
assert call.service == SERVICE_TURN_ON
|
||||
assert call.data.get(ATTR_ENTITY_ID) == "light.hello_2"
|
||||
assert call.data.get(light.ATTR_RGB_COLOR) == (0, 0, 255)
|
||||
assert call.data.get(light.ATTR_BRIGHTNESS_PCT) == 20
|
||||
|
||||
|
||||
async def test_intent_set_temperature(hass: HomeAssistant) -> None:
|
||||
"""Test setting the color temperature in kevin via intent."""
|
||||
hass.states.async_set(
|
||||
"light.test", "off", {ATTR_SUPPORTED_COLOR_MODES: [ColorMode.COLOR_TEMP]}
|
||||
)
|
||||
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
|
||||
await intent.async_setup_intents(hass)
|
||||
|
||||
await async_handle(
|
||||
hass,
|
||||
"test",
|
||||
intent.INTENT_SET,
|
||||
{
|
||||
"name": {"value": "Test"},
|
||||
"temperature": {"value": 2000},
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
call = calls[0]
|
||||
assert call.domain == light.DOMAIN
|
||||
assert call.service == SERVICE_TURN_ON
|
||||
assert call.data.get(ATTR_ENTITY_ID) == "light.test"
|
||||
assert call.data.get(light.ATTR_COLOR_TEMP_KELVIN) == 2000
|
||||
85
home-assistant/tests/components/light/test_recorder.py
Normal file
85
home-assistant/tests/components/light/test_recorder.py
Normal file
@@ -0,0 +1,85 @@
|
||||
"""The tests for light recorder."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import light
|
||||
from homeassistant.components.light import (
|
||||
_DEPRECATED_ATTR_COLOR_TEMP,
|
||||
_DEPRECATED_ATTR_MAX_MIREDS,
|
||||
_DEPRECATED_ATTR_MIN_MIREDS,
|
||||
ATTR_BRIGHTNESS,
|
||||
ATTR_COLOR_MODE,
|
||||
ATTR_COLOR_TEMP_KELVIN,
|
||||
ATTR_EFFECT,
|
||||
ATTR_EFFECT_LIST,
|
||||
ATTR_HS_COLOR,
|
||||
ATTR_MAX_COLOR_TEMP_KELVIN,
|
||||
ATTR_MIN_COLOR_TEMP_KELVIN,
|
||||
ATTR_RGB_COLOR,
|
||||
ATTR_RGBW_COLOR,
|
||||
ATTR_RGBWW_COLOR,
|
||||
ATTR_SUPPORTED_COLOR_MODES,
|
||||
ATTR_XY_COLOR,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.components.recorder import Recorder
|
||||
from homeassistant.components.recorder.history import get_significant_states
|
||||
from homeassistant.const import ATTR_FRIENDLY_NAME, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
from tests.components.recorder.common import async_wait_recording_done
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def light_only() -> None:
|
||||
"""Enable only the light platform."""
|
||||
with patch(
|
||||
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
|
||||
[Platform.LIGHT],
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
async def test_exclude_attributes(recorder_mock: Recorder, hass: HomeAssistant) -> None:
|
||||
"""Test light registered attributes to be excluded."""
|
||||
now = dt_util.utcnow()
|
||||
assert await async_setup_component(hass, "homeassistant", {})
|
||||
await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {"platform": "demo"}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5))
|
||||
await hass.async_block_till_done()
|
||||
await async_wait_recording_done(hass)
|
||||
|
||||
states = await hass.async_add_executor_job(
|
||||
get_significant_states, hass, now, None, hass.states.async_entity_ids(DOMAIN)
|
||||
)
|
||||
assert len(states) >= 1
|
||||
for entity_states in states.values():
|
||||
for state in entity_states:
|
||||
assert _DEPRECATED_ATTR_MIN_MIREDS.value not in state.attributes
|
||||
assert _DEPRECATED_ATTR_MAX_MIREDS.value not in state.attributes
|
||||
assert ATTR_SUPPORTED_COLOR_MODES not in state.attributes
|
||||
assert ATTR_EFFECT_LIST not in state.attributes
|
||||
assert ATTR_FRIENDLY_NAME in state.attributes
|
||||
assert ATTR_MAX_COLOR_TEMP_KELVIN not in state.attributes
|
||||
assert ATTR_MIN_COLOR_TEMP_KELVIN not in state.attributes
|
||||
assert ATTR_BRIGHTNESS not in state.attributes
|
||||
assert ATTR_COLOR_MODE not in state.attributes
|
||||
assert _DEPRECATED_ATTR_COLOR_TEMP.value not in state.attributes
|
||||
assert ATTR_COLOR_TEMP_KELVIN not in state.attributes
|
||||
assert ATTR_EFFECT not in state.attributes
|
||||
assert ATTR_HS_COLOR not in state.attributes
|
||||
assert ATTR_RGB_COLOR not in state.attributes
|
||||
assert ATTR_RGBW_COLOR not in state.attributes
|
||||
assert ATTR_RGBWW_COLOR not in state.attributes
|
||||
assert ATTR_XY_COLOR not in state.attributes
|
||||
288
home-assistant/tests/components/light/test_reproduce_state.py
Normal file
288
home-assistant/tests/components/light/test_reproduce_state.py
Normal file
@@ -0,0 +1,288 @@
|
||||
"""Test reproduce state for Light."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import light
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers.state import async_reproduce_state
|
||||
|
||||
from tests.common import async_mock_service
|
||||
|
||||
VALID_BRIGHTNESS = {"brightness": 180}
|
||||
VALID_EFFECT = {"effect": "random"}
|
||||
VALID_COLOR_TEMP_KELVIN = {"color_temp_kelvin": 4200}
|
||||
VALID_HS_COLOR = {"hs_color": (345, 75)}
|
||||
VALID_RGB_COLOR = {"rgb_color": (255, 63, 111)}
|
||||
VALID_RGBW_COLOR = {"rgbw_color": (255, 63, 111, 10)}
|
||||
VALID_RGBWW_COLOR = {"rgbww_color": (255, 63, 111, 10, 20)}
|
||||
VALID_XY_COLOR = {"xy_color": (0.59, 0.274)}
|
||||
|
||||
NONE_BRIGHTNESS = {"brightness": None}
|
||||
NONE_EFFECT = {"effect": None}
|
||||
NONE_COLOR_TEMP_KELVIN = {"color_temp_kelvin": None}
|
||||
NONE_HS_COLOR = {"hs_color": None}
|
||||
NONE_RGB_COLOR = {"rgb_color": None}
|
||||
NONE_RGBW_COLOR = {"rgbw_color": None}
|
||||
NONE_RGBWW_COLOR = {"rgbww_color": None}
|
||||
NONE_XY_COLOR = {"xy_color": None}
|
||||
|
||||
|
||||
async def test_reproducing_states(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test reproducing Light states."""
|
||||
hass.states.async_set("light.entity_off", "off", {})
|
||||
hass.states.async_set("light.entity_bright", "on", VALID_BRIGHTNESS)
|
||||
hass.states.async_set("light.entity_effect", "on", VALID_EFFECT)
|
||||
hass.states.async_set("light.entity_temp", "on", VALID_COLOR_TEMP_KELVIN)
|
||||
hass.states.async_set("light.entity_hs", "on", VALID_HS_COLOR)
|
||||
hass.states.async_set("light.entity_rgb", "on", VALID_RGB_COLOR)
|
||||
hass.states.async_set("light.entity_xy", "on", VALID_XY_COLOR)
|
||||
|
||||
turn_on_calls = async_mock_service(hass, "light", "turn_on")
|
||||
turn_off_calls = async_mock_service(hass, "light", "turn_off")
|
||||
|
||||
# These calls should do nothing as entities already in desired state
|
||||
await async_reproduce_state(
|
||||
hass,
|
||||
[
|
||||
State("light.entity_off", "off"),
|
||||
State("light.entity_bright", "on", VALID_BRIGHTNESS),
|
||||
State("light.entity_effect", "on", VALID_EFFECT),
|
||||
State("light.entity_temp", "on", VALID_COLOR_TEMP_KELVIN),
|
||||
State("light.entity_hs", "on", VALID_HS_COLOR),
|
||||
State("light.entity_rgb", "on", VALID_RGB_COLOR),
|
||||
State("light.entity_xy", "on", VALID_XY_COLOR),
|
||||
],
|
||||
)
|
||||
|
||||
assert len(turn_on_calls) == 0
|
||||
assert len(turn_off_calls) == 0
|
||||
|
||||
# Test invalid state is handled
|
||||
await async_reproduce_state(hass, [State("light.entity_off", "not_supported")])
|
||||
|
||||
assert "not_supported" in caplog.text
|
||||
assert len(turn_on_calls) == 0
|
||||
assert len(turn_off_calls) == 0
|
||||
|
||||
# Make sure correct services are called
|
||||
await async_reproduce_state(
|
||||
hass,
|
||||
[
|
||||
State("light.entity_xy", "off"),
|
||||
State("light.entity_off", "on", VALID_BRIGHTNESS),
|
||||
State("light.entity_bright", "on", VALID_EFFECT),
|
||||
State("light.entity_effect", "on", VALID_COLOR_TEMP_KELVIN),
|
||||
State("light.entity_temp", "on", VALID_HS_COLOR),
|
||||
State("light.entity_hs", "on", VALID_RGB_COLOR),
|
||||
State("light.entity_rgb", "on", VALID_XY_COLOR),
|
||||
],
|
||||
)
|
||||
|
||||
assert len(turn_on_calls) == 6
|
||||
|
||||
expected_calls = []
|
||||
|
||||
expected_off = dict(VALID_BRIGHTNESS)
|
||||
expected_off["entity_id"] = "light.entity_off"
|
||||
expected_calls.append(expected_off)
|
||||
|
||||
expected_bright = dict(VALID_EFFECT)
|
||||
expected_bright["entity_id"] = "light.entity_bright"
|
||||
expected_calls.append(expected_bright)
|
||||
|
||||
expected_effect = dict(VALID_COLOR_TEMP_KELVIN)
|
||||
expected_effect["entity_id"] = "light.entity_effect"
|
||||
expected_calls.append(expected_effect)
|
||||
|
||||
expected_temp = dict(VALID_HS_COLOR)
|
||||
expected_temp["entity_id"] = "light.entity_temp"
|
||||
expected_calls.append(expected_temp)
|
||||
|
||||
expected_hs = dict(VALID_RGB_COLOR)
|
||||
expected_hs["entity_id"] = "light.entity_hs"
|
||||
expected_calls.append(expected_hs)
|
||||
|
||||
expected_rgb = dict(VALID_XY_COLOR)
|
||||
expected_rgb["entity_id"] = "light.entity_rgb"
|
||||
expected_calls.append(expected_rgb)
|
||||
|
||||
for call in turn_on_calls:
|
||||
assert call.domain == "light"
|
||||
found = False
|
||||
for expected in expected_calls:
|
||||
if call.data["entity_id"] == expected["entity_id"]:
|
||||
# We found the matching entry
|
||||
assert call.data == expected
|
||||
found = True
|
||||
break
|
||||
# No entry found
|
||||
assert found
|
||||
|
||||
assert len(turn_off_calls) == 1
|
||||
assert turn_off_calls[0].domain == "light"
|
||||
assert turn_off_calls[0].data == {"entity_id": "light.entity_xy"}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"color_mode",
|
||||
[
|
||||
light.ColorMode.COLOR_TEMP,
|
||||
light.ColorMode.BRIGHTNESS,
|
||||
light.ColorMode.HS,
|
||||
light.ColorMode.ONOFF,
|
||||
light.ColorMode.RGB,
|
||||
light.ColorMode.RGBW,
|
||||
light.ColorMode.RGBWW,
|
||||
light.ColorMode.UNKNOWN,
|
||||
light.ColorMode.WHITE,
|
||||
light.ColorMode.XY,
|
||||
],
|
||||
)
|
||||
async def test_filter_color_modes(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, color_mode
|
||||
) -> None:
|
||||
"""Test filtering of parameters according to color mode."""
|
||||
hass.states.async_set("light.entity", "off", {})
|
||||
all_colors = {
|
||||
**VALID_COLOR_TEMP_KELVIN,
|
||||
**VALID_HS_COLOR,
|
||||
**VALID_RGB_COLOR,
|
||||
**VALID_RGBW_COLOR,
|
||||
**VALID_RGBWW_COLOR,
|
||||
**VALID_XY_COLOR,
|
||||
**VALID_BRIGHTNESS,
|
||||
}
|
||||
|
||||
turn_on_calls = async_mock_service(hass, "light", "turn_on")
|
||||
|
||||
await async_reproduce_state(
|
||||
hass, [State("light.entity", "on", {**all_colors, "color_mode": color_mode})]
|
||||
)
|
||||
|
||||
expected_map = {
|
||||
light.ColorMode.COLOR_TEMP: {**VALID_BRIGHTNESS, **VALID_COLOR_TEMP_KELVIN},
|
||||
light.ColorMode.BRIGHTNESS: VALID_BRIGHTNESS,
|
||||
light.ColorMode.HS: {**VALID_BRIGHTNESS, **VALID_HS_COLOR},
|
||||
light.ColorMode.ONOFF: {**VALID_BRIGHTNESS},
|
||||
light.ColorMode.RGB: {**VALID_BRIGHTNESS, **VALID_RGB_COLOR},
|
||||
light.ColorMode.RGBW: {**VALID_BRIGHTNESS, **VALID_RGBW_COLOR},
|
||||
light.ColorMode.RGBWW: {**VALID_BRIGHTNESS, **VALID_RGBWW_COLOR},
|
||||
light.ColorMode.UNKNOWN: {
|
||||
**VALID_BRIGHTNESS,
|
||||
**VALID_HS_COLOR,
|
||||
},
|
||||
light.ColorMode.WHITE: {
|
||||
**VALID_BRIGHTNESS,
|
||||
light.ATTR_WHITE: VALID_BRIGHTNESS[light.ATTR_BRIGHTNESS],
|
||||
},
|
||||
light.ColorMode.XY: {**VALID_BRIGHTNESS, **VALID_XY_COLOR},
|
||||
}
|
||||
expected = expected_map[color_mode]
|
||||
|
||||
assert len(turn_on_calls) == 1
|
||||
assert turn_on_calls[0].domain == "light"
|
||||
assert dict(turn_on_calls[0].data) == {"entity_id": "light.entity", **expected}
|
||||
|
||||
# This should do nothing, the light is already in the desired state
|
||||
hass.states.async_set("light.entity", "on", {"color_mode": color_mode, **expected})
|
||||
await async_reproduce_state(
|
||||
hass, [State("light.entity", "on", {**expected, "color_mode": color_mode})]
|
||||
)
|
||||
assert len(turn_on_calls) == 1
|
||||
|
||||
|
||||
async def test_filter_color_modes_missing_attributes(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test warning on missing attribute when filtering for color mode."""
|
||||
color_mode = light.ColorMode.COLOR_TEMP
|
||||
hass.states.async_set("light.entity", "off", {})
|
||||
expected_log = (
|
||||
"Color mode color_temp specified "
|
||||
"but attribute color_temp_kelvin missing for: light.entity"
|
||||
)
|
||||
expected_fallback_log = "using color_temp (mireds) as fallback"
|
||||
|
||||
turn_on_calls = async_mock_service(hass, "light", "turn_on")
|
||||
|
||||
all_colors = {
|
||||
**VALID_COLOR_TEMP_KELVIN,
|
||||
**VALID_HS_COLOR,
|
||||
**VALID_RGB_COLOR,
|
||||
**VALID_RGBW_COLOR,
|
||||
**VALID_RGBWW_COLOR,
|
||||
**VALID_XY_COLOR,
|
||||
**VALID_BRIGHTNESS,
|
||||
}
|
||||
|
||||
# Test missing `color_temp_kelvin` attribute
|
||||
stored_attributes = {**all_colors}
|
||||
stored_attributes.pop("color_temp_kelvin")
|
||||
caplog.clear()
|
||||
await async_reproduce_state(
|
||||
hass,
|
||||
[State("light.entity", "on", {**stored_attributes, "color_mode": color_mode})],
|
||||
)
|
||||
assert len(turn_on_calls) == 0
|
||||
assert expected_log in caplog.text
|
||||
assert expected_fallback_log not in caplog.text
|
||||
|
||||
# Test with deprecated `color_temp` attribute
|
||||
stored_attributes["color_temp"] = 250
|
||||
expected = {"brightness": 180, "color_temp_kelvin": 4000}
|
||||
caplog.clear()
|
||||
await async_reproduce_state(
|
||||
hass,
|
||||
[State("light.entity", "on", {**stored_attributes, "color_mode": color_mode})],
|
||||
)
|
||||
|
||||
assert len(turn_on_calls) == 1
|
||||
assert expected_log in caplog.text
|
||||
assert expected_fallback_log in caplog.text
|
||||
|
||||
# Test with correct `color_temp_kelvin` attribute
|
||||
expected = {"brightness": 180, "color_temp_kelvin": 4200}
|
||||
caplog.clear()
|
||||
turn_on_calls.clear()
|
||||
await async_reproduce_state(
|
||||
hass,
|
||||
[State("light.entity", "on", {**all_colors, "color_mode": color_mode})],
|
||||
)
|
||||
assert len(turn_on_calls) == 1
|
||||
assert turn_on_calls[0].domain == "light"
|
||||
assert dict(turn_on_calls[0].data) == {"entity_id": "light.entity", **expected}
|
||||
assert expected_log not in caplog.text
|
||||
assert expected_fallback_log not in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"saved_state",
|
||||
[
|
||||
NONE_BRIGHTNESS,
|
||||
NONE_EFFECT,
|
||||
NONE_COLOR_TEMP_KELVIN,
|
||||
NONE_HS_COLOR,
|
||||
NONE_RGB_COLOR,
|
||||
NONE_RGBW_COLOR,
|
||||
NONE_RGBWW_COLOR,
|
||||
NONE_XY_COLOR,
|
||||
],
|
||||
)
|
||||
async def test_filter_none(hass: HomeAssistant, saved_state) -> None:
|
||||
"""Test filtering of parameters which are None."""
|
||||
hass.states.async_set("light.entity", "off", {})
|
||||
|
||||
turn_on_calls = async_mock_service(hass, "light", "turn_on")
|
||||
|
||||
await async_reproduce_state(hass, [State("light.entity", "on", saved_state)])
|
||||
|
||||
assert len(turn_on_calls) == 1
|
||||
assert turn_on_calls[0].domain == "light"
|
||||
assert dict(turn_on_calls[0].data) == {"entity_id": "light.entity"}
|
||||
|
||||
# This should do nothing, the light is already in the desired state
|
||||
hass.states.async_set("light.entity", "on", {})
|
||||
await async_reproduce_state(hass, [State("light.entity", "on", saved_state)])
|
||||
assert len(turn_on_calls) == 1
|
||||
@@ -0,0 +1,62 @@
|
||||
"""Test the Light significant change platform."""
|
||||
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS,
|
||||
ATTR_COLOR_TEMP_KELVIN,
|
||||
ATTR_EFFECT,
|
||||
ATTR_HS_COLOR,
|
||||
)
|
||||
from homeassistant.components.light.significant_change import (
|
||||
async_check_significant_change,
|
||||
)
|
||||
|
||||
|
||||
async def test_significant_change() -> None:
|
||||
"""Detect Light significant changes."""
|
||||
assert not async_check_significant_change(None, "on", {}, "on", {})
|
||||
assert async_check_significant_change(None, "on", {}, "off", {})
|
||||
|
||||
# Brightness
|
||||
assert not async_check_significant_change(
|
||||
None, "on", {ATTR_BRIGHTNESS: 60}, "on", {ATTR_BRIGHTNESS: 61}
|
||||
)
|
||||
assert async_check_significant_change(
|
||||
None, "on", {ATTR_BRIGHTNESS: 60}, "on", {ATTR_BRIGHTNESS: 63}
|
||||
)
|
||||
|
||||
# Color temp
|
||||
assert not async_check_significant_change(
|
||||
None, "on", {ATTR_COLOR_TEMP_KELVIN: 2000}, "on", {ATTR_COLOR_TEMP_KELVIN: 2049}
|
||||
)
|
||||
assert async_check_significant_change(
|
||||
None, "on", {ATTR_COLOR_TEMP_KELVIN: 2000}, "on", {ATTR_COLOR_TEMP_KELVIN: 2050}
|
||||
)
|
||||
|
||||
# Effect
|
||||
for eff1, eff2, expected in (
|
||||
(None, None, False),
|
||||
(None, "colorloop", True),
|
||||
("colorloop", None, True),
|
||||
("colorloop", "jump", True),
|
||||
("colorloop", "colorloop", False),
|
||||
):
|
||||
result = async_check_significant_change(
|
||||
None, "on", {ATTR_EFFECT: eff1}, "on", {ATTR_EFFECT: eff2}
|
||||
)
|
||||
assert result is expected
|
||||
|
||||
# Hue
|
||||
assert not async_check_significant_change(
|
||||
None, "on", {ATTR_HS_COLOR: [120, 20]}, "on", {ATTR_HS_COLOR: [124, 20]}
|
||||
)
|
||||
assert async_check_significant_change(
|
||||
None, "on", {ATTR_HS_COLOR: [120, 20]}, "on", {ATTR_HS_COLOR: [125, 20]}
|
||||
)
|
||||
|
||||
# Satursation
|
||||
assert not async_check_significant_change(
|
||||
None, "on", {ATTR_HS_COLOR: [120, 20]}, "on", {ATTR_HS_COLOR: [120, 22]}
|
||||
)
|
||||
assert async_check_significant_change(
|
||||
None, "on", {ATTR_HS_COLOR: [120, 20]}, "on", {ATTR_HS_COLOR: [120, 23]}
|
||||
)
|
||||
Reference in New Issue
Block a user