-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtradfri-lights.py
executable file
·91 lines (76 loc) · 3.13 KB
/
tradfri-lights.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
# file : tradfri-lights.py
# purpose : getting status from the Ikea tradfri smart lights
#
# author : harald van der laan
# date : 2017/11/01
# version : v1.2.0
#
# changelog :
# - v1.2.0 update for gateway 1.1.15 issues (harald)
# - v1.1.0 refactor for cleaner code (harald)
# - v1.0.0 initial concept (harald)
"""
tradfri-lights.py - controlling the Ikea tradfri smart lights
This module requires libcoap with dTLS compiled, at this moment there is no python coap module
that supports coap with dTLS. see ../bin/README how to compile libcoap with dTLS support
"""
# pylint convention disablement:
# C0103 -> invalid-name
# C0200 -> consider-using-enumerate
# pylint: disable=C0200, C0103
from __future__ import print_function
from __future__ import unicode_literals
import os
import sys
import ConfigParser
import argparse
from textwrap import wrap
from tradfri import tradfriActions
def parse_args():
""" function for getting parsed arguments """
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--action', choices=['power', 'brightness', 'color'], required=True)
parser.add_argument('-l', '--lightbulbid', help='lightbulbid got from tradfri-status.py',
required=True)
parser.add_argument('-v', '--value',
help='power: on/off, brightness: 0-100, color: warm/normal/cold',
required=True)
args = parser.parse_args()
return args
def main():
""" main function """
args = parse_args()
conf = ConfigParser.ConfigParser()
script_dir = os.path.dirname(os.path.realpath(__file__))
conf.read(script_dir + '/tradfri.cfg')
hubip = conf.get('tradfri', 'hubip')
apiuser = conf.get('tradfri', 'apiuser')
apikey = conf.get('tradfri', 'apikey')
if args.action == 'power':
if args.value == 'on' or args.value == 'off':
tradfriActions.tradfri_power_light(hubip, apiuser, apikey, args.lightbulbid, args.value)
else:
sys.stderr.write('[-] Tradfri: power state can only be on/off\n')
sys.exit(1)
elif args.action == 'brightness':
if 1 <= int(args.value) <= 100:
tradfriActions.tradfri_dim_light(hubip, apiuser, apikey, args.lightbulbid, args.value)
else:
sys.stderr.write('[-] Tradfri: dim value can only be between 1 and 100\n')
sys.exit(1)
elif args.action == 'color':
if args.value in tradfriActions.get_color_dict().keys():
tradfriActions.tradfri_color_light(hubip, apiuser, apikey, args.lightbulbid, args.value)
else:
message = '[-] Tradfri: color value can only be: '
for color in tradfriActions.get_color_dict().keys():
if ' ' in color:
message += " '%s'," % color
else:
message += " %s," % color
sys.stderr.write("\n".join(wrap(message)))
sys.exit(1)
if __name__ == "__main__":
main()
sys.exit(0)