From b14b14c3c95d8bf474ffd5ce0f37d1cce748429d Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 9 Sep 2019 16:55:47 +0200 Subject: [PATCH] Add device automation support to switch entities (#26466) * Add device automation support to switch entities * Update switch translations * Address review comments --- .../device_automation/toggle_entity.py | 189 +++++++++ .../components/light/device_automation.py | 197 +--------- .../components/switch/device_automation.py | 56 +++ homeassistant/components/switch/strings.json | 17 + homeassistant/helpers/script.py | 4 +- .../light/test_device_automation.py | 68 ++-- .../switch/test_device_automation.py | 372 ++++++++++++++++++ 7 files changed, 687 insertions(+), 216 deletions(-) create mode 100644 homeassistant/components/device_automation/toggle_entity.py create mode 100644 homeassistant/components/switch/device_automation.py create mode 100644 homeassistant/components/switch/strings.json create mode 100644 tests/components/switch/test_device_automation.py diff --git a/homeassistant/components/device_automation/toggle_entity.py b/homeassistant/components/device_automation/toggle_entity.py new file mode 100644 index 00000000000..2a2a7aaca18 --- /dev/null +++ b/homeassistant/components/device_automation/toggle_entity.py @@ -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) diff --git a/homeassistant/components/light/device_automation.py b/homeassistant/components/light/device_automation.py index 4ddba8b9423..61292d47449 100644 --- a/homeassistant/components/light/device_automation.py +++ b/homeassistant/components/light/device_automation.py @@ -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) diff --git a/homeassistant/components/switch/device_automation.py b/homeassistant/components/switch/device_automation.py new file mode 100644 index 00000000000..61292d47449 --- /dev/null +++ b/homeassistant/components/switch/device_automation.py @@ -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) diff --git a/homeassistant/components/switch/strings.json b/homeassistant/components/switch/strings.json new file mode 100644 index 00000000000..857b3763076 --- /dev/null +++ b/homeassistant/components/switch/strings.json @@ -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" + } + } +} diff --git a/homeassistant/helpers/script.py b/homeassistant/helpers/script.py index 1d8f915543f..81a6cd059bb 100644 --- a/homeassistant/helpers/script.py +++ b/homeassistant/helpers/script.py @@ -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.""" diff --git a/tests/components/light/test_device_automation.py b/tests/components/light/test_device_automation.py index 38c573b514f..6bbd8b5c319 100644 --- a/tests/components/light/test_device_automation.py +++ b/tests/components/light/test_device_automation.py @@ -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", }, diff --git a/tests/components/switch/test_device_automation.py b/tests/components/switch/test_device_automation.py new file mode 100644 index 00000000000..56da6767c1d --- /dev/null +++ b/tests/components/switch/test_device_automation.py @@ -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