forked from SukramJ/hahomematic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
115 lines (100 loc) · 3.58 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# !/usr/bin/python3
import asyncio
import logging
import sys
import time
from hahomematic import config, const
from hahomematic.central_unit import CentralConfig
from hahomematic.client import InterfaceConfig
from hahomematic.devices.entity_definition import validate_entity_definition
from hahomematic.xml_rpc_server import register_xml_rpc_server
logging.basicConfig(level=logging.DEBUG)
_LOGGER = logging.getLogger(__name__)
CCU_HOST = "192.168.1.173"
CCU_USERNAME = "Admin"
CCU_PASSWORD = ""
class Example:
# Create a server that listens on 127.0.0.1:* and identifies itself as myserver.
got_devices = False
def __init__(self):
self.SLEEPCOUNTER = 0
self.central = None
def systemcallback(self, src, *args):
self.got_devices = True
print("systemcallback: %s" % src)
if src == const.HH_EVENT_NEW_DEVICES and args and args[0] and len(args[0]) > 0:
self.got_devices = True
print("Number of new device descriptions: %i" % len(args[0]))
return
elif src == const.HH_EVENT_DEVICES_CREATED and args and args[0] and len(args[0]) > 0:
self.got_devices = True
print("New devices:")
print(len(args[0]))
return
for arg in args:
print("argument: %s" % arg)
def eventcallback(self, address, interface_id, key, value):
print(
"eventcallback at %i: %s, %s, %s, %s"
% (int(time.time()), address, interface_id, key, value)
)
def hacallback(self, eventtype, event_data):
print(
"hacallback: %s, %s"
% (
eventtype,
event_data,
)
)
async def example_run(self):
interface_configs = {
InterfaceConfig(
interface="HmIP-Rf",
port=2010,
),
InterfaceConfig(
interface="BidCos-RF",
port=2001,
),
InterfaceConfig(
interface="VirtualDevices",
port=9292,
path="/groups",
),
}
self.central = await CentralConfig(
domain="hahm",
name="ccu-dev",
loop=asyncio.get_running_loop(),
xml_rpc_server=register_xml_rpc_server(),
host=CCU_HOST,
username=CCU_USERNAME,
password=CCU_PASSWORD,
storage_folder="hahm",
interface_configs=interface_configs,
).get_central()
# For testing we set a short INIT_TIMEOUT
config.INIT_TIMEOUT = 10
# We have to set the cache location of stored data so the central_1 can load
# it while initializing.
config.CACHE_DIR = "cache"
# Add callbacks to handle the events and see what happens on the system.
self.central.callback_system_event = self.systemcallback
self.central.callback_entity_event = self.eventcallback
self.central.callback_ha_event = self.hacallback
await self.central.start()
while not self.got_devices and self.SLEEPCOUNTER < 20:
print("Waiting for devices")
self.SLEEPCOUNTER += 1
await asyncio.sleep(1)
await asyncio.sleep(5)
for i in range(16):
_LOGGER.debug("Sleeping (%i)", i)
await asyncio.sleep(2)
# Stop the central_1 thread so Python can exit properly.
await self.central.stop()
# valdate the device description
if validate_entity_definition():
example = Example()
asyncio.run(example.example_run())
sys.exit(0)