- Homebridge server is :
192.168.1.22
- Raspberry Pi controlling the lightbulb :
192.168.1.55
Here is the accessory I need to add:
"accessories": [
{
"name": "Kitchen Lightbulb",
"accessory": "MinimalisticHttpLightbulbBrightness",
"get_on_off_url": "http://127.0.0.1:9000/get/on_off/",
"set_on_off_url": "http://127.0.0.1:9000/set/on_off/%on_off%",
"get_brightness_url": "http://127.0.0.1:9000/get/brightness/",
"set_brightness_url": "http://127.0.0.1:9000/set/brightness/%brightness%
}
]
Here is a reminder of the default values the accessory use if you don't write them explicitely:
{
"get_on_off_expected_response_code": "GET",
"set_on_off_expected_response_code": "POST",
"get_brightness_expected_response_code": "GET",
"set_brightness_expected_response_code": "POST",
"get_on_off_expected_response_code": "200",
"set_on_off_expected_response_code": "204",
"get_brightness_expected_response_code": "200",
"set_brightness_expected_response_code": "204",
"get_on_off_polling_millis": "500",
"get_brightness_polling_millis": "500"
}
As you can see, the Raspberry Pi will need to host an HTTP server providing three routes:
GET /get/on_off/
that responds200 OK
with the current on/off state of the lightbulbsPOST /set/on_off/%on_off%/
that responds204 NO CONTENT
to HomebridgeGET /get/brightness/
that responds200 OK
with the current brightness of the lightbulbsPOST /set/brightness/%brightness%/
that responds204 NO CONTENT
to Homebridge
This example implementation uses Flask, a very simple and easy to use HTTP serving API.
from flask import Flask
app = Flask(__name__)
on_off = False
brightness = 0
@app.route('/get/on_off/', methods=['GET'])
def get_on_off():
global on_off
#print('get on_off:', '1' if on_off else '0')
return '1' if on_off else '0', 200
@app.route('/set/on_off/<int:arg>', methods=['POST'])
def set_on_off(arg):
global on_off
on_off = arg == 1
print('set on_off:', on_off)
return '', 204
@app.route('/get/brightness/', methods=['GET'])
def get_brightness():
global brightness
#print('get brightness', brightness)
return str(brightness), 200
@app.route('/set/brightness/<int:arg>', methods=['POST'])
def set_brightness(arg):
global brightness
brightness = arg
print('set brightness:', brightness)
return '', 204
app.run(host='0.0.0.0', port=9000)