bot comments

This commit is contained in:
J. Nick Koston
2026-04-11 12:03:04 -10:00
parent 7cd2d67cca
commit 804ac0c6a0
2 changed files with 18 additions and 5 deletions

View File

@@ -6,7 +6,7 @@ from collections.abc import Collection
from typing import Final
import sqlalchemy
from sqlalchemy import lambda_stmt, select
from sqlalchemy import lambda_stmt, select, union_all
from sqlalchemy.sql.elements import BooleanClauseList, ColumnElement
from sqlalchemy.sql.expression import literal
from sqlalchemy.sql.lambdas import StatementLambdaElement
@@ -127,12 +127,19 @@ def select_events_context_id_subquery(
def select_context_user_ids_for_context_ids(
context_ids: Collection[bytes],
) -> StatementLambdaElement:
"""Select (context_id_bin, context_user_id_bin) for the given context ids."""
"""Select (context_id_bin, context_user_id_bin) for the given context ids.
Union of events and states since a parent context can originate from
either table (e.g., a state set directly via the API).
"""
return lambda_stmt(
lambda: (
lambda: union_all(
select(Events.context_id_bin, Events.context_user_id_bin)
.where(Events.context_id_bin.in_(context_ids))
.where(Events.context_user_id_bin.is_not(None))
.where(Events.context_user_id_bin.is_not(None)),
select(States.context_id_bin, States.context_user_id_bin)
.where(States.context_id_bin.in_(context_ids))
.where(States.context_user_id_bin.is_not(None)),
)
)

View File

@@ -20,6 +20,7 @@ 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 (
@@ -364,7 +365,12 @@ async def ws_event_stream(
# Live subscription needs call_service events so the live consumer can
# cache parent user_ids as they fire. Historical queries don't — the
# context_only join fetches them by context_id regardless of type.
live_event_types = (*event_types, EVENT_CALL_SERVICE)
# 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,