forked from danielperna84/pyhomematic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
80 lines (61 loc) · 2.52 KB
/
example.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
#!/usr/bin/python3
import time
import sys
import logging
from pyhomematic import HMConnection
logging.basicConfig(level=logging.INFO)
DEVICE1 = 'address_of_rollershutter_device' # e.g. KEQ7654321
DEVICE2 = 'address_of_doorcontact' # e.g. LEQ1234567
DEVICE3 = 'address_of_thermostat'
def systemcallback(src, *args):
print(src)
for arg in args:
print(arg)
try:
# Create a server that listens on 127.0.0.1:7080 and identifies itself as myserver.
# Connect to Homegear at 127.0.0.1:2001
# Automatically start everything. Without autostart, pyhomematic.start() can be called.
# We add a systemcallback so we can see what else happens besides the regular events.
pyhomematic = HMConnection(interface_id="myserver",
autostart=True,
systemcallback=systemcallback,
remotes={"rf":{
"ip":"127.0.0.1",
"port": 2001}})
except Exception:
sys.exit(1)
sleepcounter = 0
def eventcallback(address, interface_id, key, value):
print("CALLBACK: %s, %s, %s, %s" % (address, interface_id, key, value))
while not pyhomematic.devices and sleepcounter < 20:
print("Waiting for devices")
sleepcounter += 1
time.sleep(1)
print(pyhomematic.devices)
# Get level of rollershutter from 0.0 to 1.0.
print(pyhomematic.devices[DEVICE1].get_level())
# Set level of rollershutter to 50%.
pyhomematic.devices[DEVICE1].set_level(0.5)
time.sleep(10)
# Move rollershutter down.
pyhomematic.devices[DEVICE1].move_down()
time.sleep(10)
# Get level of rollershutter from 0.0 to 1.0 directly from channel.
print(pyhomematic.devices_all[DEVICE1 + ':1'].getValue("LEVEL"))
# Check if doorcontact is open by querying the device.
print(pyhomematic.devices[DEVICE2].is_open())
# Check if doorcontact is open or closed by querying the device-channel. True or False, depending on state.
print(pyhomematic.devices_all[DEVICE2 + ':1'].getValue("STATE"))
# Get Actual Temperature
print(pyhomematic.devices[DEVICE3].actual_temperature)
# Get Set Temperature
print(pyhomematic.devices[DEVICE3].set_temperature)
# Get Battery State
print(pyhomematic.devices[DEVICE3].battery_state)
# Set an eventcallback for the doorcontact that should be called when events occur.
pyhomematic.devices[DEVICE2].setEventCallback(eventcallback)
time.sleep(10)
# Now open / close doorcontact and watch the eventcallback being called.
# Stop the server thread so Python can exit properly.
pyhomematic.stop()
sys.exit(0)