Home Assistant Unofficial Reference 2024.12.1
logbook.py
Go to the documentation of this file.
1 """Describe logbook events."""
2 
3 from collections.abc import Callable
4 from typing import Any
5 
7  LOGBOOK_ENTRY_ENTITY_ID,
8  LOGBOOK_ENTRY_MESSAGE,
9  LOGBOOK_ENTRY_NAME,
10 )
11 from homeassistant.core import Event, HomeAssistant, callback
12 
13 from .const import DOMAIN, EVENT_ALEXA_SMART_HOME
14 
15 
16 @callback
18  hass: HomeAssistant,
19  async_describe_event: Callable[[str, str, Callable[[Event], dict[str, str]]], None],
20 ) -> None:
21  """Describe logbook events."""
22 
23  @callback
24  def async_describe_logbook_event(event: Event) -> dict[str, Any]:
25  """Describe a logbook event."""
26  data = event.data
27 
28  if entity_id := data["request"].get("entity_id"):
29  state = hass.states.get(entity_id)
30  name = state.name if state else entity_id
31  message = (
32  "sent command"
33  f" {data['request']['namespace']}/{data['request']['name']} for {name}"
34  )
35  else:
36  message = (
37  f"sent command {data['request']['namespace']}/{data['request']['name']}"
38  )
39 
40  return {
41  LOGBOOK_ENTRY_NAME: "Amazon Alexa",
42  LOGBOOK_ENTRY_MESSAGE: message,
43  LOGBOOK_ENTRY_ENTITY_ID: entity_id,
44  }
45 
46  async_describe_event(DOMAIN, EVENT_ALEXA_SMART_HOME, async_describe_logbook_event)
None async_describe_events(HomeAssistant hass, Callable[[str, str, Callable[[Event], dict[str, str]]], None] async_describe_event)
Definition: logbook.py:20
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88