PyTradfriLight

class knut.services.light.pytradfri.PyTradfriLight(location, uid, room, device_id, host, psk_id, psk)

A TRÅDFRI light service.

Extend the Light to implement a light which uses the pytradfri package to control an IKEA TRÅDFRI light.

Connect to a TRÅDFRI light with it’s the device_id. To connect to the TRÅDFRI gateway with the known host address, a pre-shared key psk with its uid psk_id is used. Those are generated as following:

from pytradfri.api.libcoap_api import APIFactory
import uuid

host = ''  # the IP of the gateway
key = ''  # the 'Security Code' of the Gateway (16 chars)

psk_id = uuid.uuid4().hex
api_factory = APIFactory(host=host, psk_id=psk_id)
psk = api_factory.generate_psk(key)
print('Generated PSK: ', psk)
print('Generated PSK_ID: ', psk_id)

To get the device_id of all devices known by the gateway, run the following commands with the host, psk_id and psk obtained in the previous step:

from pytradfri.api.libcoap_api import APIFactory
from pytradfri import Gateway

api = APIFactory(host, psk_id, psk).request
gateway = Gateway()
devices_commands = api(gateway.get_devices())
devices = api(devices_commands)

for device in devices:
    print('Found device %s with the device_id %i'
          % (device.name, device.uid))
device_id = None

The TRÅDFRI device id.

mired_to_precent(value)

Return the mired value in percentage.

Convert the mired value to a percentage value in the range [0, 100], where 0 is cold and 100 warm.

observation()

Observe the TRÅDFRI light.

If the observed light changes, update_backend() is called.

percent_to_mired(value)

Return the mired value of value.

Convert the value which is in the range [0, 100], where 0 is cold and 100 warm, to the corresponding mired value.

status_setter(status)

Applies the status to the back-end.

The status dictionary must have 'state' as key and can have the following keys additional:

  • The 'dimlevel' dimlevel.

  • The lights 'temperature' temperature.

  • The light 'color' light.

update_backend(device)

Update the back-end to the device state.

Update the back-end to the parsed TRÅDFRI device. The update is only done, if the id of device equals the device_id.

update_device()

Update the TRÅDFRI device to the back-end.