magico13 / PyEmVue

Python Library for the Emporia Vue Energy Monitor
MIT License
185 stars 36 forks source link

print device names with usage #21

Closed ifuchs closed 1 year ago

ifuchs commented 2 years ago

I am trying to play around with pyemvue and the first thing I tried to do was print the device names and their usage. However, I seem to get the names of 5 devices of the 10 that the Emporia app shows. Also, this script takes >30 seconds to run which seems rather excessive.

My script looks like this:

import pyemvue
import time
import json

from pyemvue.enums import Scale, Unit

with open('keys.json') as f:
   data = json.load(f)

vue = pyemvue.PyEmVue()

vue.login(id_token=data['idToken'],
    access_token=data['accessToken'],
    refresh_token=data['refreshToken'],
    token_storage_file='keys.json')

devices = vue.get_devices()
deviceGids = []
for device in devices:
    deviceGids.append(device.device_gid)

    devicename = vue.populate_device_properties(device)
    print(devicename.device_name)

channel_usage_list = vue.get_devices_usage(deviceGids, None, scale=Scale.HOUR.value, unit=Unit.KWH.value)

sum=0
for channel in channel_usage_list:
    sum += channel.usage
    print("%2.3f" % channel.usage, `'kwh')
magico13 commented 2 years ago

I realize this response is way late and you might have already figured this out, but here is a script that I use in testing and is working fine to give me the current hour's data.

from datetime import datetime
import json
import pyemvue
from pyemvue.enums import Scale, Unit

with open('keys.json') as f:
    tokens = json.load(f)

vue = pyemvue.PyEmVue()
vue.login(id_token=tokens['idToken'], access_token=tokens['accessToken'], refresh_token=tokens['refreshToken'], token_storage_file='keys.json')

devices = vue.get_devices()
device_gids = []
device_information = {}
for device in devices:
    if not device.device_gid in device_gids:
        device_gids.append(device.device_gid)
        device_information[device.device_gid] = device
    else:
        device_information[device.device_gid].channels += device.channels

channel_usage_list = vue.get_devices_usage(device_gids, datetime.utcnow(), scale=Scale.HOUR.value,unit=Unit.KWH.value)
for channel in channel_usage_list:
    device = device_information[channel.device_gid]
    name = None
    for dev_channel in device.channels:
        if dev_channel.channel_num == channel.channel_num:
            name = dev_channel.name or device.device_name
            break

    print(f"{name} ({channel.channel_num}) - ", "%.3f" % (channel.usage), 'kwh')
sbates130272 commented 1 year ago

This example is out-dated and no longer works.

magico13 commented 1 year ago

I put a newer example in #42 but realize I need to update the examples in the readme per #32