|
1 | 1 | # knx-telegram-store |
2 | 2 |
|
3 | | -A standalone, host-agnostic Python library for KNX telegram persistence with pluggable storage backends. |
4 | | - |
5 | | -## Motivation |
6 | | - |
7 | | -KNX telegram storage is needed in multiple projects — [Home Assistant KNX](https://www.home-assistant.io/integrations/knx/) and [SpectrumKNX](https://github.com/spectrumknx/spectrumknx) — but currently each implements its own storage layer with zero shared code. This library extracts a common interface and ships multiple backends so both projects (and others) can share the same well-tested storage implementation. |
| 3 | +A standalone, host-agnostic Python library for KNX telegram persistence. |
8 | 4 |
|
9 | 5 | ## Features |
10 | 6 |
|
11 | | -- **Canonical data model** — A single `StoredTelegram` dataclass covering all KNX telegram fields. |
12 | | -- **Abstract storage interface** — `TelegramStore` ABC with pluggable backends. |
13 | | -- **Declarative query model** — `TelegramQuery` supports multi-value filters, time ranges, time-delta context windows, and pagination. |
14 | | -- **Graceful degradation** — Simple backends return all data; consumers apply client-side filtering. SQL backends filter server-side. |
15 | | -- **Zero core dependencies** — The core library (model + interface + in-memory backend) has no runtime dependencies. |
| 7 | +- **Canonical Data Model**: A unified model for KNX telegrams shared between Home Assistant and SpectrumKNX. |
| 8 | +- **Pluggable Backends**: |
| 9 | + - **In-Memory**: Fast, deque-based storage with full filtering support. |
| 10 | + - **SQLite**: Lightweight persistent storage with SQL-based filtering. |
| 11 | + - **PostgreSQL + TimescaleDB**: Full-scale time-series storage. |
| 12 | +- **Unified Query Model**: Powerful declarative filtering including time-delta context windows and pagination. |
| 13 | +- **Zero Runtime Dependencies**: Core library (model, interface, in-memory) has no dependencies. |
| 14 | +- **Automated Schema Management**: SQL backends handle their own creation and upgrades. |
16 | 15 |
|
17 | | -## Backends |
| 16 | +## Installation |
18 | 17 |
|
19 | | -| Backend | Use Case | Dependencies | |
20 | | -|---|---|---| |
21 | | -| **In-Memory** | Testing, development | None | |
22 | | -| **HA Storage** | Home Assistant native persistence | None (uses HA's `Store`) | |
23 | | -| **SQLite** | Lightweight persistent storage | `aiosqlite` | |
24 | | -| **PostgreSQL + TimescaleDB** | Full-scale time-series storage | `asyncpg`, `sqlalchemy` | |
| 18 | +```bash |
| 19 | +pip install knx-telegram-store |
| 20 | +``` |
25 | 21 |
|
26 | | -## Installation |
| 22 | +For SQL support: |
27 | 23 |
|
28 | 24 | ```bash |
29 | | -pip install knx-telegram-store # Core only |
30 | | -pip install knx-telegram-store[sqlite] # + SQLite backend |
31 | | -pip install knx-telegram-store[postgres] # + PostgreSQL backend |
| 25 | +pip install knx-telegram-store[sqlite] |
| 26 | +pip install knx-telegram-store[postgres] |
32 | 27 | ``` |
33 | 28 |
|
34 | | -## Documentation |
| 29 | +## Usage |
35 | 30 |
|
36 | | -See [docs/design_and_implementation.md](docs/design_and_implementation.md) for the full design document. |
| 31 | +```python |
| 32 | +from datetime import datetime |
| 33 | +from knx_telegram_store import StoredTelegram, TelegramQuery |
| 34 | +from knx_telegram_store.backends.memory import MemoryStore |
| 35 | + |
| 36 | +async def main(): |
| 37 | + store = MemoryStore(max_size=1000) |
| 38 | + await store.initialize() |
| 39 | + |
| 40 | + telegram = StoredTelegram( |
| 41 | + timestamp=datetime.now(), |
| 42 | + source="1.1.1", |
| 43 | + destination="1/1/1", |
| 44 | + telegramtype="GroupValueWrite", |
| 45 | + direction="Incoming", |
| 46 | + value=22.5, |
| 47 | + unit="°C" |
| 48 | + ) |
| 49 | + |
| 50 | + await store.store(telegram) |
| 51 | + |
| 52 | + query = TelegramQuery(destinations=["1/1/1"]) |
| 53 | + result = await store.query(query) |
| 54 | + |
| 55 | + for t in result.telegrams: |
| 56 | + print(f"{t.timestamp}: {t.source} -> {t.destination} | {t.value} {t.unit}") |
| 57 | + |
| 58 | + await store.close() |
| 59 | +``` |
37 | 60 |
|
38 | 61 | ## License |
39 | 62 |
|
40 | | -Apache License 2.0 |
| 63 | +MIT |
0 commit comments