Skip to content

Commit 07369a6

Browse files
committedJan 27, 2019
pytradfri - lightbulb control
1 parent cca3fe6 commit 07369a6

File tree

10 files changed

+129
-0
lines changed

10 files changed

+129
-0
lines changed
 

‎pytradfri/first_run

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
python3 -i -m pytradfri `cat ip` -K `cat security`

‎pytradfri/get_ip

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
cd `dirname "$0"`
3+
sudo arping -c1 `cat mac` 2>/dev/null | grep from | sed 's/^.*from \([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\) .*$/\1/' | tee ./ip

‎pytradfri/install

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
3+
# dependencies
4+
sudo apt install python3 \
5+
python3-pip \
6+
python3-setuptools \
7+
python3-wheel \
8+
libpython3-dev \
9+
arping \
10+
libtool
11+
12+
# python dependencies
13+
pip3 install 'aiocoap==0.4a1' \
14+
'DTLSSocket==0.1.7' \
15+
'typing>=3,<4' \
16+
pytradfri
17+
18+
# /usr/lobcal/bin/coap-client
19+
git clone --depth 1 --recursive -b dtls https://github.com/home-assistant/libcoap.git
20+
cd libcoap
21+
./autogen.sh
22+
./configure --disable-documentation --disable-shared --without-debug CFLAGS="-D COAP_DEBUG_FD=stderr"
23+
make
24+
sudo make install
25+
cd ..
26+
rm -rf libcoap/

‎pytradfri/ip

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
192.168.1.2

‎pytradfri/light

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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])

‎pytradfri/mac

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
00:00:00:00:00:00

‎pytradfri/modes.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"on": {
3+
"dimmer": 127,
4+
"color_temp": 454
5+
},
6+
"off": {
7+
"dimmer": 0
8+
},
9+
10+
"max": {
11+
"dimmer": 254,
12+
"color_temp": 250
13+
},
14+
"min": {
15+
"dimmer": 1,
16+
"color_temp": 454
17+
},
18+
19+
"intmax": {
20+
"dimmer": 254
21+
},
22+
"intmid": {
23+
"dimmer": 127
24+
},
25+
"intmin": {
26+
"dimmer": 1
27+
},
28+
29+
"tempmax": {
30+
"color_temp": 250
31+
},
32+
"tempmid": {
33+
"color_temp": 352
34+
},
35+
"tempmin": {
36+
"color_temp": 454
37+
}
38+
}

‎pytradfri/run

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
python3 -i -m pytradfri `cat ip`

‎pytradfri/security

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xxxxxxxxxxxxxxxx

‎pytradfri/tradfri_standalone_psk.conf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"192.168.1.2": {
3+
"identity": "00000000000000000000000000000000",
4+
"key": "yyyyyyyyyyyyyyyy"
5+
}
6+
}

0 commit comments

Comments
 (0)
Please sign in to comment.