From c81d5a1ac26002c708ea341ee3cf5e1b6b91f0ee Mon Sep 17 00:00:00 2001
From: "J. Nick Koston" <nick@koston.org>
Date: Wed, 10 Jul 2024 07:53:38 -0700
Subject: [PATCH] Handle empty entity_id in the recorder filter (#121681)

fixes #111745
---
 homeassistant/components/recorder/core.py |  5 ++---
 tests/components/recorder/test_init.py    | 17 +++++++++++++++++
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/homeassistant/components/recorder/core.py b/homeassistant/components/recorder/core.py
index 01fda0f02fa..28291de3be8 100644
--- a/homeassistant/components/recorder/core.py
+++ b/homeassistant/components/recorder/core.py
@@ -319,9 +319,8 @@ class Recorder(threading.Thread):
             if event.event_type in exclude_event_types:
                 return
 
-            if (
-                entity_filter is None
-                or (entity_id := event.data.get(ATTR_ENTITY_ID)) is None
+            if entity_filter is None or not (
+                entity_id := event.data.get(ATTR_ENTITY_ID)
             ):
                 queue_put(event)
                 return
diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py
index adfc451c742..8e28e15fdf7 100644
--- a/tests/components/recorder/test_init.py
+++ b/tests/components/recorder/test_init.py
@@ -2700,3 +2700,20 @@ async def test_all_tables_use_default_table_args(hass: HomeAssistant) -> None:
     """Test that all tables use the default table args."""
     for table in db_schema.Base.metadata.tables.values():
         assert table.kwargs.items() >= db_schema._DEFAULT_TABLE_ARGS.items()
+
+
+async def test_empty_entity_id(
+    hass: HomeAssistant,
+    async_setup_recorder_instance: RecorderInstanceGenerator,
+    caplog: pytest.LogCaptureFixture,
+) -> None:
+    """Test the recorder can handle an empty entity_id."""
+    await async_setup_recorder_instance(
+        hass,
+        {
+            "exclude": {"domains": "hidden_domain"},
+        },
+    )
+    hass.bus.async_fire("hello", {"entity_id": ""})
+    await async_wait_recording_done(hass)
+    assert "Invalid entity ID" not in caplog.text
-- 
GitLab