mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Migrate script to use describe_event for logbook (#36729)
This commit is contained in:
parent
15113ae854
commit
238430136e
@ -25,7 +25,6 @@ from homeassistant.const import (
|
||||
EVENT_HOMEASSISTANT_START,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
EVENT_LOGBOOK_ENTRY,
|
||||
EVENT_SCRIPT_STARTED,
|
||||
EVENT_STATE_CHANGED,
|
||||
HTTP_BAD_REQUEST,
|
||||
STATE_NOT_HOME,
|
||||
@ -81,7 +80,6 @@ ALL_EVENT_TYPES = [
|
||||
EVENT_LOGBOOK_ENTRY,
|
||||
EVENT_HOMEASSISTANT_START,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
EVENT_SCRIPT_STARTED,
|
||||
]
|
||||
|
||||
LOG_MESSAGE_SCHEMA = vol.Schema(
|
||||
@ -323,17 +321,6 @@ def humanify(hass, events):
|
||||
"context_user_id": event.context.user_id,
|
||||
}
|
||||
|
||||
elif event.event_type == EVENT_SCRIPT_STARTED:
|
||||
yield {
|
||||
"when": event.time_fired,
|
||||
"name": event.data.get(ATTR_NAME),
|
||||
"message": "started",
|
||||
"domain": "script",
|
||||
"entity_id": event.data.get(ATTR_ENTITY_ID),
|
||||
"context_id": event.context.id,
|
||||
"context_user_id": event.context.user_id,
|
||||
}
|
||||
|
||||
|
||||
def _get_related_entity_ids(session, entity_filter):
|
||||
timer_start = time.perf_counter()
|
||||
@ -456,9 +443,6 @@ def _keep_event(hass, event, entities_filter):
|
||||
elif event.event_type == EVENT_LOGBOOK_ENTRY:
|
||||
domain = event.data.get(ATTR_DOMAIN)
|
||||
|
||||
elif event.event_type == EVENT_SCRIPT_STARTED:
|
||||
domain = "script"
|
||||
|
||||
elif not entity_id and event.event_type in hass.data.get(DOMAIN, {}):
|
||||
# If the entity_id isn't described, use the domain that describes
|
||||
# the event for filtering.
|
||||
|
@ -10,7 +10,6 @@ from homeassistant.const import (
|
||||
ATTR_NAME,
|
||||
CONF_ALIAS,
|
||||
CONF_ICON,
|
||||
EVENT_SCRIPT_STARTED,
|
||||
SERVICE_RELOAD,
|
||||
SERVICE_TOGGLE,
|
||||
SERVICE_TURN_OFF,
|
||||
@ -41,6 +40,8 @@ CONF_SEQUENCE = "sequence"
|
||||
|
||||
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
||||
|
||||
EVENT_SCRIPT_STARTED = "script_started"
|
||||
|
||||
SCRIPT_ENTRY_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_ALIAS): cv.string,
|
||||
@ -81,13 +82,11 @@ def scripts_with_entity(hass: HomeAssistant, entity_id: str) -> List[str]:
|
||||
|
||||
component = hass.data[DOMAIN]
|
||||
|
||||
results = []
|
||||
|
||||
for script_entity in component.entities:
|
||||
if entity_id in script_entity.script.referenced_entities:
|
||||
results.append(script_entity.entity_id)
|
||||
|
||||
return results
|
||||
return [
|
||||
script_entity.entity_id
|
||||
for script_entity in component.entities
|
||||
if entity_id in script_entity.script.referenced_entities
|
||||
]
|
||||
|
||||
|
||||
@callback
|
||||
@ -114,13 +113,11 @@ def scripts_with_device(hass: HomeAssistant, device_id: str) -> List[str]:
|
||||
|
||||
component = hass.data[DOMAIN]
|
||||
|
||||
results = []
|
||||
|
||||
for script_entity in component.entities:
|
||||
if device_id in script_entity.script.referenced_devices:
|
||||
results.append(script_entity.entity_id)
|
||||
|
||||
return results
|
||||
return [
|
||||
script_entity.entity_id
|
||||
for script_entity in component.entities
|
||||
if device_id in script_entity.script.referenced_devices
|
||||
]
|
||||
|
||||
|
||||
@callback
|
||||
@ -191,6 +188,19 @@ async def async_setup(hass, config):
|
||||
DOMAIN, SERVICE_TOGGLE, toggle_service, schema=SCRIPT_TURN_ONOFF_SCHEMA
|
||||
)
|
||||
|
||||
@callback
|
||||
def async_describe_logbook_event(event):
|
||||
"""Describe the logbook event."""
|
||||
return {
|
||||
"name": event.data.get(ATTR_NAME),
|
||||
"message": "started",
|
||||
"entity_id": event.data.get(ATTR_ENTITY_ID),
|
||||
}
|
||||
|
||||
hass.components.logbook.async_describe_event(
|
||||
DOMAIN, EVENT_SCRIPT_STARTED, async_describe_logbook_event
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@ -259,8 +269,7 @@ class ScriptEntity(ToggleEntity):
|
||||
@property
|
||||
def state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
attrs = {}
|
||||
attrs[ATTR_LAST_TRIGGERED] = self.script.last_triggered
|
||||
attrs = {ATTR_LAST_TRIGGERED: self.script.last_triggered}
|
||||
if self.script.can_cancel:
|
||||
attrs[ATTR_CAN_CANCEL] = self.script.can_cancel
|
||||
if self.script.last_action:
|
||||
|
@ -2,6 +2,7 @@
|
||||
"domain": "script",
|
||||
"name": "Scripts",
|
||||
"documentation": "https://www.home-assistant.io/integrations/script",
|
||||
"after_dependencies": ["logbook"],
|
||||
"codeowners": ["@home-assistant/core"],
|
||||
"quality_scale": "internal"
|
||||
}
|
||||
|
@ -191,7 +191,6 @@ EVENT_HOMEASSISTANT_STOP = "homeassistant_stop"
|
||||
EVENT_HOMEASSISTANT_FINAL_WRITE = "homeassistant_final_write"
|
||||
EVENT_LOGBOOK_ENTRY = "logbook_entry"
|
||||
EVENT_PLATFORM_DISCOVERED = "platform_discovered"
|
||||
EVENT_SCRIPT_STARTED = "script_started"
|
||||
EVENT_SERVICE_REGISTERED = "service_registered"
|
||||
EVENT_SERVICE_REMOVED = "service_removed"
|
||||
EVENT_STATE_CHANGED = "state_changed"
|
||||
|
@ -13,10 +13,8 @@ from homeassistant.components.alexa.smart_home import EVENT_ALEXA_SMART_HOME
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_HIDDEN,
|
||||
ATTR_NAME,
|
||||
EVENT_HOMEASSISTANT_START,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
EVENT_SCRIPT_STARTED,
|
||||
EVENT_STATE_CHANGED,
|
||||
STATE_NOT_HOME,
|
||||
STATE_OFF,
|
||||
@ -302,45 +300,6 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
entries[1], pointB, "blu", domain="sensor", entity_id=entity_id2
|
||||
)
|
||||
|
||||
def test_exclude_script_events(self):
|
||||
"""Test if script start can be excluded by entity_id."""
|
||||
name = "My Script Rule"
|
||||
domain = "script"
|
||||
entity_id = "script.my_script"
|
||||
entity_id2 = "script.my_script_2"
|
||||
entity_id2 = "sensor.blu"
|
||||
|
||||
eventA = ha.Event(
|
||||
logbook.EVENT_SCRIPT_STARTED,
|
||||
{logbook.ATTR_NAME: name, logbook.ATTR_ENTITY_ID: entity_id},
|
||||
)
|
||||
eventB = ha.Event(
|
||||
logbook.EVENT_SCRIPT_STARTED,
|
||||
{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_include_events_entity(self):
|
||||
"""Test if events are filtered if entity is included in config."""
|
||||
entity_id = "sensor.bla"
|
||||
@ -1293,35 +1252,6 @@ async def test_logbook_view_period_entity(hass, hass_client):
|
||||
assert json[0]["entity_id"] == entity_id_test
|
||||
|
||||
|
||||
async def test_humanify_script_started_event(hass):
|
||||
"""Test humanifying Script Run event."""
|
||||
event1, event2 = list(
|
||||
logbook.humanify(
|
||||
hass,
|
||||
[
|
||||
ha.Event(
|
||||
EVENT_SCRIPT_STARTED,
|
||||
{ATTR_ENTITY_ID: "script.hello", ATTR_NAME: "Hello Script"},
|
||||
),
|
||||
ha.Event(
|
||||
EVENT_SCRIPT_STARTED,
|
||||
{ATTR_ENTITY_ID: "script.bye", ATTR_NAME: "Bye Script"},
|
||||
),
|
||||
],
|
||||
)
|
||||
)
|
||||
|
||||
assert event1["name"] == "Hello Script"
|
||||
assert event1["domain"] == "script"
|
||||
assert event1["message"] == "started"
|
||||
assert event1["entity_id"] == "script.hello"
|
||||
|
||||
assert event2["name"] == "Bye Script"
|
||||
assert event2["domain"] == "script"
|
||||
assert event2["message"] == "started"
|
||||
assert event2["entity_id"] == "script.bye"
|
||||
|
||||
|
||||
async def test_logbook_describe_event(hass, hass_client):
|
||||
"""Test teaching logbook about a new event."""
|
||||
await hass.async_add_executor_job(init_recorder_component, hass)
|
||||
|
@ -4,18 +4,17 @@ import unittest
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import script
|
||||
from homeassistant.components.script import DOMAIN
|
||||
from homeassistant.components import logbook, script
|
||||
from homeassistant.components.script import DOMAIN, EVENT_SCRIPT_STARTED
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_NAME,
|
||||
EVENT_SCRIPT_STARTED,
|
||||
SERVICE_RELOAD,
|
||||
SERVICE_TOGGLE,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
)
|
||||
from homeassistant.core import Context, callback, split_entity_id
|
||||
from homeassistant.core import Context, Event, callback, split_entity_id
|
||||
from homeassistant.exceptions import ServiceNotFound
|
||||
from homeassistant.helpers.service import async_get_all_descriptions
|
||||
from homeassistant.loader import bind_hass
|
||||
@ -468,3 +467,34 @@ async def test_config(hass):
|
||||
test_script = hass.states.get("script.test_script")
|
||||
assert test_script.name == "Script Name"
|
||||
assert test_script.attributes["icon"] == "mdi:party"
|
||||
|
||||
|
||||
async def test_logbook_humanify_script_started_event(hass):
|
||||
"""Test humanifying script started event."""
|
||||
await async_setup_component(hass, DOMAIN, {})
|
||||
|
||||
event1, event2 = list(
|
||||
logbook.humanify(
|
||||
hass,
|
||||
[
|
||||
Event(
|
||||
EVENT_SCRIPT_STARTED,
|
||||
{ATTR_ENTITY_ID: "script.hello", ATTR_NAME: "Hello Script"},
|
||||
),
|
||||
Event(
|
||||
EVENT_SCRIPT_STARTED,
|
||||
{ATTR_ENTITY_ID: "script.bye", ATTR_NAME: "Bye Script"},
|
||||
),
|
||||
],
|
||||
)
|
||||
)
|
||||
|
||||
assert event1["name"] == "Hello Script"
|
||||
assert event1["domain"] == "script"
|
||||
assert event1["message"] == "started"
|
||||
assert event1["entity_id"] == "script.hello"
|
||||
|
||||
assert event2["name"] == "Bye Script"
|
||||
assert event2["domain"] == "script"
|
||||
assert event2["message"] == "started"
|
||||
assert event2["entity_id"] == "script.bye"
|
||||
|
Loading…
x
Reference in New Issue
Block a user