magico13 / PyEmVue

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

Possible they changed URL's again ? #25

Closed aviadoffer closed 2 years ago

aviadoffer commented 2 years ago

devices = vue.get_devices() Traceback (most recent call last): File "", line 1, in File "c:\Python38-64\lib\site-packages\pyemvue-0.12.4-py3.8.egg\pyemvue\pyemvue.py", line 40, in get_devices File "c:\Python38-64\lib\site-packages\requests\models.py", line 941, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.emporiaenergy.com/customers/9695/devices?detailed=true&hie rarchy=true

magico13 commented 2 years ago

They did, I'm working on a fix as we speak. The way the "device usage" info comes back is totally different so that's the biggest change that I'm trying to deal with. Fixed the customers call pretty easily (don't need to provide the gid manually, they pull it off the auth token) but the usage call provides all the data nested now which is super handy for some things but also completely different from before.

magico13 commented 2 years ago

I'm going to try to be better about checking after app updates because I could have caught this a week ago and prepped for it.

aviadoffer commented 2 years ago

You the man! thank you.

mikemccand commented 2 years ago

Thank you! This just hit me too!

First, I tested interactively (python command-line) and I was able to load my devices. Then I turned those same Python statements into a little .py script, tried to run it, and suddenly hit this Bad Request for url error and was very confused. But it turns out the API changed in between me testing interactively and writing/running a standalone Python script!

So I came here and was really happy to see that 1) this issue was just opened (so it's not me) and 2) @magico13 is already working on a fix!

Any more indication that they might be making/releasing an official public API soon?

magico13 commented 2 years ago

Any more indication that they might be making/releasing an official public API soon?

Hard to say. If they are the app likely won't use it. Biggest indication in the past to me was when they swapped to using "AppAPI" in the url implying there might be some "PublicAPI" or similar endpoint in the future. Or it's just so they can have separate APIs for their different products (like a "BatteryAPI" or something)

jeremy-compostella commented 2 years ago

They did, I'm working on a fix as we speak. The way the "device usage" info comes back is totally different so that's the biggest change that I'm trying to deal with. Fixed the customers call pretty easily (don't need to provide the gid manually, they pull it off the auth token) but the usage call provides all the data nested now which is super handy for some things but also completely different from before.

Thanks for already being on that and I am so looking forward for a fix as my system relying on your library broke an hour ago due to their API change (https://github.com/jeremy-compostella/home-manager). I was trying to see what changed. What is your reverse engineering process ? Let me know if I can help.

magico13 commented 2 years ago

Code updated in master but not deployed to PyPI yet. Need to update the readme and api docs. Replaced get_devices_usage with get_device_list_usage which has a very different return. Here's an example method that will print the usage from all the channels if you pass in the return from that method:

def print_recursive(usage_dict, depth=0):
    for gid, device in usage_dict.items():
        for channelnum, channel in device.channels.items():
            name = channel.name
            print('-'*depth, f'{gid} {channelnum} {name} {channel.usage} kwh')
            if channel.nested_devices:
                print_recursive(channel.nested_devices, depth+1)
magico13 commented 2 years ago

Updated on PyPI with 0.14.0, let me know if there are any additional issues, I did rush it a bit. Closing this.

aviadoffer commented 2 years ago

I get devices now but:

channel_usage_list = vue.get_devices_usage(deviceGids, None, scale=pyemvue.enums.Scale.SECOND.value, unit=pyemvue.enums.Unit.KWH.value) Traceback (most recent call last): File "", line 1, in AttributeError: 'PyEmVue' object has no attribute 'get_devices_usage'

magico13 commented 2 years ago

Correct, that method changed pretty substantially, see this message: https://github.com/magico13/PyEmVue/issues/25#issuecomment-901309335

aviadoffer commented 2 years ago

I missed that sorry. thanks

aviadoffer commented 2 years ago

OK looks like TotalUsage doesn't always work and for AMPS it shows zero.

channel_usage_list = vue.get_device_list_usage(deviceGids, None, scale=pyemvue.enums.Scale.SECOND.value, unit=pyemvue.enums.Unit.AMPHOURS.value)

<pyemvue.device.VueDeviceChannelUsage object at 0x000000146B3B3CA0>

channel_usage_list[deviceGids[0]].channels["Mains_A"].usage -0.002654653647917231 channel_usage_list[deviceGids[0]].channels["Mains_B"].usage -0.0026299228329355823 channel_usage_list[deviceGids[0]].channels["TotalUsage"].usage 0.0

jeremy-compostella commented 2 years ago

Thanks for the prompt resolution.

magico13 commented 2 years ago

I didn't check it with amps and unfortunately I need to get back to work. After work I can look into that if that's alright, it seems to be working ok with kilowatt hours which was my main focus.

aviadoffer commented 2 years ago

yes, no worry. I can sum up manually for now. thanks again for your quick resolution

jertel commented 2 years ago

@aviadoffer Make sure you're not accidentally grabbing the new "Balance" channel. It has a name and channel_num of "Balance" and can have positive or negative values. I ran into this when upgrading the Vuegraf project and those negative usage values caught my eye.

magico13 commented 2 years ago

Something else that I noticed is that the usage for a channel is now the usage with nested devices excluded. So if you have an Emporia plug drawing 100 watts on a circuit drawing 250 watts (total) the plug will say 100 watts and the circuit will say 150 watts like you see in the app. It used to report as 100 and 250.

Where that can get interesting is if you have a plug drawing 1000 watts and the circuit is doing 1200 watts total, it'll report 1000 for the plug and 200 for the circuit. In theory this applies for nested vues as well, say if you have one on a subpanel.

aviadoffer commented 2 years ago

That's screwed up. Anyway seems like Main_A and Main_B works for me for amphours and for KWh/KW I use [1,2,3]. I am sure it will change soon again , so it is what it is.

magico13 commented 2 years ago

Looks like TotalUsage might only populate at the Day and higher scale and only shows up if you have solar.