Basic Usage Example
This example demonstrates the basic setup and usage of SmartHomeHarmonizer.
Installation
First, install SmartHomeHarmonizer:
pip install smarthomeharmonizer
Simple Setup
Create a file called my_smart_home.py:
Basic SmartHomeHarmonizer setup
"""Basic usage example for SmartHomeHarmonizer.
To run this example:
1. Install SmartHomeHarmonizer: pip install smarthomeharmonizer
2. Run: python basic_usage.py
3. Test with: curl http://localhost:5000/api/v1/devices
"""
from smarthomeharmonizer import create_app, DeviceManager
from smarthomeharmonizer.adapters.smart_light import SmartLightAdapter
def main():
"""Run a basic SmartHomeHarmonizer server."""
# Create device manager
manager = DeviceManager()
# Register devices
living_room_light = SmartLightAdapter('living_room_light', 'Living Room Light')
bedroom_light = SmartLightAdapter('bedroom_light', 'Bedroom Light')
manager.register_device(living_room_light)
manager.register_device(bedroom_light)
# Create Flask application
app = create_app(manager)
print("SmartHomeHarmonizer is running!")
print("Available devices:")
for device in manager.list_devices():
print(f" - {device['name']} (ID: {device['deviceId']})")
print("\nAPI Endpoints:")
print(" GET /api/v1/devices - List all devices")
print(" GET /api/v1/devices/<device_id> - Get device info")
print(" GET /api/v1/devices/<device_id>/state - Get device state")
print(" POST /api/v1/devices/<device_id>/command - Execute command")
# Run the server
app.run(host='0.0.0.0', port=5000, debug=True)
if __name__ == '__main__':
main()
Running the Example
Run the example:
python my_smart_home.py
The server will start on http://localhost:5000.
Testing the API
List all devices:
curl http://localhost:5000/api/v1/devices
Turn on a light:
curl -X POST http://localhost:5000/api/v1/devices/living_room_light/command \
-H "Content-Type: application/json" \
-d '{"command": "turnOn"}'
Set brightness:
curl -X POST http://localhost:5000/api/v1/devices/living_room_light/command \
-H "Content-Type: application/json" \
-d '{"command": "setBrightness", "parameters": {"brightness": 75}}'
Get device state:
curl http://localhost:5000/api/v1/devices/living_room_light/state
What’s Next?
See ../api_reference for complete API documentation
See Custom Adapter Example for creating custom device adapters
See Alexa Integration for voice assistant integration