|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import sys |
| 3 | + |
| 4 | +from pytradfri import Gateway |
| 5 | +from pytradfri.api.libcoap_api import APIFactory |
| 6 | +from pytradfri.error import PytradfriError |
| 7 | +from pytradfri.util import load_json, save_json |
| 8 | + |
| 9 | +IP_FILE = sys.path[0] + '/ip' |
| 10 | +KEYS_FILE = sys.path[0] + '/tradfri_standalone_psk.conf' |
| 11 | +MODES_FILE = sys.path[0] + '/modes.json' |
| 12 | + |
| 13 | +host = open(IP_FILE).read().strip() |
| 14 | +conf = load_json(KEYS_FILE) |
| 15 | + |
| 16 | +mode_name = sys.argv[1] if len(sys.argv) > 1 else "on" |
| 17 | +modes = load_json(MODES_FILE) |
| 18 | + |
| 19 | +def run(config, mode): |
| 20 | + identity = config.get('identity') |
| 21 | + psk = config.get('key') |
| 22 | + api_factory = APIFactory(host=host, psk_id=identity, psk=psk) |
| 23 | + |
| 24 | + api = api_factory.request |
| 25 | + |
| 26 | + gateway = Gateway() |
| 27 | + |
| 28 | + devices_command = gateway.get_devices() |
| 29 | + devices_commands = api(devices_command) |
| 30 | + devices = api(devices_commands) |
| 31 | + |
| 32 | + lights = [dev for dev in devices if dev.has_light_control] |
| 33 | + |
| 34 | + for light in lights: |
| 35 | + try: |
| 36 | + dim_command = light.light_control.set_dimmer(mode.get('dimmer')) # 0-254 |
| 37 | + api(dim_command) |
| 38 | + except: |
| 39 | + print("No `dimmer` in mode", mode_name, mode) |
| 40 | + pass |
| 41 | + |
| 42 | + try: |
| 43 | + color_command = light.light_control.set_color_temp(mode.get('color_temp')) # 250-454 |
| 44 | + api(color_command) |
| 45 | + except: |
| 46 | + print("No `color_temp` in mode", mode_name, mode) |
| 47 | + pass |
| 48 | + |
| 49 | +run(conf[host], modes[mode_name]) |
0 commit comments