-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
72 lines (61 loc) · 2.91 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
import aiohttp
import asyncio
import libhomeseer
import logging
import sys
async def main():
if len(sys.argv) < 2:
print("Usage: python example.py ipaddress")
exit(1)
host = sys.argv[1]
websession = aiohttp.ClientSession()
# Create an instance of the HomeSeer class by calling its constructor with (at minimum) host and websession
# host is a string containing the IP address of the HomeSeer software installation
# websession is an instance of aiohttp.ClientSession
# You must also pass username, password, http_port, and ascii_port to the constructor if these are not the defaults
# username = "default", password = "default", http_port = 80, ascii_port = 11000
homeseer = libhomeseer.HomeSeer(host, websession)
# libhomeseer does not implement asyncio timeouts directly so your implementation must provide for timeouts
timeout = 10
# Initialize the HomeSeer connection by awaiting HomeSeer.initialize()
# HomeSeer.initialize() populates HomeSeer.devices and HomeSeer.events
# HomeSeer.devices is a dict of (supported) device objects indexed by ref
# HomeSeer.events is a list of event objects
try:
await asyncio.wait_for(homeseer.initialize(), timeout)
except asyncio.TimeoutError:
print(f"Error connecting to HomeSeer at {host}")
return
# Not all HomeSeer devices are fully supported in libhomeseer yet
# Devices with as-yet unsupported control pairs will be reported in debug logging
# Device attributes (ref, value, status, etc.) are represented as properties of the device object
# Certain device classes feature methods to control the device based on its detected Control Pairs:
# HomeSeerSwitchableDevice - device.on() and device.off()
# HomeSeerDimmableDevice - device.on(), device.off(), and device.dim(0-100)
# HomeSeerLockableDevice - device.lock() and device.unlock()
print()
print("-----------------")
print("HomeSeer Devices:")
print("-----------------")
for device in homeseer.devices.values():
print(
f"{device.location2} {device.location} {device.name} "
f"(Type: {device.device_type_string}, Ref: {device.ref}) (libhomeseer type: {type(device)})"
)
# All HomeSeer events will be represented as an event object
# Event attributes (group and name) are represented as properties of the device object
print()
print("----------------")
print("HomeSeer Events:")
print("----------------")
for event in homeseer.events:
print(f"Group: {event.group}, Name: {event.name}")
# Start the ASCII listener by awaiting HomeSeer.start_listener()
# Stop the ASCII listener by awaiting HomeSeer.stop_listener()
print()
print("Starting HomeSeer ASCII listener...")
await homeseer.start_listener()
logging.basicConfig(level=logging.DEBUG)
loop = asyncio.get_event_loop()
loop.create_task(main())
loop.run_forever()