init home-assistant
This commit is contained in:
1
home-assistant/tests/components/switch/__init__.py
Normal file
1
home-assistant/tests/components/switch/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""The tests for Switch platforms."""
|
||||
73
home-assistant/tests/components/switch/common.py
Normal file
73
home-assistant/tests/components/switch/common.py
Normal file
@@ -0,0 +1,73 @@
|
||||
"""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
|
||||
|
||||
from homeassistant.components.switch import DOMAIN, SwitchDeviceClass, SwitchEntity
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ENTITY_MATCH_ALL,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.loader import bind_hass
|
||||
|
||||
|
||||
@bind_hass
|
||||
def turn_on(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None:
|
||||
"""Turn all or specified switch on."""
|
||||
hass.add_job(async_turn_on, hass, entity_id)
|
||||
|
||||
|
||||
async def async_turn_on(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None:
|
||||
"""Turn all or specified switch on."""
|
||||
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
|
||||
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) -> None:
|
||||
"""Turn all or specified switch off."""
|
||||
hass.add_job(async_turn_off, hass, entity_id)
|
||||
|
||||
|
||||
async def async_turn_off(
|
||||
hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL
|
||||
) -> None:
|
||||
"""Turn all or specified switch off."""
|
||||
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
|
||||
await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True)
|
||||
|
||||
|
||||
class MockSwitch(SwitchEntity):
|
||||
"""Mocked switch entity."""
|
||||
|
||||
_attr_device_class = SwitchDeviceClass.SWITCH
|
||||
|
||||
def __init__(self, name: str | None, state: str) -> None:
|
||||
"""Initialize the mock switch entity."""
|
||||
self._attr_name = name
|
||||
self._attr_is_on = state == STATE_ON
|
||||
|
||||
def turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the entity on."""
|
||||
self._attr_is_on = True
|
||||
|
||||
def turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn the entity off."""
|
||||
self._attr_is_on = False
|
||||
|
||||
|
||||
def get_mock_switch_entities() -> list[MockSwitch]:
|
||||
"""Return a list of mock switch entities."""
|
||||
return [
|
||||
MockSwitch("AC", STATE_ON),
|
||||
MockSwitch("AC", STATE_OFF),
|
||||
MockSwitch(None, STATE_OFF),
|
||||
]
|
||||
222
home-assistant/tests/components/switch/test_device_action.py
Normal file
222
home-assistant/tests/components/switch/test_device_action.py
Normal file
@@ -0,0 +1,222 @@
|
||||
"""The test for switch device automation."""
|
||||
|
||||
import pytest
|
||||
from pytest_unordered import unordered
|
||||
|
||||
from homeassistant.components import automation
|
||||
from homeassistant.components.device_automation import DeviceAutomationType
|
||||
from homeassistant.components.switch import DOMAIN
|
||||
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_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 switch."""
|
||||
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_actions = []
|
||||
expected_actions += [
|
||||
{
|
||||
"domain": DOMAIN,
|
||||
"type": action,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": False},
|
||||
}
|
||||
for action in ("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,
|
||||
)
|
||||
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_off", "turn_on", "toggle")
|
||||
]
|
||||
actions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.ACTION, device_entry.id
|
||||
)
|
||||
assert actions == unordered(expected_actions)
|
||||
|
||||
|
||||
@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",
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
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_on")
|
||||
await hass.async_block_till_done()
|
||||
assert len(turn_on_calls) == 1
|
||||
assert turn_on_calls[-1].data == {"entity_id": entry.entity_id}
|
||||
|
||||
|
||||
@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.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}
|
||||
395
home-assistant/tests/components/switch/test_device_condition.py
Normal file
395
home-assistant/tests/components/switch/test_device_condition.py
Normal file
@@ -0,0 +1,395 @@
|
||||
"""The test for switch 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.switch 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_get_device_automation_capabilities,
|
||||
async_get_device_automations,
|
||||
)
|
||||
|
||||
|
||||
@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 switch."""
|
||||
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 == unordered(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 switch 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 switch 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")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
assert service_calls[0].data["some"] == "is_on event - test_event1"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
async def test_if_fires_on_for_condition(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
service_calls: list[ServiceCall],
|
||||
) -> None:
|
||||
"""Test for firing if condition is on with delay."""
|
||||
point1 = dt_util.utcnow()
|
||||
point2 = point1 + timedelta(seconds=10)
|
||||
point3 = point2 + timedelta(seconds=10)
|
||||
|
||||
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)
|
||||
|
||||
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"
|
||||
407
home-assistant/tests/components/switch/test_device_trigger.py
Normal file
407
home-assistant/tests/components/switch/test_device_trigger.py
Normal file
@@ -0,0 +1,407 @@
|
||||
"""The test for switch 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.switch 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,
|
||||
)
|
||||
|
||||
|
||||
@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 switch."""
|
||||
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 switch 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 switch 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 {{ trigger.platform }}"
|
||||
" - {{ trigger.entity_id }}"
|
||||
" - {{ trigger.from_state.state }}"
|
||||
" - {{ trigger.to_state.state }}"
|
||||
" - {{ trigger.for }}"
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"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 {{ trigger.platform }}"
|
||||
" - {{ trigger.entity_id }}"
|
||||
" - {{ trigger.from_state.state }}"
|
||||
" - {{ trigger.to_state.state }}"
|
||||
" - {{ trigger.for }}"
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"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 {{ trigger.platform }}"
|
||||
" - {{ trigger.entity_id }}"
|
||||
" - {{ trigger.from_state.state }}"
|
||||
" - {{ trigger.to_state.state }}"
|
||||
" - {{ trigger.for }}"
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
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_off {{ trigger.platform }}"
|
||||
" - {{ trigger.entity_id }}"
|
||||
" - {{ trigger.from_state.state }}"
|
||||
" - {{ trigger.to_state.state }}"
|
||||
" - {{ trigger.for }}"
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
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_off 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 {{ trigger.platform }}"
|
||||
" - {{ trigger.entity_id }}"
|
||||
" - {{ trigger.from_state.state }}"
|
||||
" - {{ trigger.to_state.state }}"
|
||||
" - {{ trigger.for }}"
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
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"
|
||||
)
|
||||
84
home-assistant/tests/components/switch/test_init.py
Normal file
84
home-assistant/tests/components/switch/test_init.py
Normal file
@@ -0,0 +1,84 @@
|
||||
"""The tests for the Switch component."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import core
|
||||
from homeassistant.components import switch
|
||||
from homeassistant.const import CONF_PLATFORM
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import common
|
||||
from .common import MockSwitch
|
||||
|
||||
from tests.common import MockUser, setup_test_component_platform
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def entities(
|
||||
hass: HomeAssistant, mock_switch_entities: list[MockSwitch]
|
||||
) -> list[MockSwitch]:
|
||||
"""Initialize the test switch."""
|
||||
setup_test_component_platform(hass, switch.DOMAIN, mock_switch_entities)
|
||||
return mock_switch_entities
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
async def test_methods(hass: HomeAssistant, entities: list[MockSwitch]) -> None:
|
||||
"""Test is_on, turn_on, turn_off methods."""
|
||||
switch_1, switch_2, switch_3 = entities
|
||||
assert await async_setup_component(
|
||||
hass, switch.DOMAIN, {switch.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert switch.is_on(hass, switch_1.entity_id)
|
||||
assert not switch.is_on(hass, switch_2.entity_id)
|
||||
assert not switch.is_on(hass, switch_3.entity_id)
|
||||
|
||||
await common.async_turn_off(hass, switch_1.entity_id)
|
||||
await common.async_turn_on(hass, switch_2.entity_id)
|
||||
|
||||
assert not switch.is_on(hass, switch_1.entity_id)
|
||||
assert switch.is_on(hass, switch_2.entity_id)
|
||||
|
||||
# Turn all off
|
||||
await common.async_turn_off(hass)
|
||||
|
||||
assert not switch.is_on(hass, switch_1.entity_id)
|
||||
assert not switch.is_on(hass, switch_2.entity_id)
|
||||
assert not switch.is_on(hass, switch_3.entity_id)
|
||||
|
||||
# Turn all on
|
||||
await common.async_turn_on(hass)
|
||||
|
||||
assert switch.is_on(hass, switch_1.entity_id)
|
||||
assert switch.is_on(hass, switch_2.entity_id)
|
||||
assert switch.is_on(hass, switch_3.entity_id)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
async def test_switch_context(
|
||||
hass: HomeAssistant,
|
||||
entities,
|
||||
hass_admin_user: MockUser,
|
||||
) -> None:
|
||||
"""Test that switch context works."""
|
||||
assert await async_setup_component(hass, "switch", {"switch": {"platform": "test"}})
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("switch.ac")
|
||||
assert state is not None
|
||||
|
||||
await hass.services.async_call(
|
||||
"switch",
|
||||
"toggle",
|
||||
{"entity_id": state.entity_id},
|
||||
True,
|
||||
core.Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
|
||||
state2 = hass.states.get("switch.ac")
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == hass_admin_user.id
|
||||
107
home-assistant/tests/components/switch/test_light.py
Normal file
107
home-assistant/tests/components/switch/test_light.py
Normal file
@@ -0,0 +1,107 @@
|
||||
"""The tests for the Light Switch platform."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.light import (
|
||||
ATTR_COLOR_MODE,
|
||||
ATTR_SUPPORTED_COLOR_MODES,
|
||||
ColorMode,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import common as switch_common
|
||||
|
||||
from tests.components.light import common
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def setup_homeassistant(hass: HomeAssistant):
|
||||
"""Set up the homeassistant integration."""
|
||||
await async_setup_component(hass, "homeassistant", {})
|
||||
|
||||
|
||||
async def test_default_state(hass: HomeAssistant) -> None:
|
||||
"""Test light switch default state."""
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"light",
|
||||
{
|
||||
"light": {
|
||||
"platform": "switch",
|
||||
"entity_id": "switch.test",
|
||||
"name": "Christmas Tree Lights",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("light.christmas_tree_lights")
|
||||
assert state is not None
|
||||
assert state.state == "unavailable"
|
||||
assert state.attributes["supported_features"] == 0
|
||||
assert state.attributes.get("brightness") is None
|
||||
assert state.attributes.get("hs_color") is None
|
||||
assert state.attributes.get("color_temp") is None
|
||||
assert state.attributes.get("effect_list") is None
|
||||
assert state.attributes.get("effect") is None
|
||||
assert state.attributes.get(ATTR_SUPPORTED_COLOR_MODES) == [ColorMode.ONOFF]
|
||||
assert state.attributes.get(ATTR_COLOR_MODE) is None
|
||||
|
||||
|
||||
async def test_light_service_calls(hass: HomeAssistant) -> None:
|
||||
"""Test service calls to light."""
|
||||
await async_setup_component(hass, "switch", {"switch": [{"platform": "demo"}]})
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"light",
|
||||
{"light": [{"platform": "switch", "entity_id": "switch.decorative_lights"}]},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("light.light_switch").state == "on"
|
||||
|
||||
await common.async_toggle(hass, "light.light_switch")
|
||||
|
||||
assert hass.states.get("switch.decorative_lights").state == "off"
|
||||
assert hass.states.get("light.light_switch").state == "off"
|
||||
|
||||
await common.async_turn_on(hass, "light.light_switch")
|
||||
|
||||
assert hass.states.get("switch.decorative_lights").state == "on"
|
||||
assert hass.states.get("light.light_switch").state == "on"
|
||||
assert (
|
||||
hass.states.get("light.light_switch").attributes.get(ATTR_COLOR_MODE)
|
||||
== ColorMode.ONOFF
|
||||
)
|
||||
|
||||
await common.async_turn_off(hass, "light.light_switch")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("switch.decorative_lights").state == "off"
|
||||
assert hass.states.get("light.light_switch").state == "off"
|
||||
|
||||
|
||||
async def test_switch_service_calls(hass: HomeAssistant) -> None:
|
||||
"""Test service calls to switch."""
|
||||
await async_setup_component(hass, "switch", {"switch": [{"platform": "demo"}]})
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"light",
|
||||
{"light": [{"platform": "switch", "entity_id": "switch.decorative_lights"}]},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("light.light_switch").state == "on"
|
||||
|
||||
await switch_common.async_turn_off(hass, "switch.decorative_lights")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("switch.decorative_lights").state == "off"
|
||||
assert hass.states.get("light.light_switch").state == "off"
|
||||
|
||||
await switch_common.async_turn_on(hass, "switch.decorative_lights")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("switch.decorative_lights").state == "on"
|
||||
assert hass.states.get("light.light_switch").state == "on"
|
||||
@@ -0,0 +1,54 @@
|
||||
"""Test reproduce state for Switch."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers.state import async_reproduce_state
|
||||
|
||||
from tests.common import async_mock_service
|
||||
|
||||
|
||||
async def test_reproducing_states(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test reproducing Switch states."""
|
||||
hass.states.async_set("switch.entity_off", "off", {})
|
||||
hass.states.async_set("switch.entity_on", "on", {})
|
||||
|
||||
turn_on_calls = async_mock_service(hass, "switch", "turn_on")
|
||||
turn_off_calls = async_mock_service(hass, "switch", "turn_off")
|
||||
|
||||
# These calls should do nothing as entities already in desired state
|
||||
await async_reproduce_state(
|
||||
hass,
|
||||
[State("switch.entity_off", "off"), State("switch.entity_on", "on", {})],
|
||||
)
|
||||
|
||||
assert len(turn_on_calls) == 0
|
||||
assert len(turn_off_calls) == 0
|
||||
|
||||
# Test invalid state is handled
|
||||
await async_reproduce_state(hass, [State("switch.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("switch.entity_on", "off"),
|
||||
State("switch.entity_off", "on", {}),
|
||||
# Should not raise
|
||||
State("switch.non_existing", "on"),
|
||||
],
|
||||
)
|
||||
|
||||
assert len(turn_on_calls) == 1
|
||||
assert turn_on_calls[0].domain == "switch"
|
||||
assert turn_on_calls[0].data == {"entity_id": "switch.entity_off"}
|
||||
|
||||
assert len(turn_off_calls) == 1
|
||||
assert turn_off_calls[0].domain == "switch"
|
||||
assert turn_off_calls[0].data == {"entity_id": "switch.entity_on"}
|
||||
@@ -0,0 +1,13 @@
|
||||
"""Test the sensor significant change platform."""
|
||||
|
||||
from homeassistant.components.switch.significant_change import (
|
||||
async_check_significant_change,
|
||||
)
|
||||
|
||||
|
||||
async def test_significant_change() -> None:
|
||||
"""Detect Switch significant change."""
|
||||
attrs = {}
|
||||
assert not async_check_significant_change(None, "on", attrs, "on", attrs)
|
||||
assert not async_check_significant_change(None, "off", attrs, "off", attrs)
|
||||
assert async_check_significant_change(None, "on", attrs, "off", attrs)
|
||||
Reference in New Issue
Block a user