| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- import pyemvue
- import json
- import datetime
- from pyemvue.enums import Scale, Unit
- def printRecursive(usageDict, info, depth=0):
- for gid, device in usageDict.items():
- for channelNum, channel in device.channels.items():
- name = channel.name
- if name == 'Main':
- name = info[gid].device_name
- print('-'*depth, f'{gid}\t{channelNum}\t{name}\t{channel.usage}\tkWh')
- if channel.nested_devices:
- printRecursive(channel.nested_devices, info, depth+1)
- def usageOverTime(vue, device):
- now = datetime.datetime.now(datetime.timezone.utc)
- search_end = now
- # search_end = now - datetime.timedelta(hours=12)
- search_start = search_end - datetime.timedelta(hours=12)
- usage_over_time, start_time = vue.get_chart_usage(
- device.channels[0],
- search_start,
- search_end,
- scale=Scale.MINUTE.value,
- unit=Unit.KWH.value
- )
- print('Usage for the last 7 days, starting', start_time.isoformat())
- currTime = start_time
- for usage in usage_over_time:
- print(currTime, usage*1000, 'Wh')
- currTime += datetime.timedelta(minutes=1)
- if __name__ == '__main__':
- with open('keys.json') as f:
- keys = json.load(f)
- vue = pyemvue.PyEmVue()
- if 'id_token' in keys:
- # Login using access tokens.
- vue.login(
- id_token=keys['id_token'],
- access_token=keys['access_token'],
- refresh_token=keys['refresh_token'],
- token_storage_file='keys.json'
- )
- else:
- # Login with username & password, and update keys.json.
- vue.login(
- username=keys['username'],
- password=keys['password'],
- token_storage_file='keys.json'
- )
-
- devices = vue.get_devices()
- # Show current usage for all devices.
- deviceGIDs = []
- deviceInfo = {}
- for device in devices:
- gid = device.device_gid
- if not gid in deviceGIDs:
- deviceGIDs.append(gid)
- deviceInfo[gid] = device
- else:
- deviceInfo[gid].channels += device.channels
-
- usageDict = vue.get_device_list_usage(
- deviceGids=deviceGIDs,
- instant=None,
- scale=Scale.MINUTE.value,
- unit=Unit.KWH.value
- )
- print(f'device_gid\tchannel_num\tname\tusage\tunit')
- printRecursive(usageDict, deviceInfo)
- # Show previous usage for the first device.
- usageOverTime(vue, devices[0])
|