Add device automation support to switch entities (#26466)

* Add device automation support to switch entities

* Update switch translations

* Address review comments
This commit is contained in:
Erik Montnemery 2019-09-09 16:55:47 +02:00 committed by GitHub
parent fec6706bf7
commit b14b14c3c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 687 additions and 216 deletions

View file

@ -0,0 +1,189 @@
"""Device automation helpers for toggle entity."""
import voluptuous as vol
import homeassistant.components.automation.state as state
from homeassistant.components.device_automation.const import (
CONF_IS_OFF,
CONF_IS_ON,
CONF_TOGGLE,
CONF_TURN_OFF,
CONF_TURN_ON,
)
from homeassistant.core import split_entity_id
from homeassistant.const import (
CONF_CONDITION,
CONF_DEVICE,
CONF_DEVICE_ID,
CONF_DOMAIN,
CONF_ENTITY_ID,
CONF_PLATFORM,
CONF_TYPE,
)
from homeassistant.helpers.entity_registry import async_entries_for_device
from homeassistant.helpers import condition, config_validation as cv, service
ENTITY_ACTIONS = [
{
# Turn entity off
CONF_DEVICE: None,
CONF_TYPE: CONF_TURN_OFF,
},
{
# Turn entity on
CONF_DEVICE: None,
CONF_TYPE: CONF_TURN_ON,
},
{
# Toggle entity
CONF_DEVICE: None,
CONF_TYPE: CONF_TOGGLE,
},
]
ENTITY_CONDITIONS = [
{
# True when entity is turned off
CONF_CONDITION: "device",
CONF_TYPE: CONF_IS_OFF,
},
{
# True when entity is turned on
CONF_CONDITION: "device",
CONF_TYPE: CONF_IS_ON,
},
]
ENTITY_TRIGGERS = [
{
# Trigger when entity is turned off
CONF_PLATFORM: "device",
CONF_TYPE: CONF_TURN_OFF,
},
{
# Trigger when entity is turned on
CONF_PLATFORM: "device",
CONF_TYPE: CONF_TURN_ON,
},
]
ACTION_SCHEMA = vol.Schema(
{
vol.Required(CONF_DEVICE): None,
vol.Optional(CONF_DEVICE_ID): str,
vol.Required(CONF_DOMAIN): str,
vol.Required(CONF_ENTITY_ID): cv.entity_id,
vol.Required(CONF_TYPE): vol.In([CONF_TOGGLE, CONF_TURN_OFF, CONF_TURN_ON]),
}
)
CONDITION_SCHEMA = vol.Schema(
{
vol.Required(CONF_CONDITION): "device",
vol.Optional(CONF_DEVICE_ID): str,
vol.Required(CONF_DOMAIN): str,
vol.Required(CONF_ENTITY_ID): cv.entity_id,
vol.Required(CONF_TYPE): vol.In([CONF_IS_OFF, CONF_IS_ON]),
}
)
TRIGGER_SCHEMA = vol.Schema(
{
vol.Required(CONF_PLATFORM): "device",
vol.Optional(CONF_DEVICE_ID): str,
vol.Required(CONF_DOMAIN): str,
vol.Required(CONF_ENTITY_ID): cv.entity_id,
vol.Required(CONF_TYPE): vol.In([CONF_TURN_OFF, CONF_TURN_ON]),
}
)
def _is_domain(entity, domain):
return split_entity_id(entity.entity_id)[0] == domain
async def async_call_action_from_config(hass, config, variables, context, domain):
"""Change state based on configuration."""
config = ACTION_SCHEMA(config)
action_type = config[CONF_TYPE]
if action_type == CONF_TURN_ON:
action = "turn_on"
elif action_type == CONF_TURN_OFF:
action = "turn_off"
else:
action = "toggle"
service_action = {
service.CONF_SERVICE: "{}.{}".format(domain, action),
CONF_ENTITY_ID: config[CONF_ENTITY_ID],
}
await service.async_call_from_config(
hass, service_action, blocking=True, variables=variables, context=context
)
def async_condition_from_config(config, config_validation):
"""Evaluate state based on configuration."""
condition_type = config[CONF_TYPE]
if condition_type == CONF_IS_ON:
stat = "on"
else:
stat = "off"
state_config = {
condition.CONF_CONDITION: "state",
condition.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
condition.CONF_STATE: stat,
}
return condition.state_from_config(state_config, config_validation)
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
trigger_type = config[CONF_TYPE]
if trigger_type == CONF_TURN_ON:
from_state = "off"
to_state = "on"
else:
from_state = "on"
to_state = "off"
state_config = {
state.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
state.CONF_FROM: from_state,
state.CONF_TO: to_state,
}
return await state.async_trigger(hass, state_config, action, automation_info)
async def _async_get_automations(hass, device_id, automation_templates, domain):
"""List device automations."""
automations = []
entity_registry = await hass.helpers.entity_registry.async_get_registry()
entities = async_entries_for_device(entity_registry, device_id)
domain_entities = [x for x in entities if _is_domain(x, domain)]
for entity in domain_entities:
for automation in automation_templates:
automation = dict(automation)
automation.update(
device_id=device_id, entity_id=entity.entity_id, domain=domain
)
automations.append(automation)
return automations
async def async_get_actions(hass, device_id, domain):
"""List device actions."""
return await _async_get_automations(hass, device_id, ENTITY_ACTIONS, domain)
async def async_get_conditions(hass, device_id, domain):
"""List device conditions."""
return await _async_get_automations(hass, device_id, ENTITY_CONDITIONS, domain)
async def async_get_triggers(hass, device_id, domain):
"""List device triggers."""
return await _async_get_automations(hass, device_id, ENTITY_TRIGGERS, domain)

View file

@ -1,215 +1,56 @@
"""Provides device automations for lights."""
import voluptuous as vol
import homeassistant.components.automation.state as state
from homeassistant.components.device_automation.const import (
CONF_IS_OFF,
CONF_IS_ON,
CONF_TOGGLE,
CONF_TURN_OFF,
CONF_TURN_ON,
)
from homeassistant.core import split_entity_id
from homeassistant.const import (
CONF_CONDITION,
CONF_DEVICE,
CONF_DEVICE_ID,
CONF_DOMAIN,
CONF_ENTITY_ID,
CONF_PLATFORM,
CONF_TYPE,
)
from homeassistant.helpers import condition, config_validation as cv, service
from homeassistant.helpers.entity_registry import async_entries_for_device
from homeassistant.components.device_automation import toggle_entity
from homeassistant.const import CONF_DOMAIN
from . import DOMAIN
# mypy: allow-untyped-defs, no-check-untyped-defs
ENTITY_ACTIONS = [
{
# Turn light off
CONF_DEVICE: None,
CONF_DOMAIN: DOMAIN,
CONF_TYPE: CONF_TURN_OFF,
},
{
# Turn light on
CONF_DEVICE: None,
CONF_DOMAIN: DOMAIN,
CONF_TYPE: CONF_TURN_ON,
},
{
# Toggle light
CONF_DEVICE: None,
CONF_DOMAIN: DOMAIN,
CONF_TYPE: CONF_TOGGLE,
},
]
ACTION_SCHEMA = toggle_entity.ACTION_SCHEMA.extend({vol.Required(CONF_DOMAIN): DOMAIN})
ENTITY_CONDITIONS = [
{
# True when light is turned off
CONF_CONDITION: "device",
CONF_DOMAIN: DOMAIN,
CONF_TYPE: CONF_IS_OFF,
},
{
# True when light is turned on
CONF_CONDITION: "device",
CONF_DOMAIN: DOMAIN,
CONF_TYPE: CONF_IS_ON,
},
]
ENTITY_TRIGGERS = [
{
# Trigger when light is turned off
CONF_PLATFORM: "device",
CONF_DOMAIN: DOMAIN,
CONF_TYPE: CONF_TURN_OFF,
},
{
# Trigger when light is turned on
CONF_PLATFORM: "device",
CONF_DOMAIN: DOMAIN,
CONF_TYPE: CONF_TURN_ON,
},
]
ACTION_SCHEMA = vol.All(
vol.Schema(
{
vol.Required(CONF_DEVICE): None,
vol.Optional(CONF_DEVICE_ID): str,
vol.Required(CONF_DOMAIN): DOMAIN,
vol.Required(CONF_ENTITY_ID): cv.entity_id,
vol.Required(CONF_TYPE): vol.In([CONF_TOGGLE, CONF_TURN_OFF, CONF_TURN_ON]),
}
)
CONDITION_SCHEMA = toggle_entity.CONDITION_SCHEMA.extend(
{vol.Required(CONF_DOMAIN): DOMAIN}
)
CONDITION_SCHEMA = vol.All(
vol.Schema(
{
vol.Required(CONF_CONDITION): "device",
vol.Optional(CONF_DEVICE_ID): str,
vol.Required(CONF_DOMAIN): DOMAIN,
vol.Required(CONF_ENTITY_ID): cv.entity_id,
vol.Required(CONF_TYPE): vol.In([CONF_IS_OFF, CONF_IS_ON]),
}
)
)
TRIGGER_SCHEMA = vol.All(
vol.Schema(
{
vol.Required(CONF_PLATFORM): "device",
vol.Optional(CONF_DEVICE_ID): str,
vol.Required(CONF_DOMAIN): DOMAIN,
vol.Required(CONF_ENTITY_ID): cv.entity_id,
vol.Required(CONF_TYPE): vol.In([CONF_TURN_OFF, CONF_TURN_ON]),
}
)
TRIGGER_SCHEMA = toggle_entity.TRIGGER_SCHEMA.extend(
{vol.Required(CONF_DOMAIN): DOMAIN}
)
def _is_domain(entity, domain):
return split_entity_id(entity.entity_id)[0] == domain
async def async_action_from_config(hass, config, variables, context):
async def async_call_action_from_config(hass, config, variables, context):
"""Change state based on configuration."""
config = ACTION_SCHEMA(config)
action_type = config[CONF_TYPE]
if action_type == CONF_TURN_ON:
action = "turn_on"
elif action_type == CONF_TURN_OFF:
action = "turn_off"
else:
action = "toggle"
service_action = {
service.CONF_SERVICE: "light.{}".format(action),
CONF_ENTITY_ID: config[CONF_ENTITY_ID],
}
await service.async_call_from_config(
hass,
service_action,
blocking=True,
variables=variables,
# validate_config=False,
context=context,
await toggle_entity.async_call_action_from_config(
hass, config, variables, context, DOMAIN
)
def async_condition_from_config(config, config_validation):
"""Evaluate state based on configuration."""
config = CONDITION_SCHEMA(config)
condition_type = config[CONF_TYPE]
if condition_type == CONF_IS_ON:
stat = "on"
else:
stat = "off"
state_config = {
condition.CONF_CONDITION: "state",
condition.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
condition.CONF_STATE: stat,
}
return condition.state_from_config(state_config, config_validation)
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
config = TRIGGER_SCHEMA(config)
trigger_type = config[CONF_TYPE]
if trigger_type == CONF_TURN_ON:
from_state = "off"
to_state = "on"
else:
from_state = "on"
to_state = "off"
state_config = {
state.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
state.CONF_FROM: from_state,
state.CONF_TO: to_state,
}
return await state.async_trigger(hass, state_config, action, automation_info)
return toggle_entity.async_condition_from_config(config, config_validation)
async def async_trigger(hass, config, action, automation_info):
"""Temporary so existing automation framework can be used for testing."""
return await async_attach_trigger(hass, config, action, automation_info)
async def _async_get_automations(hass, device_id, automation_templates):
"""List device automations."""
automations = []
entity_registry = await hass.helpers.entity_registry.async_get_registry()
entities = async_entries_for_device(entity_registry, device_id)
domain_entities = [x for x in entities if _is_domain(x, DOMAIN)]
for entity in domain_entities:
for automation in automation_templates:
automation = dict(automation)
automation.update(device_id=device_id, entity_id=entity.entity_id)
automations.append(automation)
return automations
"""Listen for state changes based on configuration."""
config = TRIGGER_SCHEMA(config)
return await toggle_entity.async_attach_trigger(
hass, config, action, automation_info
)
async def async_get_actions(hass, device_id):
"""List device actions."""
return await _async_get_automations(hass, device_id, ENTITY_ACTIONS)
return await toggle_entity.async_get_actions(hass, device_id, DOMAIN)
async def async_get_conditions(hass, device_id):
"""List device conditions."""
return await _async_get_automations(hass, device_id, ENTITY_CONDITIONS)
return await toggle_entity.async_get_conditions(hass, device_id, DOMAIN)
async def async_get_triggers(hass, device_id):
"""List device triggers."""
return await _async_get_automations(hass, device_id, ENTITY_TRIGGERS)
return await toggle_entity.async_get_triggers(hass, device_id, DOMAIN)

View file

@ -0,0 +1,56 @@
"""Provides device automations for lights."""
import voluptuous as vol
from homeassistant.components.device_automation import toggle_entity
from homeassistant.const import CONF_DOMAIN
from . import DOMAIN
# mypy: allow-untyped-defs, no-check-untyped-defs
ACTION_SCHEMA = toggle_entity.ACTION_SCHEMA.extend({vol.Required(CONF_DOMAIN): DOMAIN})
CONDITION_SCHEMA = toggle_entity.CONDITION_SCHEMA.extend(
{vol.Required(CONF_DOMAIN): DOMAIN}
)
TRIGGER_SCHEMA = toggle_entity.TRIGGER_SCHEMA.extend(
{vol.Required(CONF_DOMAIN): DOMAIN}
)
async def async_call_action_from_config(hass, config, variables, context):
"""Change state based on configuration."""
config = ACTION_SCHEMA(config)
await toggle_entity.async_call_action_from_config(
hass, config, variables, context, DOMAIN
)
def async_condition_from_config(config, config_validation):
"""Evaluate state based on configuration."""
config = CONDITION_SCHEMA(config)
return toggle_entity.async_condition_from_config(config, config_validation)
async def async_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
config = TRIGGER_SCHEMA(config)
return await toggle_entity.async_attach_trigger(
hass, config, action, automation_info
)
async def async_get_actions(hass, device_id):
"""List device actions."""
return await toggle_entity.async_get_actions(hass, device_id, DOMAIN)
async def async_get_conditions(hass, device_id):
"""List device conditions."""
return await toggle_entity.async_get_conditions(hass, device_id, DOMAIN)
async def async_get_triggers(hass, device_id):
"""List device triggers."""
return await toggle_entity.async_get_triggers(hass, device_id, DOMAIN)

View file

@ -0,0 +1,17 @@
{
"device_automation": {
"action_type": {
"toggle": "Toggle {entity_name}",
"turn_on": "Turn on {entity_name}",
"turn_off": "Turn off {entity_name}"
},
"condition_type": {
"turn_on": "{entity_name} turned on",
"turn_off": "{entity_name} turned off"
},
"trigger_type": {
"turn_on": "{entity_name} turned on",
"turn_off": "{entity_name} turned off"
}
}
}

View file

@ -333,7 +333,9 @@ class Script:
self._log("Executing step %s" % self.last_action)
integration = await async_get_integration(self.hass, action[CONF_DOMAIN])
platform = integration.get_platform("device_automation")
await platform.async_action_from_config(self.hass, action, variables, context)
await platform.async_call_action_from_config(
self.hass, action, variables, context
)
async def _async_fire_event(self, action, variables, context):
"""Fire an event."""

View file

@ -1,7 +1,7 @@
"""The test for light device automation."""
import pytest
from homeassistant.components import light
from homeassistant.components.light import DOMAIN
from homeassistant.const import STATE_ON, STATE_OFF, CONF_PLATFORM
from homeassistant.setup import async_setup_component
import homeassistant.components.automation as automation
@ -54,28 +54,28 @@ async def test_get_actions(hass, device_reg, entity_reg):
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create("light", "test", "5678", device_id=device_entry.id)
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
expected_actions = [
{
"device": None,
"domain": "light",
"domain": DOMAIN,
"type": "turn_off",
"device_id": device_entry.id,
"entity_id": "light.test_5678",
"entity_id": f"{DOMAIN}.test_5678",
},
{
"device": None,
"domain": "light",
"domain": DOMAIN,
"type": "turn_on",
"device_id": device_entry.id,
"entity_id": "light.test_5678",
"entity_id": f"{DOMAIN}.test_5678",
},
{
"device": None,
"domain": "light",
"domain": DOMAIN,
"type": "toggle",
"device_id": device_entry.id,
"entity_id": "light.test_5678",
"entity_id": f"{DOMAIN}.test_5678",
},
]
actions = await async_get_device_automations(
@ -92,21 +92,21 @@ async def test_get_conditions(hass, device_reg, entity_reg):
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create("light", "test", "5678", device_id=device_entry.id)
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
expected_conditions = [
{
"condition": "device",
"domain": "light",
"domain": DOMAIN,
"type": "is_off",
"device_id": device_entry.id,
"entity_id": "light.test_5678",
"entity_id": f"{DOMAIN}.test_5678",
},
{
"condition": "device",
"domain": "light",
"domain": DOMAIN,
"type": "is_on",
"device_id": device_entry.id,
"entity_id": "light.test_5678",
"entity_id": f"{DOMAIN}.test_5678",
},
]
conditions = await async_get_device_automations(
@ -123,21 +123,21 @@ async def test_get_triggers(hass, device_reg, entity_reg):
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create("light", "test", "5678", device_id=device_entry.id)
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
expected_triggers = [
{
"platform": "device",
"domain": "light",
"domain": DOMAIN,
"type": "turn_off",
"device_id": device_entry.id,
"entity_id": "light.test_5678",
"entity_id": f"{DOMAIN}.test_5678",
},
{
"platform": "device",
"domain": "light",
"domain": DOMAIN,
"type": "turn_on",
"device_id": device_entry.id,
"entity_id": "light.test_5678",
"entity_id": f"{DOMAIN}.test_5678",
},
]
triggers = await async_get_device_automations(
@ -148,12 +148,10 @@ async def test_get_triggers(hass, device_reg, entity_reg):
async def test_if_fires_on_state_change(hass, calls):
"""Test for turn_on and turn_off triggers firing."""
platform = getattr(hass.components, "test.light")
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
assert await async_setup_component(
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
dev1, dev2, dev3 = platform.DEVICES
@ -165,7 +163,7 @@ async def test_if_fires_on_state_change(hass, calls):
{
"trigger": {
"platform": "device",
"domain": light.DOMAIN,
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "turn_on",
},
@ -188,7 +186,7 @@ async def test_if_fires_on_state_change(hass, calls):
{
"trigger": {
"platform": "device",
"domain": light.DOMAIN,
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "turn_off",
},
@ -232,12 +230,10 @@ async def test_if_fires_on_state_change(hass, calls):
async def test_if_state(hass, calls):
"""Test for turn_on and turn_off conditions."""
platform = getattr(hass.components, "test.light")
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
assert await async_setup_component(
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
dev1, dev2, dev3 = platform.DEVICES
@ -251,7 +247,7 @@ async def test_if_state(hass, calls):
"condition": [
{
"condition": "device",
"domain": "light",
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "is_on",
}
@ -269,7 +265,7 @@ async def test_if_state(hass, calls):
"condition": [
{
"condition": "device",
"domain": "light",
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "is_off",
}
@ -305,12 +301,10 @@ async def test_if_state(hass, calls):
async def test_action(hass, calls):
"""Test for turn_on and turn_off actions."""
platform = getattr(hass.components, "test.light")
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
assert await async_setup_component(
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
dev1, dev2, dev3 = platform.DEVICES
@ -323,7 +317,7 @@ async def test_action(hass, calls):
"trigger": {"platform": "event", "event_type": "test_event1"},
"action": {
"device": None,
"domain": "light",
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "turn_off",
},
@ -332,7 +326,7 @@ async def test_action(hass, calls):
"trigger": {"platform": "event", "event_type": "test_event2"},
"action": {
"device": None,
"domain": "light",
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "turn_on",
},
@ -341,7 +335,7 @@ async def test_action(hass, calls):
"trigger": {"platform": "event", "event_type": "test_event3"},
"action": {
"device": None,
"domain": "light",
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "toggle",
},

View file

@ -0,0 +1,372 @@
"""The test for switch device automation."""
import pytest
from homeassistant.components.switch import DOMAIN
from homeassistant.const import STATE_ON, STATE_OFF, CONF_PLATFORM
from homeassistant.setup import async_setup_component
import homeassistant.components.automation as automation
from homeassistant.components.device_automation import (
_async_get_device_automations as async_get_device_automations,
)
from homeassistant.helpers import device_registry
from tests.common import (
MockConfigEntry,
async_mock_service,
mock_device_registry,
mock_registry,
)
@pytest.fixture
def device_reg(hass):
"""Return an empty, loaded, registry."""
return mock_device_registry(hass)
@pytest.fixture
def entity_reg(hass):
"""Return an empty, loaded, registry."""
return mock_registry(hass)
@pytest.fixture
def calls(hass):
"""Track calls to a mock serivce."""
return async_mock_service(hass, "test", "automation")
def _same_lists(a, b):
if len(a) != len(b):
return False
for d in a:
if d not in b:
return False
return True
async def test_get_actions(hass, device_reg, entity_reg):
"""Test we get the expected actions from a switch."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
expected_actions = [
{
"device": None,
"domain": DOMAIN,
"type": "turn_off",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_5678",
},
{
"device": None,
"domain": DOMAIN,
"type": "turn_on",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_5678",
},
{
"device": None,
"domain": DOMAIN,
"type": "toggle",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_5678",
},
]
actions = await async_get_device_automations(
hass, "async_get_actions", device_entry.id
)
assert _same_lists(actions, expected_actions)
async def test_get_conditions(hass, device_reg, entity_reg):
"""Test we get the expected conditions from a switch."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
expected_conditions = [
{
"condition": "device",
"domain": DOMAIN,
"type": "is_off",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_5678",
},
{
"condition": "device",
"domain": DOMAIN,
"type": "is_on",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_5678",
},
]
conditions = await async_get_device_automations(
hass, "async_get_conditions", device_entry.id
)
assert _same_lists(conditions, expected_conditions)
async def test_get_triggers(hass, device_reg, entity_reg):
"""Test we get the expected triggers from a switch."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
expected_triggers = [
{
"platform": "device",
"domain": DOMAIN,
"type": "turn_off",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_5678",
},
{
"platform": "device",
"domain": DOMAIN,
"type": "turn_on",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_5678",
},
]
triggers = await async_get_device_automations(
hass, "async_get_triggers", device_entry.id
)
assert _same_lists(triggers, expected_triggers)
async def test_if_fires_on_state_change(hass, calls):
"""Test for turn_on and turn_off triggers firing."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
dev1, dev2, dev3 = platform.DEVICES
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "device",
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "turn_on",
},
"action": {
"service": "test.automation",
"data_template": {
"some": "turn_on {{ trigger.%s }}"
% "}} - {{ trigger.".join(
(
"platform",
"entity_id",
"from_state.state",
"to_state.state",
"for",
)
)
},
},
},
{
"trigger": {
"platform": "device",
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "turn_off",
},
"action": {
"service": "test.automation",
"data_template": {
"some": "turn_off {{ trigger.%s }}"
% "}} - {{ trigger.".join(
(
"platform",
"entity_id",
"from_state.state",
"to_state.state",
"for",
)
)
},
},
},
]
},
)
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert len(calls) == 0
hass.states.async_set(dev1.entity_id, STATE_OFF)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "turn_off state - {} - on - off - None".format(
dev1.entity_id
)
hass.states.async_set(dev1.entity_id, STATE_ON)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "turn_on state - {} - off - on - None".format(
dev1.entity_id
)
async def test_if_state(hass, calls):
"""Test for turn_on and turn_off conditions."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
dev1, dev2, dev3 = platform.DEVICES
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {"platform": "event", "event_type": "test_event1"},
"condition": [
{
"condition": "device",
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "is_on",
}
],
"action": {
"service": "test.automation",
"data_template": {
"some": "is_on {{ trigger.%s }}"
% "}} - {{ trigger.".join(("platform", "event.event_type"))
},
},
},
{
"trigger": {"platform": "event", "event_type": "test_event2"},
"condition": [
{
"condition": "device",
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "is_off",
}
],
"action": {
"service": "test.automation",
"data_template": {
"some": "is_off {{ trigger.%s }}"
% "}} - {{ trigger.".join(("platform", "event.event_type"))
},
},
},
]
},
)
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert len(calls) == 0
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2")
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "is_on event - test_event1"
hass.states.async_set(dev1.entity_id, STATE_OFF)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2")
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "is_off event - test_event2"
async def test_action(hass, calls):
"""Test for turn_on and turn_off actions."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
dev1, dev2, dev3 = platform.DEVICES
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {"platform": "event", "event_type": "test_event1"},
"action": {
"device": None,
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "turn_off",
},
},
{
"trigger": {"platform": "event", "event_type": "test_event2"},
"action": {
"device": None,
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "turn_on",
},
},
{
"trigger": {"platform": "event", "event_type": "test_event3"},
"action": {
"device": None,
"domain": DOMAIN,
"entity_id": dev1.entity_id,
"type": "toggle",
},
},
]
},
)
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert len(calls) == 0
hass.bus.async_fire("test_event1")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_OFF
hass.bus.async_fire("test_event1")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_OFF
hass.bus.async_fire("test_event2")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
hass.bus.async_fire("test_event2")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
hass.bus.async_fire("test_event3")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_OFF
hass.bus.async_fire("test_event3")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON