Fixing connect/disconnect and adding UI elements - connect/disconnect buttons and status indicator#155
Fixing connect/disconnect and adding UI elements - connect/disconnect buttons and status indicator#155mspnr wants to merge 7 commits into
Conversation
…add self._mqtt.disconnect() call
- Filter out None/empty topics before subscribing - Add exception handling in _on_mqtt_connect() callback
- Add Connect and Disconnect buttons in MQTT settings - Add connection status indicator (Connected/Disconnected) - Implement SimpleApiPlugin with admin-only endpoints: - POST /api/plugin/mqtt command=connect - POST /api/plugin/mqtt command=disconnect - GET /api/plugin/mqtt (returns connection status) - Add is_api_protected() and is_api_adminonly() for API security
jneilliii
left a comment
There was a problem hiding this comment.
Thanks for your contribution, just a couple of code comments.
…ailability
- Update setup.py: change "paho-mqtt<2" to "paho-mqtt>=1.4.0,<2"
- Remove unnecessary exception handling in on_api_get()
- is_connected() is now guaranteed to be available
- Simplify code by removing try/except fallback logic
- Replace bare exception handling with explicit is_connected() checks
- mqtt_disconnect(): check connection before calling disconnect()
- Add connection check before publishing Last Will Testament
- Eliminates bare except: clauses that could hide unexpected errors
|
@jneilliii added 200d681 according to your suggestions: Changes MadeThank you for the review feedback! I've addressed both suggestions: 1. Dependency ManagementPinned minimum paho-mqtt version to 1.4.0 (when
2. Proper Use of
|
|
@Kunsi I don't see any other code issues related to this PR, but haven't tested yet. I feel like there could be some changes to how the connect/disconnect buttons and connection status area are handled to utilize ko bindings instead of DOM manipulation via js, but technically should still work as-is in this PR. |
- Replace jQuery DOM manipulation with ko.observables
- Add computed observables for connection status and button states
- Update template to use data-bind attributes instead of IDs
- Remove all $("#...") selectors from mqtt.js
Testing and bonus effectsEvery time before pushing commits I test the function on my instance of OctoPrint. Moreover - after making these changes - OctoPrint keeps connection even after restart of Home Assistant, issue described here #151 DOM manipulation vs ko bindingsAs I understood you have concern about this commit 2a74c82 . Fair enough - I improved the situation here: 70514e3 I've refactored the connect/disconnect buttons and connection status to use ko.observables and computed observables
The UI now follows OctoPrint's standard ViewModel patterns and is more maintainable. All functionality remains the |
|
I made some adjustments to AI's disable button observables to include isConnected() checks in mspnr@b3802b8. One last issue I notice is that the connection state isn't getting updated in the front-end when the back-end loses the connection to the broker. To test this yourself, be in a connected state and stop your MQTT broker. The plugin will lose it's connection but the UI still thinks it's connected. Same for when you start the broker back up and plugin is in disconnected state.
Probably need to add a self._plugin_manager.send_plugin_message(self._identifier, {"connected": False}) to _on_mqtt_disconnect function and |
|
Thank you for improving buttons behavior! I added sync MQTT connection status between backend and frontend and also tested that. It is working just fine. Actually pressing connect is not needed anymore, because my tests showed, that OctoPrint always connected back to the broker as soon as it became available again. I would keep Connect/Disconnect buttons for debugging or testing edge cases in configuration. |

This PR addresses the issue where OctoPrint-MQTT fails to reconnect after the MQTT broker (Home Assistant) restarts. See #151
Note: It was also requested to merge with devel branch, but it is 14 commit behind master, so I am not sure it makes sense.
Bug Fixes
1. Fixed MQTT disconnect/reconnect logic
mqtt_disconnect()method never calledself._mqtt.disconnect(), so the broker didn't receive aproper disconnect packet. This caused reconnection attempts to fail because the broker thought the client was still
connected.
self._mqtt.disconnect()call before stopping the network loop2. Fixed invalid topic subscription error
entries had invalid/empty topic values, causing a
ValueError: Invalid topicexception. This prevented_mqtt_connectedfrom being set toTrue._on_mqtt_connect()to log errors without breaking the connectionNew Features
Added manual Connect/Disconnect controls in settings UI
SimpleApiPluginwith admin-only API endpoints:POST /api/plugin/mqttwith commandconnect- initiates connectionPOST /api/plugin/mqttwith commanddisconnect- disconnects from brokerGET /api/plugin/mqtt- returns current connection statusis_connected()from Paho MQTT client for accurate real-time statusBenefits
Screenshots