David00 / rpi-power-monitor

Power Monitor (for Raspberry Pi)
https://david00.github.io/rpi-power-monitor/
GNU General Public License v3.0
1.01k stars 103 forks source link

MQTT Plugin #104

Closed jmadden91 closed 1 year ago

jmadden91 commented 1 year ago

Sorry for the muck around. New to the github workflow. Here is a PR for the mqtt based on the develop/v0.3.0-plugins branch for you to consider.

Let me know if anything needs to be changed.

Cheers

David00 commented 1 year ago

Hey @jmadden91, no problem! Git is tricky :)

This looks so much better - thanks! Just a couple changes would be to delete the .pyc files and the .gitignore - both of those generally shouldn't be included in the normal git workflow / version control. Here's how I would proceed:

  1. Delete your local __pycache__ folder: rm -r rpi_power_monitor/__pycache__/
  2. Remove your local .gitignore file from git, but keep it locally: git rm --cached .gitignore

(Both of those commands from the top level project folder)

Then, you can do git add rpi_power_monitor/__pycache__ to stage the changes for commit, followed by a commit amend, and finally a force push to overwrite your last commit. (Generally force pushing is not good but it's perfectly fine in this case)

git commit --amend
# (Maybe change the message to read something like "Add MQTT plugin"?)
git push --force

This should take care of removing the un-needed files, and updating the commit message to summarize the work you did.

jmadden91 commented 1 year ago

Thanks mate, I think that should have sorted it. Cheers

David00 commented 1 year ago

Thanks mate, I think that should have sorted it. Cheers

Almost! :)

The .gitignore was gone, but the .pyc files in the pycache directory were still there. I removed them and amended your commit. Since I force pushed the amended commit, your local copy is now out of sync, so you'll need to reset your local copy with:

(Edit: I think this will get rid of any non-git tracked files, like your .gitignore, so use caution!)

git reset --hard origin/develop/v0.3.0-plugins

jmadden91 commented 1 year ago

Thanks mate. I've updated the error handling a bit so it can recover if the mqtt broker disconnects.

David00 commented 1 year ago

Hey @jmadden91, I'm getting ready to merge this PR into master now. Somehow, your local __pycache__/ folder keeps getting added to the source. I just force-pushed to remove it, so you should probably do a git reset --hard origin/develop/v0.3.0-plugins.

Then, to make sure pycache stays out, you can setup a .gitignore file in the base project directory. Assuming ~/rpi-power-monitor is your root project folder:

echo "__pycache__/" >> ~/rpi-power-monitor/.gitignore echo ".gitignore" >> ~/rpi-power-monitor/.gitignore

David00 commented 1 year ago

@jmadden91, I figured it would be a good idea for plugin authors to write their own documentation for their plugin, so I just created a template (and example) for my GPIO Controller example plugin. Can you fill out the template and add it to your mqtt/ plugin folder?

David00 commented 1 year ago

Hey @jmadden91, I just created the README for your plugin so that we can get it added to the list of plugins. Can you review it here and see if there's anything that you want to add or modify?

https://github.com/David00/rpi-power-monitor/blob/develop/v0.3.0-plugins/rpi_power_monitor/plugins/mqtt/README.md

Once it is good to go, I'll add it to the plugin list in the docs.

jmadden91 commented 1 year ago

Hey mate, sorry I got this working on my system and promptly stopped checking in here.. Yes that sounds fine. Its been working perfectly for the last few months.

David00 commented 8 months ago

FYI, the paho-mqtt library has a new major release which introduces breaking changes to any current implementations. See https://eclipse.dev/paho/files/paho.mqtt.python/html/migrations.html for details and a workaround.

For now, I've updated the MQTT plugin README in 7dc78e5dc553fcd1dbc17686592c076598edd004 to specify installing version 1.6.1 until the plugin can be updated to use version 2.x.

5ft24dave commented 1 month ago

Jmadden91, How do I install the mqtt.py plugin? Thanks

David00 commented 1 month ago

Hey @5ft24dave, if your power monitor is relatively up to date with the current master branch, then you'll already have the required files needed. If you see the mqtt.py file inside ~/rpi_power_monitor/rpi_power_monitor/plugins/mqtt/, then you're all set. All that's needed is to enable the plugin via the power monitor's config.toml Configuration file according to the plugin's README.

Just update the following text with the details from your MQTT system and drop it into config.toml:

[plugins.mqtt]
    enabled = true
    host = "192.168.x.x"
    username = "username"
    password = "password"
    prefix = "powermon"
    refresh = 5 # Refresh rate in seconds

... and then restart the power monitor with sudo systemctl restart power-monitor.service.

Also note - there is a MQTT version 2 plugin: https://david00.github.io/rpi-power-monitor/docs/v0.3.0/plugin-list.html#3-mqtt-v2

5ft24dave commented 1 month ago

Ah. I was still pointed at the dev v0.3.0 branch. pointed to master, all is there. HA is seeing data, just need to set up the sensors and I'll be good.