The Smart House Backend is a high-performance RESTful API built with Python and FastAPI. It serves as the central nervous system for a smart home environment, enabling management of users, smart devices, sensors, and automation rules. The system processes real-time events via Redis, evaluates user-defined rules, logs consequences, and simulates device-state changes via MQTT. Its asynchronous design ensures scalability and responsiveness.
-
User Management
- Register new users (
POST /users/) - Retrieve user details (
GET /users/{user_id}) - Update user profiles (
PUT /users/{user_id}) - Delete users (
DELETE /users/{user_id})
- Register new users (
-
Device Management
- CRUD operations for smart devices (
/devicesendpoints) - Each device has a live
state("on"/"off") - Change state via generic update or
POST /devices/{id}/state
- CRUD operations for smart devices (
-
Sensor Management
- CRUD operations for sensors (
/sensorsendpoints) - Filter sensors by device (
GET /sensors/device/{device_id})
- CRUD operations for sensors (
-
Event Handling & Reactor
- Submit events to Redis queue (
app/queues/event_producer.py) - Background reactor consumes events, matches rules, and logs consequences
- Submit events to Redis queue (
-
Rule Engine
- Define automation rules (
POST /rules/) with trigger type, condition, operator, target device, and action - List rules (
GET /rules/) and delete rules (DELETE /rules/{rule_id})
- Define automation rules (
-
Consequence Tracking
- Log each action as a
Consequencein MongoDB - Automatic status update to
executedwith timestamp - List consequences (
GET /consequences/) and execute pending (PUT /consequences/{id}/execute)
- Log each action as a
-
Device State Simulation
- Reactor triggers
turn_on/turn_offactions viaset_device_state()service
- Reactor triggers
-
MQTT Simulation
- Simple Paho-MQTT scripts to publish control messages and log device responses
- Python 3.10+
- FastAPI + Uvicorn
- MongoDB (Beanie ODM + Motor)
- Redis for event queuing
- Paho-MQTT for device simulation
- Docker & Docker Compose
- Pytest + pytest-asyncio + httpx for testing
smarthouse-cps-backend/
├── app/
│ ├── api/routes/ # FastAPI routers (users, devices, sensors, rules, consequences)
│ ├── core/ # Configuration & Redis client
│ ├── devices/ # MQTT simulator scripts
│ ├── models/ # Beanie Document models
│ ├── schemas/ # Pydantic schemas
│ ├── services/ # Business logic & database operations
│ ├── queues/ # Event producer & reactor worker
│ └── main.py # FastAPI application entrypoint
├── docker-compose.yml # Service definitions (backend, mqtt, mongo)
├── Dockerfile # Backend image build
├── requirements.txt # Python dependencies
└── tests/ # Test suite
-
Users (
/users)POST /users/GET /users/{user_id}PUT /users/{user_id}DELETE /users/{user_id}
-
Devices (
/devices)POST /devices/GET /devices/(by user)GET /devices/{device_id}PUT /devices/{device_id}POST /devices/{device_id}/state(on/off)DELETE /devices/{device_id}
-
Sensors (
/sensors)POST /sensors/GET /sensors/GET /sensors/device/{device_id}GET /sensors/{sensor_id}DELETE /sensors/{sensor_id}
-
Rules (
/rules)POST /rules/GET /rules/DELETE /rules/{rule_id}
-
Consequences (
/consequences)GET /consequences/GET /consequences/{id}PUT /consequences/{id}/execute
-
Event Trigger (
/monitor/trigger)POST /monitor/triggerto enqueue an event
- Configure via
.env - Launch with
docker-compose up --build - Explore Swagger UI at
http://localhost:8000/docs
pytest- WebSocket Integration for instant, real-time device and sensor updates.
- Device & Sensor History endpoints for comprehensive audit trails and analytics.
- Advanced Authentication & Authorization, including refresh tokens, OAuth2 support, and role-based access control enhancements.
- Dashboards & Visualization to present smart home data, trends, and system health.
- CI/CD Pipeline with automated testing, linting, and deployments.
- Third-Party Integrations (e.g., voice assistants, IFTTT, Home Assistant).
- Backup & Restore Mechanism for user data and configurations.
Contributions are welcome! Please fork the repo, create a feature branch, and submit a pull request with descriptive commits.
This project is licensed under the MIT License. See the LICENSE file for details.