-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_consumption.py
54 lines (40 loc) · 1.11 KB
/
log_consumption.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
import os
import json
import tuyapower
import pandas as pd
FILEPATH = "data.csv"
DEVICES = [
{
'plugid': 'bfe05adcc475194032xhir',
'plugip': '192.168.178.34',
'plugkey': '86ccc1678b3fd823',
'plugvers': '3.3',
'name': '1'
}
]
def query_consumption(devices):
data = []
for device in devices:
json_output = tuyapower.deviceJSON(
device['plugid'],
device['plugip'],
device['plugkey'],
device['plugvers']
)
parsed_data = json.loads(json_output)
parsed_data['name'] = device['name']
data.append(parsed_data)
return data
def write_data_to_csv(filepath, data_dict_list):
dataframes = [pd.DataFrame(dict_, index=[idx])
for idx, dict_ in enumerate(data_dict_list)]
df = pd.concat(dataframes)
if os.path.isfile(filepath):
df.to_csv(filepath, mode='a', index=False, header=False)
else:
df.to_csv(filepath, index=False)
def main():
data = query_consumption(DEVICES)
write_data_to_csv(FILEPATH, data)
if __name__ == '__main__':
main()