-
Notifications
You must be signed in to change notification settings - Fork 14
/
module_usage_example.py
59 lines (46 loc) · 1.55 KB
/
module_usage_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
import asyncio
import logging
import random
import sys
# pip install SenseLink
from senselink import SenseLink
root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
async def change_mutable_plug_power(plug):
while True:
power = random.randrange(2, 15, 1)
plug.data_source.power = power
logging.info(f"Changed power to {power}")
await asyncio.sleep(random.randrange(1, 4, 1))
# Config example
# sources:
# - mutable:
# plugs:
# - mutable1:
# alias: "Mutable 1"
# mac: 50:c7:bf:f6:4f:39 # used specifically below
# power: 15
async def main():
# Get config
config = open('config.yml', 'r')
# Create controller, with config
controller = SenseLink(config)
# Create instances
controller.create_instances()
# Get Mutable controller object, and create task to update it
mutable_plug = controller.plug_for_mac("50:c7:bf:f6:4f:39")
plug_update = change_mutable_plug_power(mutable_plug)
# Get base SenseLink tasks (for other controllers in the config, perhaps), and
# add our new top level plug task, as well as the main SenseLink controller itself
tasks = controller.tasks
tasks.add(plug_update)
tasks.add(controller.server_start())
# Start all the tasks
logging.info("Starting SenseLink controller")
await asyncio.gather(*tasks)
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
logging.info("Interrupt received, stopping SenseLink")