Files
meshcore-ha-agent/README.md
T
Dejvino 5d60a91964 Allowlist pubkey prefixes only; ignore public and hashtag channels
- is_allowed matches pubkey_prefix only; sender names are never trusted
  (channel senders must be known contacts of the companion for the
  MeshCore integration to include their pubkey_prefix)
- channel messages on channel index 0 (public) and hashtag channels
  ('#name') are ignored unconditionally, before the allowlist check
- config flow validates allowlist entries as 6-64 hex characters
  (invalid_allowlist error) and UI strings/README updated
2026-12-01 19:45:36 +01:00

126 lines
5.0 KiB
Markdown

# MeshCore Agent for Home Assistant
A HACS custom integration that lets MeshCore nodes talk to Home Assistant over
the mesh. The first module is a **todo list with reminders**: any allowlisted
node can list, add, complete, remove todos and set per-todo reminders —
entirely from text messages.
Built on top of [meshcore-dev/meshcore-ha](https://github.com/meshcore-dev/meshcore-ha):
it listens for `meshcore_message` events and replies via the
`meshcore.send_message` / `meshcore.send_channel_message` services.
## Requirements
- Home Assistant 2025.6.0 or later
- The [MeshCore integration](https://github.com/meshcore-dev/meshcore-ha)
installed and configured (USB, BLE or TCP companion)
## Installation
1. In HACS → ⋮ → *Custom repositories*, add this repository with category
**Integration**.
2. Install **MeshCore Agent**.
3. Restart Home Assistant.
4. Settings → Devices & services → *Add Integration* → **MeshCore Agent**.
5. Fill in the allowlist of senders who may command the agent — **public key
prefixes only** (comma separated; the same format the MeshCore integration
uses). Leave "Allow all senders" unchecked; anyone on the mesh could
otherwise command your todo list.
## Mesh commands
Send a direct message to your companion (or a message to a channel, if that
channel's senders are allowlisted):
| Command | Example | Result |
| --- | --- | --- |
| `todo` | `todo` | List all todos (open first, then done) |
| `todo add <text>` | `todo add Buy milk` | Add a todo |
| `todo done <n>` | `todo done 2` | Check a todo off |
| `todo uncheck <n>` | `todo uncheck 2` | Reopen a todo |
| `todo remove <n>` | `todo remove 2` (alias `rm`) | Delete a todo |
| `remind <n> <when>` | `remind 1 in 30m` | Set a reminder for a todo |
| `remind off <n>` | `remind off 1` | Clear a todo's reminder |
| `help` | `help` | Command overview |
`<n>` is the number shown by `todo`, or a unique text prefix of the item
(`todo done buy m`). Messages may optionally start with a wake word:
`agent todo`, `bot done 2`.
Commands are accepted over DMs and on private named channels. The public
channel (index 0) and hashtag channels (`#name`) are ignored completely.
### Reminder times
| Syntax | Example | Meaning |
| --- | --- | --- |
| `in <N><unit>` | `in 30m`, `in 1h30m`, `in 1d` | Relative offset (`s`/`m`/`h`/`d`) |
| `HH:MM` | `14:30` | Today at that time, or tomorrow if it passed |
| `today/tomorrow HH:MM` | `tomorrow 09:00` | Explicit day |
| `<weekday> HH:MM` | `monday 08:00` | Next occurrence of that weekday |
| `YYYY-MM-DD [HH:MM]` | `2026-12-24 14:30` | Absolute date |
Reminders are sent over the mesh ("Reminder: Buy milk") to the sender who set
them, using the same time zone as Home Assistant. They also show up as the
item's due date on the Home Assistant side.
### Reply flow
- Direct messages are answered as direct messages to the sender's
`pubkey_prefix`.
- Private channel messages (from allowlisted, known contacts) are answered
back into the same channel; reminders set from a channel are delivered back
into that channel.
- Replies are truncated to the configured maximum length (default 200 chars).
## Home Assistant side
The todo list appears as a `todo` entity (`todo.<companion>_todos`), so the
same items can also be added, completed and deleted from the Home Assistant UI
— and changes made in the UI are visible to mesh users on the next `todo`.
## Security notes
- Only allowlisted senders' commands are executed; everything else is ignored.
- The allowlist matches **public key prefixes only** — names are never
trusted. A channel message carries a `pubkey_prefix` only when the sender
is a known contact of the companion, so channel users must be saved as
contacts on the companion to command the agent.
- The public channel (index 0) and all hashtag channels are ignored
unconditionally, regardless of the allowlist.
## Architecture
```
custom_components/meshcore_agent/
├── core/ # pure Python, no HA imports, fully unit-tested
│ ├── store.py # todo store: items, reminders, reference resolution
│ ├── timeparse.py # reminder time parser
│ └── commands.py # command registry + handlers
├── __init__.py # HA adapter: event listener, reply sender, reminder scheduler
├── config_flow.py # UI config: allowlist, allow-all, reply length cap
├── todo.py # todo platform entity
└── store.py # HA storage wrapper (JSON under .storage)
```
The core does all parsing/permission-sensitive work and is covered by pytest
(`tests/`); the HA layer is a thin event adapter. A crash in message handling
is caught and logged, and never takes down the event loop.
## Development
```bash
uv venv .venv
uv pip install pytest pytest-asyncio homeassistant
.venv/pytest
```
To run against a live instance, symlink or copy the integration into your HA
config directory:
```bash
ln -s "$(pwd)/custom_components/meshcore_agent" /path/to/config/custom_components/meshcore_agent
```
then restart Home Assistant or reload the integration.