edekeijzer / OctoPrint-PSUControl-Shelly

GNU Affero General Public License v3.0
10 stars 6 forks source link

Add support for the second generation "Shelly plus" devices. #16

Open DanForever opened 1 year ago

DanForever commented 1 year ago

The api (https://shelly-api-docs.shelly.cloud/gen2/) is marginally different as is the authentication protocol (digest vs basic)

edekeijzer commented 1 year ago

@DanForever unfortunately I do not have any Shelly NG devices (with Shelly firmware) laying around to test with. I've simply added a checkbox in the settings and use Digest auth when it's checked. Could you install this link and check if it works?

DanForever commented 1 year ago

This is something I'll be able to test later on once I get octoprint up and running.

I was just doing some research on how I will be able to turn off the printer automatically with octoprint in the future and found this plugin.

I do know that simply using digest won't be enough, as the REST api paths are slightly different (I've actually got a pull request in another repo for a similar update https://github.com/geerlingguy/shelly-plug-prometheus/pull/6): for example, the url for gen 1 might look like http://hostname/meter/0 but for gen 2 it's http://hostname/rpc/Switch.GetStatus?id=0

DanForever commented 1 year ago

Also the username is always "admin" for gen 2 devices with digest auth (makes sense considering they don't really need to support multiple users)

numo68 commented 1 year ago

Hi, was anything done here already? If not, I have plus devices and might be able to test or even implement (provided it is not too complicated to setup an environment, I never did anything for OctoPrint).

Another feature I would like to add is running a script inside the shelly device instead of controlling the relay directly, and also allowing to call it at the shutdown. What I want to achieve is hooking both the printer and the controlling raspberry behind the shelly. For that use case I need to wait a reasonable time so the operating system can shut down properly, then turn off the power to the whole setup (printer, enclosure with some lights and stuff, and the RPi). Calling a script would allow for such delay.

edekeijzer commented 1 year ago

Hi, was anything done here already? If not, I have plus devices and might be able to test or even implement (provided it is not too complicated to setup an environment, I never did anything for OctoPrint). You could try installing this: https://github.com/edekeijzer/OctoPrint-PSUControl-Shelly/archive/refs/heads/shelly-ng.zip which should use the Shelly NG authentication method, but without a Plus device to test with, I can only guess that the old API endpoints will still work.

Another feature I would like to add is running a script inside the shelly device instead of controlling the relay directly, and also allowing to call it at the shutdown. What I want to achieve is hooking both the printer and the controlling raspberry behind the shelly. For that use case I need to wait a reasonable time so the operating system can shut down properly, then turn off the power to the whole setup (printer, enclosure with some lights and stuff, and the RPi). Calling a script would allow for such delay. This would reach far beyond the original purpose of this plugin and the PSU Control plugin in general, as this would never allow you to use PSU Control to power on your setup. In this case, I'd personally try to achieve this by writing a shell script that will be executed as shutdown command from Octoprint. Call Shelly API, then shutdown the Pi.

x2on commented 1 year ago

Second gen Shelly Plus Plug S works here with the current plugin, but only without password.

x2on commented 1 year ago

I tested the NG build - works fine with authentication and 2nd device.

d-sko commented 1 year ago

I'm using the NG build with a Shelly Plus 1 PM, it works fine, thanks. One suggestion for convenience: as the username is always admin on the 2nd gen Shellys (https://shelly-api-docs.shelly.cloud/gen2/General/Authentication) it should be set by default when checking Shelly Next-gen (Pro/Plus) device or the username field should be hidden when the option is checked.

mitch0405 commented 1 year ago

Should the cloud work on this new ng build? I have the new shelly plus plug s. I tried local with http://192.168.178.135/, it does go on with a delay but sometimes does not go off and also with a delay. I use admin and my cloud password Weird thing is that when I put the links in my browser, it always works and very fast for on and off; http://192.168.178.135/relay/0?turn=on http://192.168.178.135/relay/0?turn=off

For the cloud, I can acces the url but I cannot use turn=on tag https://shelly-54-eu.shelly.cloud/device/status?id=Removed&auth_key=Removed

I have everything set on shelly plugin in the psu settings. CloudErrors.txt

Edit: I got cloud working via webhooks plugin, using Post https://shelly-54-eu.shelly.cloud/device/relay/control/channel=0&turn=on&id=x&auth_key=x

mitch0405 commented 1 year ago

So, after couple of hours digging in your code, I found a solution for my problem with the new gen2 plug and the cloud. I saw in the log that the sensing part was causing the delay. In the python file init.py I changed the following code after some testing: Under def get_psu_state(self): From status = json_data['data']['device_status']['relays'][output]['ison'] to status = json_data['data']['device_status']['switch:0']['output']

I uploaded the edited python file and now cloud works with my plus plug. Also polling on 10 sec to not get max request limit.

luciangpl commented 1 year ago

Hi, do we have a solution for PlugS? Preferably, locally, I would like the printer to be stopped even if the cloud services are not available.

luciangpl commented 1 year ago

Hi, do we have a solution for PlugS? Preferably, locally, I would like the printer to be stopped even if the cloud services are not available.

LE - It works with the NG version above, I saw it later

Cimos commented 2 months ago

Can confirm NG version works with Shelly 1PM mini Gen3