Migrate automation to use describe_event for logbook (#36356)
parent
a4d4e26fe5
commit
e6fe34e64d
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue