8000 Migrate script to use describe_event for logbook by frenck · Pull Request #36729 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Migrate script to use describe_event for logbook #36729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions homeassistant/components/logbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP,
EVENT_LOGBOOK_ENTRY,
EVENT_SCRIPT_STARTED,
EVENT_STATE_CHANGED,
HTTP_BAD_REQUEST,
STATE_NOT_HOME,
Expand Down Expand Up @@ -81,7 +80,6 @@
EVENT_LOGBOOK_ENTRY,
EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP,
EVENT_SCRIPT_STARTED,
]

LOG_MESSAGE_SCHEMA = vol.Schema(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down
43 changes: 26 additions & 17 deletions homeassistant/components/script/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
ATTR_NAME,
CONF_ALIAS,
CONF_ICON,
EVENT_SCRIPT_STARTED,
SERVICE_RELOAD,
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
Expand Down Expand Up @@ -41,6 +40,8 @@

ENTITY_ID_FORMAT = DOMAIN + ".{}"

EVENT_SCRIPT_STARTED = "script_started"

SCRIPT_ENTRY_SCHEMA = vol.Schema(
{
vol.Optional(CONF_ALIAS): cv.string,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -191,6 +188,19 @@ async def toggle_service(service):
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


Expand Down Expand Up @@ -259,8 +269,7 @@ def name(self):
@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:
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/script/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
1 change: 0 additions & 1 deletion homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@
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"
Expand Down
70 changes: 0 additions & 70 deletions tests/components/logbook/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
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,
Expand Down Expand Up @@ -302,45 +300,6 @@ def test_exclude_events_domain(self):
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"
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 34 additions & 4 deletions tests/components/script/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

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
Expand Down Expand Up @@ -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"
0