forked from hvanderlaan/ikea-smartlight
-
Notifications
You must be signed in to change notification settings - Fork 17
/
tradfri-groups.py
executable file
·71 lines (57 loc) · 2.23 KB
/
tradfri-groups.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
#!/usr/bin/env python
# file : tradfri-groups.py
# purpose : controlling status from the Ikea tradfri smart groups
#
# author : harald van der laan
# date : 2017/04/10
# version : v1.1.0
#
# changelog :
# - v1.1.0 refactor for cleaner code (harald)
# - v1.0.0 initial concept (harald)
"""
tradfri-groupss.py - controlling the Ikea Tradfri smart groups
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
import sys
import ConfigParser
import argparse
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('-g', '--groupid', help='get group ID from tradfri-status.py',
required=True)
parser.add_argument('-v', '--value',
help='power: on/off, brightness: 0 - 100', required=True)
args = parser.parse_args()
return args
def main():
""" main function """
args = parse_args()
conf = ConfigParser.ConfigParser()
conf.read('tradfri.cfg')
hubip = conf.get('tradfri', 'hubip')
securityid = conf.get('tradfri', 'securityid')
if args.action == 'power':
if args.value == 'on' or args.value == 'off':
tradfriActions.tradfri_power_group(hubip, securityid, args.groupid, args.value)
else:
sys.stderr.write('[-] Tradfri: power state can only be on/off\n')
sys.exit(1)
elif args.action == 'brightness':
if 0 <= int(args.value) <= 100:
tradfriActions.tradfri_dim_group(hubip, securityid, args.groupid, args.value)
else:
sys.stderr.write('[-] Tradfri: dim value can only be between 1 and 100\n')
sys.exit(1)
if __name__ == "__main__":
main()
sys.exit(0)