This is built on the pytradfri project and adds a REST API for simpler usage of controlling your IKEA lights.
GET /devices # retrieve all devices
GET /groups # retrieve all groups
GET /devices/65545 # get single device information
PUT /devices/65545/state/1 # turn device on
PUT /devices/65545/state/0 # turn device off
PUT /groups/131077/dimmer/60 # dim all lamps in group to 60%
PUT /gateway/restart # restart gateway
Use Swagger UI to view all available endpoints.
Below is tested on Raspbian (stretch) and Linux Mint 20.3 (Una), but should work in all similar environments.
Install Docker first if you haven't:
sudo apt install docker-ce # Raspbian
sudo apt install docker.io # other
Then run:
./docker/build.sh # builds the image
./docker/run.sh 2080 # starts server at http://localhost:2080/
First time only:
sudo ./docker/install-coap-client.sh
sudo apt install python3-venv python3-pip
python3 -m venv .venv
source .venv/bin/activate
pip3 install -r requirements.txt
./create-crypt-key.sh # creates rest/crypt.key (login tokens depend on this)
Run:
source .venv/bin/activate
./rest/rest.py 2080 # start server at http://localhost:2080/
./test/test.py --help # automatically test some of the endpoints
Navigate to http://localhost:2080/ to view the Swagger UI and all available endpoints.
First you'll need to login providing the IP of the gateway and the security code (written on the back of the gateway):
curl --request POST \
--url http://localhost:2080/gateway/login \
--header 'content-type: application/json' \
--data '{ "host": "192.168.0.7", "code": "0RfPG338tTwBthto" }'
The response will contain a authorization token:
{ "token": "ZXCWeWiPfYhki..." }
Use this in the header for all other requests, like this:
curl -X GET --header 'authorization: Bearer ZXCWeWiPfYhki...' http://localhost:2080/devices
curl -X PUT --header 'authorization: Bearer ZXCWeWiPfYhki...' http://localhost:2080/devices/65545/state/1
This token does never expire, but keep in mind it's linked to your IKEA gateway IP, so using a static IP might be a good idea.
- Setting dimmer value on a group will not turn on lights who already have that exact value since earlier. This works on individual devices.