-
Notifications
You must be signed in to change notification settings - Fork 0
/
juicebox_tplink.py
56 lines (44 loc) · 1.51 KB
/
juicebox_tplink.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
import asyncio
from senselink import SenseLink
from juicebox import Juicebox
import socket
import json
import logging
import sys
root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
watts = 0
def update_watts(w):
global watts
logging.info("Updating wattage %f" %(w))
watts = w
async def change_mutable_plug_power(plug):
global watts
while True:
plug.data_source.power = float(watts)
await asyncio.sleep(1)
async def main():
# Get config
config = open('juicelink.yml', 'r')
# Create controller, with config
controller = SenseLink(config, 9999)
# Create instances
controller.create_instances()
j = Juicebox("192.168.50.202", update_watts)
# Get Mutable controller object, and create task to update it
mutable_plug = controller.plug_for_mac("70:AC:00:00:00:00")
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())
tasks.add(j.readForever())
print("Starting SenseLink controller")
await asyncio.gather(*tasks)
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
logging.info("Interrupt received, stopping SenseLink")