-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightControl.py
116 lines (86 loc) · 3.19 KB
/
LightControl.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
import aiohttp
import os
import asyncio
import pysmartthings
from random import randint
from time import sleep
from Light import Light
from dotenv import load_dotenv
load_dotenv()
token = os.getenv('TOKEN')
device_name = os.getenv('DEVICE_NAME')
loop = asyncio.get_event_loop()
lights_object = Light()
async def lightsOn(object, name):
async with aiohttp.ClientSession() as session:
api = pysmartthings.SmartThings(session, token)
await findDevices(api, object, name)
await object.on()
async def lightsOff(object, name):
async with aiohttp.ClientSession() as session:
api = pysmartthings.SmartThings(session, token)
await findDevices(api, object, name)
await object.off()
async def listDevices():
async with aiohttp.ClientSession() as session:
api = pysmartthings.SmartThings(session, token)
# * Prints all devices
await getLocations(api)
await getDevices(api)
largeDivider()
async def findDevices(api, object, name):
devices = await api.devices()
for device in devices:
if device.label == name:
object.device = device
async def getLocations(api):
locations = await api.locations()
largeDivider()
print(f'{len(locations)} Locations')
for location in locations:
smallDivider()
print(f'Name: {location.name}')
print(f'\tId: {location.location_id}')
async def getDevices(api):
devices = await api.devices()
largeDivider()
print(f'{len(devices)} Devices')
for device in devices:
smallDivider()
await device.status.refresh()
print(f'Label: {device.label}')
print(f'\t Type: {device.name}')
print(f'\tId: {device.device_id}')
print(f'\tCapabilities: {device.capabilities}')
print(f'\tStatus: {device.status.switch}')
def largeDivider():
print('---------------------------------------------------------------------------------------------------------')
def smallDivider():
print('-----------------------------------------------------------------------')
def weirdRandomLights(object, name, low_num = 0, high_num = 60):
subtract_from_high = 0
while True:
try:
num = randint(low_num, high_num - int(subtract_from_high))
if num == low_num:
loop.run_until_complete(lightsOff(object, name))
subtract_from_high=0
print(f'Turning off')
elif num == low_num+1:
loop.run_until_complete(lightsOn(object, name))
subtract_from_high=0
print(f'Turning on')
else:
print(num)
subtract_from_high+=0.4
sleep(1)
print(f'Max: {high_num - int(subtract_from_high)}')
except KeyboardInterrupt:
exit()
#* How to find device names
loop.run_until_complete( listDevices() )
#weirdRandomLights(lights_object, device_name)
#* syntax to run async function
#* loop.run_until_complete( function_name() )
#! For some reason logging doesnt work in async functions or something idk because this is my first time using async functions
#TODO Make async functions work with logging