forked from SukramJ/hahomematic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
122 lines (104 loc) · 3.8 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
116
117
118
119
120
121
122
"""Example for hahomematic."""
# !/usr/bin/python3
from __future__ import annotations
import asyncio
import logging
import sys
from aiohttp import ClientSession, TCPConnector
from hahomematic import config, const
from hahomematic.central_unit import CentralConfig
from hahomematic.client import InterfaceConfig
from hahomematic.platforms.custom.definition import validate_entity_definition
logging.basicConfig(level=logging.DEBUG)
_LOGGER = logging.getLogger(__name__)
CCU_HOST = "192.168.1.173"
CCU_USERNAME = "Admin"
CCU_PASSWORD = ""
class Example:
"""Example for hahomematic."""
# Create a server that listens on 127.0.0.1:* and identifies itself as myserver.
got_devices = False
def __init__(self):
"""Init example."""
self.SLEEPCOUNTER = 0
self.central = None
def _systemcallback(self, name, *args, **kwargs):
self.got_devices = True
if (
name == const.HmSystemEvent.NEW_DEVICES
and kwargs
and kwargs.get("device_descriptions")
and len(kwargs["device_descriptions"]) > 0
):
self.got_devices = True
return
if (
name == const.HmSystemEvent.DEVICES_CREATED
and kwargs
and kwargs.get("new_devices")
and len(kwargs["new_devices"]) > 0
):
if len(kwargs["new_devices"]) > 1:
self.got_devices = True
return
def _eventcallback(self, interface_id, channel_address, parameter, value):
pass
def _hacallback(self, eventtype, event_data):
pass
async def example_run(self):
"""Process the example."""
central_name = "ccu-dev"
interface_configs = {
InterfaceConfig(
central_name=central_name,
interface=const.HmInterfaceName.HMIP_RF,
port=2010,
),
InterfaceConfig(
central_name=central_name,
interface=const.HmInterfaceName.BIDCOS_RF,
port=2001,
),
InterfaceConfig(
central_name=central_name,
interface=const.HmInterfaceName.VIRTUAL_DEVICES,
port=9292,
remote_path="/groups",
),
}
self.central = CentralConfig(
name=central_name,
host=CCU_HOST,
username=CCU_USERNAME,
password=CCU_PASSWORD,
central_id="1234",
storage_folder="homematicip_local",
interface_configs=interface_configs,
default_callback_port=54321,
client_session=ClientSession(connector=TCPConnector(limit=3)),
).create_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.register_system_event_callback(self._systemcallback)
self.central.register_entity_event_callback(self._eventcallback)
self.central.register_ha_event_callback(self._hacallback)
await self.central.start()
while not self.got_devices and self.SLEEPCOUNTER < 20:
_LOGGER.info("Waiting for devices")
self.SLEEPCOUNTER += 1
await asyncio.sleep(1)
await asyncio.sleep(5)
for i in range(16):
_LOGGER.info("Sleeping (%i)", i)
await asyncio.sleep(2)
# Stop the central_1 thread so Python can exit properly.
await self.central.stop()
# validate the device description
if validate_entity_definition():
example = Example()
asyncio.run(example.example_run())
sys.exit(0)