New plugin: MQTT for PSUControl

Hello people,

I created a plugin to control the PSUControl plugin via MQTT or specifically with HomeAssistant.
As far as I can tell it works pretty reliable. More information you find on github.

I plan on releasing it in the repository some time later. But since this is my first plugin I haven't figured everything out yet.

One question I have is: Is there a way to test the update function without the plugin being registered in the repo?

Feedback is appreciated.

1 Like

The updates happen directly with github, so if you publish a release on github with tag that matches the version number in setup.py and install it, then update setup.py version number and release a new version you can test that.

1 Like

Thank you! I will keep testing it then. Didn't work when I first tried.

you may have to use the option to force an update check, because those responses are cached locally.

Neat! I don't currently use MQTT but it's cool to provide support for it. If you push to the plugin repo be sure to add a psucontrol tag to it.

1 Like

Thanks guys!

The updater works now and thus the current version on github should be good enough for a first release. Planning to do that next week.

@jneilliii you seem to be familiar with mqtt plugins..Maybe you can help me out with another problem?

In my on_settings_save function (line 146) I want to unsubscribe and subscribe again to keep everything in sync with the settings. The mqtt plugin throws an error though.

File "/octoprint/plugins/lib/python3.8/site-packages/octoprint_mqtt/init.py", line 382, in mqtt_subscribe
self._mqtt_subscriptions.append((topic, callback, args, kwargs))
AttributeError: 'filter' object has no attribute 'append'

The way it is the error shows up if you do more than 1 change/save.
If I change the order to usubscrbe before subscribe the error comes up on save instantly.

So right now I don't unsubscribe the old topic which can result in unwanted behaviour until a restart happens.

Any ideas?

If I had to take a wild guess from just looking I'm going to bet many plugins don't call mqtt_unsubscribe and there's a bug.

https://github.com/OctoPrint/OctoPrint-MQTT/blob/08edf0d4c1aa617c2073e2fe233ae8b38a2165eb/octoprint_mqtt/init.py#L396 has self._mqtt_subscriptions being set to a filter object which should perhaps be converted to a list.

1 Like

In Python 3, filter() returns an iterable object whereas in Python 2 it returned a list. Likely where the error comes from.

2 Likes

Yeah, probably a bug upstream. I haven't been told of an issue, but I use that here.

1 Like

I have opened an issue on the MQTT plugin repo. If you could share some logs on it here that would be helpful.

Try with my patched MQTT plugin @oerkel47. Install by copying/pasting the URL below in Plugin Manager > Get More > ...from URL and click install.

https://github.com/OctoPrint/OctoPrint-MQTT/archive/refs/heads/bugfix/unsubscribe.zip

If that works I'll merge in the changes to MQTT.

1 Like

Thanks everyone!

@jneilliii works like a charm with your modification. Awesome!

Cool, thanks for confirming. I'll merge the change to the repo and release a new version.

I am working on putting it on the repo atm and have one more question:
In the <your plugin's identifier>.md can I also specify the compatibility with other plugins with their identifier? In this case it would only work with mqtt >= 0.8.10 for example.

Not currently. You can grab the plugin version though doing something like this

mqtt_version = None
for k, v in self._plugin_manager.plugin_implementations.items():
    if k == 'mqtt':
        mqtt_version = k._plugin_version

if mqtt_version:
    print("MQTT Version: {}".format(mqtt_version))
else:
    print("MQTT not found!")

Haven't tested it but think that would work...

1 Like

Thanks! I decided to just place some little info to use the newest version of mqtt. People who install new plugins will probably keep their plugins up to date anyway.

Hi.
I installed PSU Control and MQTT for PSU Control and it seems that it should do what I want by sending MQTT commands directly without HomeAssistant or other home automation products but in PSU Control, I can't select "MQTT for PSU Control" but a lot of other plug-ins. Am I missing something or is it just a missing link here?
Svein.....

Someone else had this question on Discord recently. This plugin is for controlling PSU control using MQTT, not PSU Control sending commands to a smart socket over MQTT. The opposite direction to what you are trying to do. For example, you could configure PSU control as a device in Home Assistant to control it.

If you are using a device with Tasmota, there's dedicated plugins or a PSU control plugin for that that you can use instead (they're the most common MQTT plugs afaik).

Thanks for fast reply.

I want to control any ordinary smart socket via MQTT. I have several other options to do that but it would be nice to be able to do it from Octoprint UI.

What's the smart socket running (firmware-wise) specifically?

Since it varies by plug/manufacturer/firmware, as far as I understand it is difficult to create a generic one.