This is a Python class to communicate with the IKEA Trådfri (Tradfri) ZigBee-based Gateway. The gateway can control IKEA lights and also Philips Hue bulbs. Some of the features include:
- Get information on the gateway
- List all devices connected to gateway
- List all lights and get attributes of lights (name, state, color temp, dimmer level etc)
- Change attribute values of lights (name, state, color temp, dimmer level etc)
Table of contents:
- Installation
- Stand-alone use (command-line interface)
- Implement in your own Python platform
- Docker support
- Acknowledgements
In order to use the code, you first need to install libcoap as per the following instructions (you might have to use sudo for some commands to work):
$ apt-get install libtool
$ git clone --depth 1 --recursive -b dtls https://github.com/home-assistant/libcoap.git
$ cd libcoap
$ ./autogen.sh
$ ./configure --disable-documentation --disable-shared --without-debug CFLAGS="-D COAP_DEBUG_FD=stderr"
$ make
$ make install
If you want to test this library stand-alone in a command-line interface:
$ python3 -i -m pytradfri IP KEY
Where the following variables are substituted:
- IP is the IP-address to your gateway.
- KEY is written on the back of your IKEA Tradfri Gateway.
List all lights:
lights
Set brightnes of item 1 to 50 in lights list:
lights[1].set_light_brightness(50)
#!/usr/bin/env python3
# put all of this in test_pytradfri.py
# Run by executing the following command from shell, from the same folder you have stored test_pytradfri.py in.
# python3 -m test_pytradfri IP KEY
# Pre-requisites
# pip3 install pytradfri
import sys
import pytradfri
# Assign configuration variables. The configuration check takes care they are present.
api = pytradfri.coap_cli.api_factory(sys.argv[1], sys.argv[2])
gateway = pytradfri.gateway.Gateway(api)
devices = gateway.get_devices()
lights = [dev for dev in devices if dev.has_light_control]
# Print all lights
print(lights)
# Lights can be accessed by its index, so lights[1] is the second light
# Example 1: checks state of the light 2 (true=on)
print(lights[1].light_control.lights[0].state)
# Example 2: get dimmer level of light 2
print(lights[1].light_control.lights[0].dimmer)
# Example 3: What is the name of light 2
print(lights[1].name)
# Example 4: Set the light level of light 2
lights[1].light_control.set_dimmer(20)
# Example 5: Change color of light 2
lights[1].light_control.set_hex_color('f5faf6') # f5faf6 = cold | f1e0b5 = normal | efd275 = warm
There is a Docker script available to bootstrap a dev environment. Run ./script/dev_docker
and you will build and launch a container that is ready to go. After launching, follow the above instructions to test the library stand-alone.
This is an implementation based on analysis I found here by vidarlo.
A lot of work was also put in by Paulus Schoutsen (@balloob) who took the initial code concept into this library.