Send an empty logbook response when all requested entity_ids are filtered away (#73046)

This commit is contained in:
J. Nick Koston 2022-06-05 18:14:47 -10:00 committed by GitHub
parent c66b000d34
commit 6b2e5858b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 184 additions and 11 deletions

View file

@ -67,6 +67,23 @@ async def _async_wait_for_recorder_sync(hass: HomeAssistant) -> None:
)
@callback
def _async_send_empty_response(
connection: ActiveConnection, msg_id: int, start_time: dt, end_time: dt | None
) -> None:
"""Send an empty response.
The current case for this is when they ask for entity_ids
that will all be filtered away because they have UOMs or
state_class.
"""
connection.send_result(msg_id)
stream_end_time = end_time or dt_util.utcnow()
empty_stream_message = _generate_stream_message([], start_time, stream_end_time)
empty_response = messages.event_message(msg_id, empty_stream_message)
connection.send_message(JSON_DUMP(empty_response))
async def _async_send_historical_events(
hass: HomeAssistant,
connection: ActiveConnection,
@ -171,6 +188,17 @@ async def _async_get_ws_stream_events(
)
def _generate_stream_message(
events: list[dict[str, Any]], start_day: dt, end_day: dt
) -> dict[str, Any]:
"""Generate a logbook stream message response."""
return {
"events": events,
"start_time": dt_util.utc_to_timestamp(start_day),
"end_time": dt_util.utc_to_timestamp(end_day),
}
def _ws_stream_get_events(
msg_id: int,
start_day: dt,
@ -184,11 +212,7 @@ def _ws_stream_get_events(
last_time = None
if events:
last_time = dt_util.utc_from_timestamp(events[-1]["when"])
message = {
"events": events,
"start_time": dt_util.utc_to_timestamp(start_day),
"end_time": dt_util.utc_to_timestamp(end_day),
}
message = _generate_stream_message(events, start_day, end_day)
if partial:
# This is a hint to consumers of the api that
# we are about to send a another block of historical
@ -275,6 +299,10 @@ async def ws_event_stream(
entity_ids = msg.get("entity_ids")
if entity_ids:
entity_ids = async_filter_entities(hass, entity_ids)
if not entity_ids:
_async_send_empty_response(connection, msg_id, start_time, end_time)
return
event_types = async_determine_event_types(hass, entity_ids, device_ids)
event_processor = EventProcessor(
hass,