mattsaxon / pysonofflan

Python interface for controlling Sonoff smart switches/plugs running original Itead firmware, locally, in "LAN mode".
MIT License
32 stars 23 forks source link

Reading from the POW R2 #94

Closed CarlosGS closed 4 years ago

CarlosGS commented 4 years ago

Hi, I searched for this but couldn't find anything related with the official API. Sorry if I missed something.

Please let me know if there has been any work in reading power (P, I & V) from the POW R2, or if you have any tips or clues to tackle the problem.

Thank you!

CarlosGS commented 4 years ago

OK I see the values are already there so it shouldn't be hard to get them: 2020-04-23 16:16:08,817 - debug: enter handle_message() b'{"alarmVValue":[-1,260],"alarmCValue":[-1,4],"alarmPValue":[-1,800],"switch":"on","startup":"stay","pulse":"off","pulseWidth":500,"sledOnline":"on","power":151.47,"voltage":239.85,"current":0.66}'

CarlosGS commented 4 years ago

Ah, so these only get updated while the app is open. Now to figure out what is the right command to request the measurements.

CarlosGS commented 4 years ago

I tried a bit more but have almost decided to go for the Tasmota MQTT route :)

mattsaxon commented 4 years ago

Why are you trying exactly, and what are you trying to do?

You are in the right area, but you’ll need some python skills to pull off taking those values that you can see in the debug log and making them available via either the command line or the API.

mattsaxon commented 4 years ago

The command to get status, you will find in the Itead documents on github I expect. If I recall it is a rest endpoint at zeroconf/info or zeroconf/status.

What firmware version are you running? There is a newer version just out which may be relevant as it has improved the rest interface

mattsaxon commented 4 years ago

Also are you sure these are only updated when a command is issued? I had expected that anytime the value changes that a new mdns messaage would be sent? I don’t have any measuring devices so this was a pure guess based on the behaviour of the switch status, which broadcasts every change. Granted something like power, this Willis be a bit noisy!

mattsaxon commented 4 years ago

Have you sniffed the LAN to see what messages are being sent?

CarlosGS commented 4 years ago

Thanks for getting back! I saw the posted values with this command: pysonofflanr3 -l DEBUG --api_key XX --device_id XX listen With this & only when the meter is open within the ewelink app, you get messages every couple seconds with the updated values. If the app isn't open, you just get the last old value repeated on each query.

So I think it does broadcast every change - it's just internally the power/voltage/I are not being updated. There must be an external command to trigger this periodic update.

I've tried capturing traffic on my phone to see what the app sends, but I couldn't see any related LAN traffic (could see LAN comms when issuing ON/OFF commands though). What do you recommend to capture & decode the encrypted traffic?

The version is 3.4.0.

mattsaxon commented 4 years ago

The docs for the sonoff rest interface are here https://github.com/itead/Sonoff_Devices_DIY_Tools

Now the complexity here is that these docs don't strictly apply unless you are in DIY mode.

However they pretty much apply and my code works with both DIY and non DIY just with the encryption.

So hopefully the command in these docs called Get Device Info on http://[ip]:[port]/zeroconf/info may kick the device into updating the values.

That command works with both 3.4 and 3.5 firmware, but the latter 3.5 firmware has more info, so might be better (or worse!). You may not be able to download this yet for the POW R2, not sure. I don't have one to test any of this.

My suggestion is to try to invoke this command from a browser to see if it updates the value whlst pysonofflanr3 is running.

The encryption may be an issue, but lets try unencrypted first. If that doesn't work, you could reuse one of the encrypted payloads from one of the on/off calls (they don't do very much in terms of message authentication). You can just replay it from what you see in the debug message. Try it yourself and see if you can get it to switch on/off first so you know you are doing it right.

mattsaxon commented 4 years ago

If you can get this working in this very manual way, we can update the codebase to do it automatically!

CarlosGS commented 4 years ago

It works! You are right, it's possible to make it update the values. In fact I only have to replay the encripted "on" command and it seems to update the P/V/I values every time. Haven't tried with "status" or "info" yet but it looks very promising!!

CarlosGS commented 4 years ago

Thank you! :D

mattsaxon commented 4 years ago

OK, tell me how you get on with the other commands.

Can you tell me what your use case is. I use this component exclusively with Home Assistant, so typically you don't want to have to poll the devices each time. What is your setup?

CarlosGS commented 4 years ago

Yes I'm going to try to implement it, first I need to get more familiar with the library. The use case is: The POW R2 is measuring the output of a solar grid inverter. I also have a household power meter. The idea is to have a python script that reads both meters, and turns on/off other devices (mostly a water heater element) to better use the power excess :)

mattsaxon commented 4 years ago

OK, should work. A couple of things to consider.

First the CLI is really there just for testing, when you want a reliable/performant setup you really want a permanent link to the sonoff. With the CLI, each time you run it, it has to find the device. I suggest, at least considering, using the lower down classes, for example SonoffSwitch().

Secondly, I'd be interested to know, given the fact that you look like you are into home automation, why you are not using an home automation orchestrator, like home assistant or OpenHAB. As I say, your setup should work fine, but I'm interested why you have chosen to not use such a setup.

Thanks, Matt

CarlosGS commented 4 years ago

Yes the idea is to use the lower classes of the library.

I was looking for ways to use these devices without modifying the original firmware, but you are right, OpenHAB would probably be the best choice.

mattsaxon commented 4 years ago

Well if you use Home Assistant, you can use my component within it. Then we just need to expose the values through the existing API.

Note there is an issue in by home Assistant repo that is there to track this enhancement already if I recall.

CarlosGS commented 4 years ago

As far as I understand, commands are sent with plain HTTP POSTs where the json field "data" is encrypted. But when using encryption, the answers are always the same i.e. {"seq":161,"sequence":"1588272873900","error":0}. The actual data is sent through the zeroconf service. This complicates things more than I'd like, so I'm finally going to go with a custom firmware + some home manager.

So far I only got it to update the power/current/voltage values when sending a POST to /zeroconf/switch and data {} (all with encryption, otherwise the device won't respond).

It also did not update when posting to /zeroconf/info, but maybe that's related to the "subscription" of zeroconf (i.e. if you somehow "subscribed" to /info, the updates would reach ?).

Thanks again for your kind help and I hope you can connect it with home assistant at some point! mattsaxon/sonoff-lan-mode-homeassistant#13

mattsaxon commented 4 years ago

Thanks for the info. I understand your position. There is no way of subscribing to those endpoints apart fro using zeroconf. There chat about this on the Itead forum and a number of people aren’t happy with this.

CarlosGS commented 4 years ago

Don't get me wrong, it's nice and secure this way. But you are right and in my case it makes more sense to use a unified platform using a local server. Cheers! :)

mattsaxon commented 4 years ago

@CarlosGS can I suggest you check out this component https://github.com/AlexxIT/SonoffLAN/

Seems to support all the things we want, will be testing today and if it works, I will be stopping supporting this component shortly and instead contributing to AlexxITs project.

Though I do note that he's not planning to deal with the refresh problem you've identified https://github.com/AlexxIT/SonoffLAN/issues/14

CarlosGS commented 4 years ago

Oh! Thanks! :D

AlexxIT commented 4 years ago

@CarlosGS I don't have Pow too. You can install my component, enable debug logs, and send info command to device: download-1 If it forces update, I can add ping logic.

CarlosGS commented 4 years ago

Hi Alex! I tried with /info and it did not trigger an update. However it did update when sending /switch with empty data {}. That should be enough for ping logic and you can reuse the existing code :)

Sorry at the moment I can't test it in your component (already looking into Tasmota). I'll ping back if I decide to setup Home Assistant instead of OpenHab. There's just too many options x)

CarlosGS commented 4 years ago

Hi Alex, I'm finally using Home Assistant + ESPhome. It is awesome! :D

So I was in the process of testing what you requested, but found HACS a bit too cumbersome. For now I don't want to go too far from a default HA installation - Sorry! I do hope you can find more people to test this.

@mattsaxon I really appreciate you suggesting Home Assistant, the interface is truly polished and really straightforward to set up. Also I didn't know it would support my AC unit which is a nice bonus :)

Thanks both for the support!!

mattsaxon commented 4 years ago

I'm stopping maintaining this component, instead I'd recommend using https://github.com/AlexxIT/SonoffLAN . It much more functional and reliable, so any contributions I make in future will be to that repo. I suggest you try that component first and if there are issues, raise an issue/feature request there.

CarlosGS commented 4 years ago

Awesome! And thanks for your help! :)

El sáb., 30 may. 2020 11:36, mattsaxon notifications@github.com escribió:

I'm stopping maintaining this component, instead I'd recommend using https://github.com/AlexxIT/SonoffLAN . It much more functional and reliable, so any contributions I make in future will be to that repo. I suggest you try that component first and if there are issues, raise an issue/feature request there.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mattsaxon/pysonofflan/issues/94#issuecomment-636305744, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAMPPKQBZON5YRLFBZOQUU3RUDHQNANCNFSM4MOL4SGQ .