Skip to content

Commit

Permalink
move from nut2 to pynutclient
Browse files Browse the repository at this point in the history
  • Loading branch information
thurask committed May 25, 2024
1 parent 9998f96 commit 40c3f5d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 12 additions & 6 deletions octoprint_ups/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import time
import threading
from flask import make_response, jsonify
import nut2
import PyNUTClient.PyNUT as nut2

try:
from octoprint.access.permissions import Permissions
Expand Down Expand Up @@ -87,6 +87,7 @@ def check_connection(self):
try:
self.ups = self.connect(self.config["host"], self.config["port"], self.config["auth"], self.config["username"], self.config["password"])
self.ups.ver()
self.ups.DeviceLogin(self.config["ups"])
self._logger.info("Connected!")
return True
except Exception:
Expand Down Expand Up @@ -129,7 +130,7 @@ def _loop(self):
logged_not_connected = False

try:
vars = self.ups.list_vars(ups=self.config['ups'])
vars = self.ups.GetUPSVars(ups=self.config['ups'])
except nut2.PyNUTError as e:
msg = str(e)
if msg == "ERR DATA-STALE":
Expand All @@ -148,6 +149,11 @@ def _loop(self):
self._logger.exception("An exception occurred while getting vars info")
continue

# bytes are annoying
_vars = {key.decode('utf-8'): val.decode('utf-8') for key, val in vars.items()}
vars = _vars
del _vars

self._logger.debug(vars)

status_flags = vars.get('ups.status', "").split(" ")
Expand Down Expand Up @@ -233,8 +239,8 @@ def on_api_command(self, command, data):
try:
ups = self.connect(host=str(data['host']), port=int(data['port']),
auth=data["auth"], username=data["username"], password=data["password"])
res = ups.list_ups()
return jsonify(result=list(res.keys()))
res = ups.GetUPSNames()
return jsonify(result=res)
except:
return make_response("Error getting UPS list", 500)

Expand Down Expand Up @@ -275,7 +281,7 @@ def get_assets(self):
"js": ["js/ups.js"],
"less": ["less/ups.less"],
"css": ["css/ups.min.css"]
}
}


def get_update_information(self):
Expand Down Expand Up @@ -312,4 +318,4 @@ def __plugin_load__():
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information,
"octoprint.events.register_custom_events": __plugin_implementation__._hook_events_register_custom_events,
"octoprint.comm.protocol.scripts": __plugin_implementation__._hook_comm_protocol_scripts
}
}
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
OctoPrint
nut2
PyNUTClient

0 comments on commit 40c3f5d

Please sign in to comment.