From 7d2da2228141f326d6e51d56ee001a1825c81d91 Mon Sep 17 00:00:00 2001 From: Chuck Bryan Date: Mon, 5 Oct 2026 09:16:23 -0700 Subject: [PATCH] fix: reuse Home Assistant persistent notification --- homeassistant/README.md | 4 +++ homeassistant/adapter.py | 5 ++++ .../tests/test_homeassistant_adapter.py | 29 +++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/homeassistant/README.md b/homeassistant/README.md index 46ac7fdc..37263ca5 100644 --- a/homeassistant/README.md +++ b/homeassistant/README.md @@ -94,6 +94,9 @@ platforms: - sensor.cpu_usage - sensor.memory_usage cooldown_seconds: 30 + # Reuse one persistent notification for routine Hermes replies. + # Set an empty string to let Home Assistant create a new one per reply. + notification_id: hermes_agent ``` | Setting | Default | Description | @@ -104,6 +107,7 @@ platforms: | `watch_all` | `false` | Receive **all** state changes (not recommended for most setups) | | `ignore_entities` | *(none)* | Always ignore these entities (applied before domain/entity filters) | | `cooldown_seconds` | `30` | Minimum seconds between events for the same entity | +| `notification_id` | `hermes_agent` | Persistent-notification ID for agent replies; set to an empty string to create a new notification per reply | Events come from the instance the adapter authenticated to with `HASS_TOKEN`, so they are not subject to a user allowlist. The adapter reconnects automatically with 5s → 10s → 30s → 60s backoff. diff --git a/homeassistant/adapter.py b/homeassistant/adapter.py index c2e57ae0..72d23099 100644 --- a/homeassistant/adapter.py +++ b/homeassistant/adapter.py @@ -112,6 +112,9 @@ def __init__(self, config: PlatformConfig): self._ignore_entities: Set[str] = set(extra.get("ignore_entities", [])) self._watch_all: bool = bool(extra.get("watch_all", False)) self._cooldown_seconds: int = int(extra.get("cooldown_seconds", 30)) + # A stable ID updates one HA notification instead of accumulating routine replies. + # An explicit empty value preserves HA's default create-a-new behavior. + self._notification_id: str = str(extra.get("notification_id", "hermes_agent")) self._last_event_time: Dict[str, float] = {} # entity_id -> last event ts def _next_id(self) -> int: @@ -300,6 +303,8 @@ async def send( """ url = f"{self._hass_url}/api/services/persistent_notification/create" payload = {"title": NOTIFICATION_TITLE, "message": content[:self.MAX_MESSAGE_LENGTH]} + if self._notification_id: + payload["notification_id"] = self._notification_id async def _post(session) -> SendResult: async with session.post( diff --git a/homeassistant/tests/test_homeassistant_adapter.py b/homeassistant/tests/test_homeassistant_adapter.py index a8cfe8da..139a0a49 100644 --- a/homeassistant/tests/test_homeassistant_adapter.py +++ b/homeassistant/tests/test_homeassistant_adapter.py @@ -303,8 +303,37 @@ async def test_send_success(self): assert "/api/services/persistent_notification/create" in call_args[0][0] assert call_args[1]["json"]["title"] == NOTIFICATION_TITLE == "Hermes Agent" assert call_args[1]["json"]["message"] == "Test notification" + assert call_args[1]["json"]["notification_id"] == "hermes_agent" assert "Bearer tok" in call_args[1]["headers"]["Authorization"] + @pytest.mark.asyncio + async def test_send_uses_configured_notification_id(self): + adapter = _make_adapter(notification_id="hermes_agent_homeassistant_events") + mock_session = self._mock_aiohttp_session(200) + + with patch("homeassistant_plugin.adapter.aiohttp") as mock_aiohttp: + mock_aiohttp.ClientSession = MagicMock(return_value=mock_session) + mock_aiohttp.ClientTimeout = lambda total: total + + result = await adapter.send("ha_events", "Test notification") + + assert result.success is True + assert mock_session.post.call_args[1]["json"]["notification_id"] == "hermes_agent_homeassistant_events" + + @pytest.mark.asyncio + async def test_send_omits_empty_notification_id(self): + adapter = _make_adapter(notification_id="") + mock_session = self._mock_aiohttp_session(200) + + with patch("homeassistant_plugin.adapter.aiohttp") as mock_aiohttp: + mock_aiohttp.ClientSession = MagicMock(return_value=mock_session) + mock_aiohttp.ClientTimeout = lambda total: total + + result = await adapter.send("ha_events", "Test notification") + + assert result.success is True + assert "notification_id" not in mock_session.post.call_args[1]["json"] + # --------------------------------------------------------------------------- # Toolset integration