From e6fe34e64d6d54350ca67c89ddb6c20c77b1fff1 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 2 Jun 2020 02:18:40 +0200 Subject: [PATCH] Migrate automation to use describe_event for logbook (#36356) --- .../components/automation/__init__.py | 15 +++- .../components/automation/manifest.json | 2 +- homeassistant/components/logbook/__init__.py | 17 ----- homeassistant/const.py | 1 - tests/components/automation/test_init.py | 41 ++++++++++- tests/components/logbook/test_init.py | 69 ------------------- 6 files changed, 53 insertions(+), 92 deletions(-) diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 42f8c9777f5..9fabe0e2206 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -14,7 +14,6 @@ from homeassistant.const import ( CONF_ID, CONF_PLATFORM, CONF_ZONE, - EVENT_AUTOMATION_TRIGGERED, EVENT_HOMEASSISTANT_STARTED, SERVICE_RELOAD, SERVICE_TOGGLE, @@ -62,6 +61,7 @@ DEFAULT_CONDITION_TYPE = CONDITION_TYPE_AND DEFAULT_INITIAL_STATE = True EVENT_AUTOMATION_RELOADED = "automation_reloaded" +EVENT_AUTOMATION_TRIGGERED = "automation_triggered" ATTR_LAST_TRIGGERED = "last_triggered" ATTR_VARIABLES = "variables" @@ -222,6 +222,19 @@ async def async_setup(hass, config): hass, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=vol.Schema({}) ) + @callback + def async_describe_logbook_event(event): + """Describe a logbook event.""" + return { + "name": event.data.get(ATTR_NAME), + "message": "has been triggered", + "entity_id": event.data.get(ATTR_ENTITY_ID), + } + + hass.components.logbook.async_describe_event( + DOMAIN, EVENT_AUTOMATION_TRIGGERED, async_describe_logbook_event + ) + return True diff --git a/homeassistant/components/automation/manifest.json b/homeassistant/components/automation/manifest.json index 1b5fad1b588..a93baa0528a 100644 --- a/homeassistant/components/automation/manifest.json +++ b/homeassistant/components/automation/manifest.json @@ -2,7 +2,7 @@ "domain": "automation", "name": "Automation", "documentation": "https://www.home-assistant.io/integrations/automation", - "after_dependencies": ["device_automation", "webhook"], + "after_dependencies": ["device_automation", "logbook", "webhook"], "codeowners": ["@home-assistant/core"], "quality_scale": "internal" } diff --git a/homeassistant/components/logbook/__init__.py b/homeassistant/components/logbook/__init__.py index cd6a06720ae..82506c35b3b 100644 --- a/homeassistant/components/logbook/__init__.py +++ b/homeassistant/components/logbook/__init__.py @@ -22,7 +22,6 @@ from homeassistant.const import ( ATTR_NAME, CONF_EXCLUDE, CONF_INCLUDE, - EVENT_AUTOMATION_TRIGGERED, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, EVENT_LOGBOOK_ENTRY, @@ -82,7 +81,6 @@ ALL_EVENT_TYPES = [ EVENT_LOGBOOK_ENTRY, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, - EVENT_AUTOMATION_TRIGGERED, EVENT_SCRIPT_STARTED, ] @@ -316,17 +314,6 @@ def humanify(hass, events): "context_user_id": event.context.user_id, } - elif event.event_type == EVENT_AUTOMATION_TRIGGERED: - yield { - "when": event.time_fired, - "name": event.data.get(ATTR_NAME), - "message": "has been triggered", - "domain": "automation", - "entity_id": event.data.get(ATTR_ENTITY_ID), - "context_id": event.context.id, - "context_user_id": event.context.user_id, - } - elif event.event_type == EVENT_SCRIPT_STARTED: yield { "when": event.time_fired, @@ -461,10 +448,6 @@ def _keep_event(hass, event, entities_filter): domain = event.data.get(ATTR_DOMAIN) entity_id = event.data.get(ATTR_ENTITY_ID) - elif event.event_type == EVENT_AUTOMATION_TRIGGERED: - domain = "automation" - entity_id = event.data.get(ATTR_ENTITY_ID) - elif event.event_type == EVENT_SCRIPT_STARTED: domain = "script" entity_id = event.data.get(ATTR_ENTITY_ID) diff --git a/homeassistant/const.py b/homeassistant/const.py index 2ff1d65223b..2837c686c5d 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -180,7 +180,6 @@ CONF_XY = "xy" CONF_ZONE = "zone" # #### EVENTS #### -EVENT_AUTOMATION_TRIGGERED = "automation_triggered" EVENT_CALL_SERVICE = "call_service" EVENT_COMPONENT_LOADED = "component_loaded" EVENT_CORE_CONFIG_UPDATE = "core_config_updated" diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index a8baa9cfdb7..d17e55691bc 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -3,17 +3,21 @@ from datetime import timedelta import pytest +from homeassistant.components import logbook import homeassistant.components.automation as automation -from homeassistant.components.automation import DOMAIN, EVENT_AUTOMATION_RELOADED +from homeassistant.components.automation import ( + DOMAIN, + EVENT_AUTOMATION_RELOADED, + EVENT_AUTOMATION_TRIGGERED, +) from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_NAME, - EVENT_AUTOMATION_TRIGGERED, EVENT_HOMEASSISTANT_STARTED, STATE_OFF, STATE_ON, ) -from homeassistant.core import Context, CoreState, State +from homeassistant.core import Context, CoreState, Event, State from homeassistant.exceptions import HomeAssistantError, Unauthorized from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util @@ -1030,3 +1034,34 @@ async def test_extraction_functions(hass): "device-in-both", "device-in-last", } + + +async def test_logbook_humanify_automation_triggered_event(hass): + """Test humanifying Automation Trigger event.""" + await async_setup_component(hass, automation.DOMAIN, {}) + + event1, event2 = list( + logbook.humanify( + hass, + [ + Event( + EVENT_AUTOMATION_TRIGGERED, + {ATTR_ENTITY_ID: "automation.hello", ATTR_NAME: "Hello Automation"}, + ), + Event( + EVENT_AUTOMATION_TRIGGERED, + {ATTR_ENTITY_ID: "automation.bye", ATTR_NAME: "Bye Automation"}, + ), + ], + ) + ) + + assert event1["name"] == "Hello Automation" + assert event1["domain"] == "automation" + assert event1["message"] == "has been triggered" + assert event1["entity_id"] == "automation.hello" + + assert event2["name"] == "Bye Automation" + assert event2["domain"] == "automation" + assert event2["message"] == "has been triggered" + assert event2["entity_id"] == "automation.bye" diff --git a/tests/components/logbook/test_init.py b/tests/components/logbook/test_init.py index abe6f6ec515..27d39446fa3 100644 --- a/tests/components/logbook/test_init.py +++ b/tests/components/logbook/test_init.py @@ -14,7 +14,6 @@ from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_HIDDEN, ATTR_NAME, - EVENT_AUTOMATION_TRIGGERED, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, EVENT_SCRIPT_STARTED, @@ -305,45 +304,6 @@ class TestComponentLogbook(unittest.TestCase): entries[1], pointB, "blu", domain="sensor", entity_id=entity_id2 ) - def test_exclude_automation_events(self): - """Test if automation entries can be excluded by entity_id.""" - name = "My Automation Rule" - domain = "automation" - entity_id = "automation.my_automation_rule" - entity_id2 = "automation.my_automation_rule_2" - entity_id2 = "sensor.blu" - - eventA = ha.Event( - logbook.EVENT_AUTOMATION_TRIGGERED, - {logbook.ATTR_NAME: name, logbook.ATTR_ENTITY_ID: entity_id}, - ) - eventB = ha.Event( - logbook.EVENT_AUTOMATION_TRIGGERED, - {logbook.ATTR_NAME: name, logbook.ATTR_ENTITY_ID: entity_id2}, - ) - - config = logbook.CONFIG_SCHEMA( - { - ha.DOMAIN: {}, - logbook.DOMAIN: { - logbook.CONF_EXCLUDE: {logbook.CONF_ENTITIES: [entity_id]} - }, - } - ) - entities_filter = logbook._generate_filter_from_config(config[logbook.DOMAIN]) - events = [ - e - for e in (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB) - if logbook._keep_event(self.hass, e, entities_filter) - ] - entries = list(logbook.humanify(self.hass, events)) - - assert len(entries) == 2 - self.assert_entry( - entries[0], name="Home Assistant", message="stopped", domain=ha.DOMAIN - ) - self.assert_entry(entries[1], name=name, domain=domain, entity_id=entity_id2) - def test_exclude_script_events(self): """Test if script start can be excluded by entity_id.""" name = "My Script Rule" @@ -1335,35 +1295,6 @@ async def test_logbook_view_period_entity(hass, hass_client): assert json[0]["entity_id"] == entity_id_test -async def test_humanify_automation_triggered_event(hass): - """Test humanifying Automation Trigger event.""" - event1, event2 = list( - logbook.humanify( - hass, - [ - ha.Event( - EVENT_AUTOMATION_TRIGGERED, - {ATTR_ENTITY_ID: "automation.hello", ATTR_NAME: "Hello Automation"}, - ), - ha.Event( - EVENT_AUTOMATION_TRIGGERED, - {ATTR_ENTITY_ID: "automation.bye", ATTR_NAME: "Bye Automation"}, - ), - ], - ) - ) - - assert event1["name"] == "Hello Automation" - assert event1["domain"] == "automation" - assert event1["message"] == "has been triggered" - assert event1["entity_id"] == "automation.hello" - - assert event2["name"] == "Bye Automation" - assert event2["domain"] == "automation" - assert event2["message"] == "has been triggered" - assert event2["entity_id"] == "automation.bye" - - async def test_humanify_script_started_event(hass): """Test humanifying Script Run event.""" event1, event2 = list(