Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions homeassistant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
29 changes: 29 additions & 0 deletions homeassistant/tests/test_homeassistant_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading