diff --git a/docs/widgets/(Widget)-Libre-HW-Monitor.md b/docs/widgets/(Widget)-Libre-HW-Monitor.md index 51829ab..0b8ae2c 100644 --- a/docs/widgets/(Widget)-Libre-HW-Monitor.md +++ b/docs/widgets/(Widget)-Libre-HW-Monitor.md @@ -1,23 +1,26 @@ # Libre Hardware Monitor Widget Configuration -| Option | Type | Default | Description | -|-------------------------|---------|------------------------------------------------------------------------------------------------|----------------------------------------------------------------------| -| `label` | string | `"\udb82\udcae {info[value]}{info[unit]}"` | The primary label format. | -| `label_alt` | string | `"\uf4bc {info[histogram]} {info[value]} ({info[min]}/{info[max]}) {info[unit]}"` | Histograms. The alternative label format. | -| `sensor_id` | string | `"/amdcpu/0/load/0"` | Libre Hardware Monitor SensorId from http://localhost:8085/data.json | -| `class_name` | string | `"libre-monitor-widget"` | CSS class name for styling of different widget instances. | -| `update_interval` | integer | `1000` | The interval in milliseconds to update the widget. | -| `precision` | integer | `1` | Floating point precision of the info[value]. | -| `history_size` | integer | `60` | The size of the min/max history. | -| `histogram_num_columns` | integer | `10` | The number of columns in the histogram. | -| `histogram_fixed_min` | integer | `None` | Histogram minimum value. If None - set as history minimum value. | -| `histogram_fixed_max` | integer | `None` | Histogram maximum value. If None - set as history maximum value. | -| `server_host` | string | `"localhost"` | Libre Hardware Monitor server host. | -| `server_port` | integer | `8085` | Libre Hardware Monitor server port. | -| `server_username` | string | `""` | Libre Hardware Monitor username. Only needed if auth is enabled. | -| `server_password` | string | `""` | Libre Hardware Monitor password. Only needed if auth is enabled. | -| `histogram_icons` | list | `['\u2581', '\u2581', '\u2582', '\u2583', '\u2584', '\u2585', '\u2586', '\u2587', '\u2588']` | Icons representing CPU usage histograms. | -| `callbacks` | dict | `{'on_left': 'toggle_label', 'on_middle': 'do_nothing', 'on_right': 'do_nothing'}` | Callback functions for different mouse button actions. | +| Option | Type | Default | Description | +|--------------------------|---------|------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------| +| `label` | string | `"\udb82\udcae {info[value]}{info[unit]}"` | The primary label format. | +| `label_alt` | string | `"\uf4bc {info[histogram]} {info[value]} ({info[min]}/{info[max]}) {info[unit]}"` | Histograms. The alternative label format. | +| `sensor_id` | string | `"/amdcpu/0/load/0"` | Libre Hardware Monitor SensorId from http://localhost:8085/data.json | +| `class_name` | string | `"libre-monitor-widget"` | CSS class name for styling of different widget instances. | +| `update_interval` | integer | `1000` | The interval in milliseconds to update the widget. | +| `precision` | integer | `2` | Floating point precision of the info[value]. | +| `history_size` | integer | `60` | The size of the min/max history. | +| `histogram_num_columns` | integer | `10` | The number of columns in the histogram. | +| `histogram_fixed_min` | integer | `None` | Histogram minimum value. If None - set as history minimum value. | +| `histogram_fixed_max` | integer | `None` | Histogram maximum value. If None - set as history maximum value. | +| `sensor_id_error_label` | string | `N/A` | The label shown when the sensor id is invalid or the sensor does not exist/disabled. | +| `connection_error_label` | string | `Connection error...` | The label shown when YASB can't connect to the Libre Hardware Monitor Web server. Either the server is not running or the IP/port is wrong. | +| `auth_error_label` | string | `Auth Failed...` | The label shown when there is a username/password issue while connecting to LHM Web server if the authentication is enabled in LHM settings. | +| `server_host` | string | `"localhost"` | Libre Hardware Monitor server host. | +| `server_port` | integer | `8085` | Libre Hardware Monitor server port. | +| `server_username` | string | `""` | Libre Hardware Monitor username. Only needed if auth is enabled. | +| `server_password` | string | `""` | Libre Hardware Monitor password. Only needed if auth is enabled. | +| `histogram_icons` | list | `['\u2581', '\u2581', '\u2582', '\u2583', '\u2584', '\u2585', '\u2586', '\u2587', '\u2588']` | Icons representing CPU usage histograms. | +| `callbacks` | dict | `{'on_left': 'toggle_label', 'on_middle': 'do_nothing', 'on_right': 'do_nothing'}` | Callback functions for different mouse button actions. | ## Example Configuration (GPU Temperature) @@ -29,7 +32,7 @@ label_alt: "\uf437 {info[histogram]} {info[value]} ({info[min]}/{info[max]}) {info[unit]}" sensor_id: "/gpu-nvidia/0/temperature/0" update_interval: 1000 - precision: 1 + precision: 2 histogram_num_columns: 10 class_name: "libre-monitor-widget" @@ -80,6 +83,9 @@ - **histogram_fixed_max**: Set the fixed maximum value of the histogram. Actual sensor max value from the history is not changed. If not set manually it will be set as history maximum value. - **histogram_icons**: A list of icons representing different values of the histogram. - **histogram_num_columns**: The number of columns to display in the histogram. +- **sensor_id_error_label**: The label shown when the sensor id is invalid or the sensor does not exist/disabled. +- **connection_error_label**: The label shown when YASB can't connect to the Libre Hardware Monitor Web server. Either the server is not running or the IP/port is wrong. +- **auth_error_label**: The label shown when there is a username/password issue while connecting to LHM Web server if the authentication is enabled in LHM settings. - **server_host**: The host of the Libre Hardware Monitor server. - **server_port**: The port of the Libre Hardware Monitor server. - **server_username**: The username of the Libre Hardware Monitor server. Required if auth is enabled. diff --git a/src/core/validation/widgets/yasb/libre_monitor.py b/src/core/validation/widgets/yasb/libre_monitor.py index 12fb36f..440dd12 100644 --- a/src/core/validation/widgets/yasb/libre_monitor.py +++ b/src/core/validation/widgets/yasb/libre_monitor.py @@ -20,6 +20,9 @@ 'history_size': 60, 'histogram_fixed_min': None, 'histogram_fixed_max': None, + 'sensor_id_error_label': "N/A", + 'connection_error_label': "Connection error...", + 'auth_error_label': "Auth Failed...", 'server_host': 'localhost', 'server_port': 8085, 'server_username': '', @@ -100,6 +103,21 @@ 'required': False, 'nullable': True }, + 'sensor_id_error_label': { + 'type': 'string', + 'default': DEFAULTS['sensor_id_error_label'], + 'required': False, + }, + 'connection_error_label': { + 'type': 'string', + 'default': DEFAULTS['connection_error_label'], + 'required': False, + }, + 'auth_error_label': { + 'type': 'string', + 'default': DEFAULTS['auth_error_label'], + 'required': False, + }, 'server_host': { 'type': 'string', 'default': DEFAULTS['server_host'], diff --git a/src/core/widgets/yasb/libre_monitor.py b/src/core/widgets/yasb/libre_monitor.py index 9a9c520..a5cebb2 100644 --- a/src/core/widgets/yasb/libre_monitor.py +++ b/src/core/widgets/yasb/libre_monitor.py @@ -26,6 +26,9 @@ def __init__( history_size: int, histogram_fixed_min: float | None, histogram_fixed_max: float | None, + sensor_id_error_label, + connection_error_label, + auth_error_label, server_host: str, server_port: int, server_username: str, @@ -42,6 +45,9 @@ def __init__( self._history_long: deque[float] = deque([], maxlen=history_size) self._histogram_fixed_min = histogram_fixed_min self._histogram_fixed_max = histogram_fixed_max + self._sensor_id_error_label = sensor_id_error_label + self._connection_error_label = connection_error_label + self._auth_error_label = auth_error_label self._histogram_icons = histogram_icons self._histogram_num_columns = histogram_num_columns self._server_host = server_host @@ -72,6 +78,14 @@ def __init__( # Called if the server requests authentication self._network_manager.authenticationRequired.connect(self._handle_authentication) + # Create a request + url = QUrl(f"http://{self._server_host}:{self._server_port}/Sensor?action=Get&id={self._sensor_id}") + self.request = QNetworkRequest(url) + self.request.setHeader( + QNetworkRequest.KnownHeaders.ContentTypeHeader, + "application/x-www-form-urlencoded", + ) + # Callbacks self.callback_left = callbacks["on_left"] self.callback_right = callbacks["on_right"] @@ -132,12 +146,12 @@ def _update_label(self): """Make a request and update the label with the received data""" self._make_request() info = { - "status": "No Connection...", - "value": 0.0, + "status": "", + "value": "", "unit": "", - "min": 0.0, - "max": 0.0, - "histogram": "No Connection..." + "min": "", + "max": "", + "histogram": "", } if self._data and self._data.get("result") == "ok": value = self._data.get("value", 0.0) @@ -179,10 +193,7 @@ def _update_label(self): def _make_request(self): """Makes a post request to LibreHardwareMonitor""" - url = QUrl(f"http://{self._server_host}:{self._server_port}/Sensor?action=Get&id={self._sensor_id}") - request = QNetworkRequest(url) - request.setHeader(QNetworkRequest.KnownHeaders.ContentTypeHeader, "application/x-www-form-urlencoded") - self._network_manager.post(request, b"") + self._network_manager.post(self.request, b"") def _handle_network_response(self, reply: QNetworkReply): """ @@ -195,18 +206,21 @@ def _handle_network_response(self, reply: QNetworkReply): if self._data.get("result") == "ok": self._data["status"] = "Connected..." else: - self._data["status"] = "Invalid sensor id..." + self._data["status"] = self._sensor_id_error_label + self._data["histogram"] = self._sensor_id_error_label elif reply.error() == QNetworkReply.NetworkError.AuthenticationRequiredError: self._data = { - "status": "Authentication Failed...", + "status": self._auth_error_label, "result": "fail", - "value": 0.0, + "value": "", + "histogram": self._auth_error_label, } else: self._data = { - "status": "Connection Error...", + "status": self._connection_error_label, "result": "fail", - "value": 0.0, + "value": "", + "histogram": self._auth_error_label, } reply.deleteLater()