-
Notifications
You must be signed in to change notification settings - Fork 0
/
switchbot.py
143 lines (96 loc) · 3.73 KB
/
switchbot.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import sys
from switchbot.switchbot_api import SwitchBotApi
from switchbot.switchbot_devices import Meter
from switchbot.config import AppConfig
from timestream.write_client import WriteClient
def test_devices(bot_press=False):
switchbot = SwitchBotApi()
devices = switchbot.get_devices()
print('DEVICES:')
print(devices)
for device in devices:
print(f"Getting status for {device['deviceId']}")
status = switchbot.get_device_status(device['deviceId'])
print(status)
if bot_press and status and 'deviceType' in status and status['deviceType'] == 'Bot':
switchbot.bot_press(device['deviceId'])
def desklight():
switchbot = SwitchBotApi()
switchbot.bot_press(switchbot.desklight_device_id)
# {'deviceId': 'FD184178F166', 'deviceName': 'Temp 1 Basement', 'deviceType': 'Meter', 'enableCloudService': True, 'hubDeviceId': 'E248AB2668E1'}
def get_meters_from_api_devices():
switchbot = SwitchBotApi()
devices = switchbot.get_devices()
meters = []
if devices:
for device in devices:
# print(device)
if 'deviceType' in device and device['deviceType'] == 'Meter':
meter = Meter(name=device['deviceName'], device_id=device['deviceId'])
status = switchbot.get_device_status(device['deviceId'])
meter.set_measurements(temp=status['temperature'], humidity=status['humidity'])
meters.append(meter)
else:
print('No devices found')
return meters
# Gets temp/humidity and prints to console
def print_meter_readings():
print('Getting temp/humidity..\n')
meters = get_meters_from_api_devices()
if meters:
meters = sorted(meters, key=lambda x: x.device_name)
for meter in meters:
print(meter)
else:
print('No meters found')
# Gets temp/humidity and stores in timestream
def record_meter_readings():
print('Recording temp/humidity..')
meters = get_meters_from_api_devices()
if meters:
app_config = AppConfig()
aws_config = app_config.config['aws']
write_client = WriteClient(region="us-east-1", aws_config=aws_config)
current_time = write_client.current_milli_time()
records = []
for meter in meters:
dimensions = [
{'Name': 'deviceName', 'Value': meter.device_name},
{'Name': 'deviceType', 'Value': 'Meter'},
{'Name': 'deviceId', 'Value': meter.device_id}
]
# All values have to be passed as strings, even if the value type is numerical
temp = {
'Dimensions': dimensions,
'MeasureName': 'temperature',
'MeasureValue': str(meter.temperature),
'MeasureValueType': 'DOUBLE',
'Time': current_time
}
humid = {
'Dimensions': dimensions,
'MeasureName': 'humidity',
'MeasureValue': str(meter.humidity),
'MeasureValueType': 'DOUBLE',
'Time': current_time
}
records.append(temp)
records.append(humid)
print('Writing records')
write_client.write_records(
database_name=aws_config['timestream_db_name'],
table_name=aws_config['timestream_db_table'],
records=records
)
else:
print('No meters found')
if __name__ == '__main__':
command = sys.argv[1] if len(sys.argv) > 1 else None
if command == 'desklight':
desklight()
elif command == 'temp':
print_meter_readings()
elif command == 'temp_record':
record_meter_readings()
else:
test_devices(bot_press=False)