Migrate script to use describe_event for logbook (#36729)

This commit is contained in:
Franck Nijhof authored and GitHub committed 2020-06-12 16:44:29 -07:00
1 parent 15113ae854
commit 238430136e
6 files changed
+61 -108

No files matched your search

+26 -17
View File
@@ -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: