mirror of
https://github.com/home-assistant/core.git
synced 2026-04-21 11:36:00 +00:00
Allow passing a set of event types to logbook.async_subscribe_events (#168163)
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable, Mapping
|
||||
from collections.abc import Callable, Collection, Mapping
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.sensor import ATTR_STATE_CLASS, NON_NUMERIC_DEVICE_CLASSES
|
||||
@@ -75,12 +75,12 @@ def _async_config_entries_for_ids(
|
||||
|
||||
def async_determine_event_types(
|
||||
hass: HomeAssistant, entity_ids: list[str] | None, device_ids: list[str] | None
|
||||
) -> tuple[EventType[Any] | str, ...]:
|
||||
) -> set[EventType[Any] | str]:
|
||||
"""Reduce the event types based on the entity ids and device ids."""
|
||||
logbook_config: LogbookConfig = hass.data[DOMAIN]
|
||||
external_events = logbook_config.external_events
|
||||
if not entity_ids and not device_ids:
|
||||
return (*BUILT_IN_EVENTS, *external_events)
|
||||
return {*BUILT_IN_EVENTS, *external_events}
|
||||
|
||||
interested_domains: set[str] = set()
|
||||
for entry_id in _async_config_entries_for_ids(hass, entity_ids, device_ids):
|
||||
@@ -93,16 +93,16 @@ def async_determine_event_types(
|
||||
# to add them since we have historically included
|
||||
# them when matching only on entities
|
||||
#
|
||||
intrested_event_types: set[EventType[Any] | str] = {
|
||||
interested_event_types: set[EventType[Any] | str] = {
|
||||
external_event
|
||||
for external_event, domain_call in external_events.items()
|
||||
if domain_call[0] in interested_domains
|
||||
} | AUTOMATION_EVENTS
|
||||
if entity_ids:
|
||||
# We also allow entity_ids to be recorded via manual logbook entries.
|
||||
intrested_event_types.add(EVENT_LOGBOOK_ENTRY)
|
||||
interested_event_types.add(EVENT_LOGBOOK_ENTRY)
|
||||
|
||||
return tuple(intrested_event_types)
|
||||
return interested_event_types
|
||||
|
||||
|
||||
@callback
|
||||
@@ -187,7 +187,7 @@ def async_subscribe_events(
|
||||
hass: HomeAssistant,
|
||||
subscriptions: list[CALLBACK_TYPE],
|
||||
target: Callable[[Event[Any]], None],
|
||||
event_types: tuple[EventType[Any] | str, ...],
|
||||
event_types: Collection[EventType[Any] | str],
|
||||
entities_filter: Callable[[str], bool] | None,
|
||||
entity_ids: list[str] | None,
|
||||
device_ids: list[str] | None,
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable, Generator, Sequence
|
||||
from collections.abc import Callable, Collection, Generator, Sequence
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime as dt
|
||||
import logging
|
||||
@@ -126,7 +126,7 @@ class EventProcessor:
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
event_types: tuple[EventType[Any] | str, ...],
|
||||
event_types: Collection[EventType[Any] | str],
|
||||
entity_ids: list[str] | None = None,
|
||||
device_ids: list[str] | None = None,
|
||||
context_id: str | None = None,
|
||||
|
||||
@@ -20,7 +20,6 @@ from homeassistant.helpers.event import async_track_point_in_utc_time
|
||||
from homeassistant.helpers.json import json_bytes
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.util.async_ import create_eager_task
|
||||
from homeassistant.util.event_type import EventType
|
||||
|
||||
from .const import DOMAIN
|
||||
from .helpers import (
|
||||
@@ -366,16 +365,11 @@ async def ws_event_stream(
|
||||
# cache parent user_ids as they fire. Historical queries don't — the
|
||||
# context_only join fetches them by context_id regardless of type.
|
||||
# Unfiltered streams already include it via BUILT_IN_EVENTS.
|
||||
live_event_types: tuple[EventType[Any] | str, ...] = (
|
||||
event_types
|
||||
if EVENT_CALL_SERVICE in event_types
|
||||
else (*event_types, EVENT_CALL_SERVICE)
|
||||
)
|
||||
async_subscribe_events(
|
||||
hass,
|
||||
subscriptions,
|
||||
_queue_or_cancel,
|
||||
live_event_types,
|
||||
{*event_types, EVENT_CALL_SERVICE},
|
||||
entities_filter,
|
||||
entity_ids,
|
||||
device_ids,
|
||||
|
||||
Reference in New Issue
Block a user