Files
home-automation/apps/abstraction/README.md
2025-11-06 13:46:19 +01:00

99 lines
2.3 KiB
Markdown

# Abstraction Layer
The abstraction layer is an asyncio-based worker that manages device communication through MQTT.
## Features
- **Configuration Loading**: Reads device configuration from `config/devices.yaml`
- **MQTT Integration**: Connects to MQTT broker for real-time device communication
- **Async Design**: Built with asyncio for efficient concurrent operations
- **Message Subscription**: Listens to `home/#` topics for device events
- **Logging**: Structured logging at INFO level
## Running
### Local Development
```bash
# Start the abstraction worker
poetry run python -m apps.abstraction.main
```
### Docker Container
#### Build Image
```bash
docker build -t abstraction:dev -f apps/abstraction/Dockerfile .
```
#### Run Container
```bash
docker run --rm \
-v $(pwd)/config:/app/config:ro \
-e MQTT_BROKER=172.16.2.16 \
-e MQTT_PORT=1883 \
-e REDIS_HOST=172.23.1.116 \
-e REDIS_PORT=6379 \
-e REDIS_DB=8 \
abstraction:dev
```
#### Environment Variables
| Variable | Default | Description |
|----------|---------|-------------|
| `MQTT_BROKER` | `172.16.2.16` | MQTT broker hostname/IP |
| `MQTT_PORT` | `1883` | MQTT broker port |
| `REDIS_HOST` | `localhost` | Redis server hostname/IP |
| `REDIS_PORT` | `6379` | Redis server port |
| `REDIS_DB` | `0` | Redis database number |
### What the Worker Does
The worker will:
1. Load configuration from `config/devices.yaml`
2. Connect to MQTT broker (172.16.2.16:1883)
3. Subscribe to topic `home/#`
4. Log "Abstraction worker started" at INFO level
5. Keep running and processing MQTT messages until interrupted
## Configuration
The worker reads configuration from `config/devices.yaml`. Example structure:
```yaml
mqtt:
broker: "172.16.2.16"
port: 1883
client_id: "home-automation-abstraction"
keepalive: 60
devices:
- id: "light_living_room"
type: "light"
name: "Living Room Light"
mqtt_topic: "home/living_room/light"
```
## MQTT Broker
The worker connects to a real MQTT broker at:
- **Host**: 172.16.2.16
- **Port**: 1883
- **Authentication**: None required
Topics subscribed:
- `home/#` - All home automation topics
## Dependencies
- **PyYAML**: Configuration file parsing
- **aiomqtt**: Modern async MQTT client
- **asyncio**: Asynchronous I/O
## Stopping
Press `Ctrl+C` to gracefully stop the worker.