Skip to content

Commit fabd249

Browse files
committed
feat(LHM): Customizable error labels for LibreHardwareMonitor Widget
Added customizable error labels (sensor id, connection and auth). Changed default error labels for sensor id and auth. Moved request creation to __init__. Updated docs.
1 parent 9dec1ea commit fabd249

File tree

3 files changed

+71
-33
lines changed

3 files changed

+71
-33
lines changed

docs/widgets/(Widget)-Libre-HW-Monitor.md

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
# Libre Hardware Monitor Widget Configuration
22

3-
| Option | Type | Default | Description |
4-
|-------------------------|---------|------------------------------------------------------------------------------------------------|----------------------------------------------------------------------|
5-
| `label` | string | `"<span>\udb82\udcae </span> {info[value]}{info[unit]}"` | The primary label format. |
6-
| `label_alt` | string | `"<span>\uf4bc </span>{info[histogram]} {info[value]} ({info[min]}/{info[max]}) {info[unit]}"` | Histograms. The alternative label format. |
7-
| `sensor_id` | string | `"/amdcpu/0/load/0"` | Libre Hardware Monitor SensorId from http://localhost:8085/data.json |
8-
| `class_name` | string | `"libre-monitor-widget"` | CSS class name for styling of different widget instances. |
9-
| `update_interval` | integer | `1000` | The interval in milliseconds to update the widget. |
10-
| `precision` | integer | `1` | Floating point precision of the info[value]. |
11-
| `history_size` | integer | `60` | The size of the min/max history. |
12-
| `histogram_num_columns` | integer | `10` | The number of columns in the histogram. |
13-
| `histogram_fixed_min` | integer | `None` | Histogram minimum value. If None - set as history minimum value. |
14-
| `histogram_fixed_max` | integer | `None` | Histogram maximum value. If None - set as history maximum value. |
15-
| `server_host` | string | `"localhost"` | Libre Hardware Monitor server host. |
16-
| `server_port` | integer | `8085` | Libre Hardware Monitor server port. |
17-
| `server_username` | string | `""` | Libre Hardware Monitor username. Only needed if auth is enabled. |
18-
| `server_password` | string | `""` | Libre Hardware Monitor password. Only needed if auth is enabled. |
19-
| `histogram_icons` | list | `['\u2581', '\u2581', '\u2582', '\u2583', '\u2584', '\u2585', '\u2586', '\u2587', '\u2588']` | Icons representing CPU usage histograms. |
20-
| `callbacks` | dict | `{'on_left': 'toggle_label', 'on_middle': 'do_nothing', 'on_right': 'do_nothing'}` | Callback functions for different mouse button actions. |
3+
| Option | Type | Default | Description |
4+
|--------------------------|---------|------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------|
5+
| `label` | string | `"<span>\udb82\udcae </span> {info[value]}{info[unit]}"` | The primary label format. |
6+
| `label_alt` | string | `"<span>\uf4bc </span>{info[histogram]} {info[value]} ({info[min]}/{info[max]}) {info[unit]}"` | Histograms. The alternative label format. |
7+
| `sensor_id` | string | `"/amdcpu/0/load/0"` | Libre Hardware Monitor SensorId from http://localhost:8085/data.json |
8+
| `class_name` | string | `"libre-monitor-widget"` | CSS class name for styling of different widget instances. |
9+
| `update_interval` | integer | `1000` | The interval in milliseconds to update the widget. |
10+
| `precision` | integer | `2` | Floating point precision of the info[value]. |
11+
| `history_size` | integer | `60` | The size of the min/max history. |
12+
| `histogram_num_columns` | integer | `10` | The number of columns in the histogram. |
13+
| `histogram_fixed_min` | integer | `None` | Histogram minimum value. If None - set as history minimum value. |
14+
| `histogram_fixed_max` | integer | `None` | Histogram maximum value. If None - set as history maximum value. |
15+
| `sensor_id_error_label` | string | `N/A` | The label shown when the sensor id is invalid or the sensor does not exist/disabled. |
16+
| `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. |
17+
| `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. |
18+
| `server_host` | string | `"localhost"` | Libre Hardware Monitor server host. |
19+
| `server_port` | integer | `8085` | Libre Hardware Monitor server port. |
20+
| `server_username` | string | `""` | Libre Hardware Monitor username. Only needed if auth is enabled. |
21+
| `server_password` | string | `""` | Libre Hardware Monitor password. Only needed if auth is enabled. |
22+
| `histogram_icons` | list | `['\u2581', '\u2581', '\u2582', '\u2583', '\u2584', '\u2585', '\u2586', '\u2587', '\u2588']` | Icons representing CPU usage histograms. |
23+
| `callbacks` | dict | `{'on_left': 'toggle_label', 'on_middle': 'do_nothing', 'on_right': 'do_nothing'}` | Callback functions for different mouse button actions. |
2124

2225
## Example Configuration (GPU Temperature)
2326

@@ -29,7 +32,7 @@
2932
label_alt: "<span>\uf437 </span>{info[histogram]} {info[value]} ({info[min]}/{info[max]}) {info[unit]}"
3033
sensor_id: "/gpu-nvidia/0/temperature/0"
3134
update_interval: 1000
32-
precision: 1
35+
precision: 2
3336
histogram_num_columns: 10
3437
class_name: "libre-monitor-widget"
3538

@@ -80,6 +83,9 @@
8083
- **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.
8184
- **histogram_icons**: A list of icons representing different values of the histogram.
8285
- **histogram_num_columns**: The number of columns to display in the histogram.
86+
- **sensor_id_error_label**: The label shown when the sensor id is invalid or the sensor does not exist/disabled.
87+
- **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.
88+
- **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.
8389
- **server_host**: The host of the Libre Hardware Monitor server.
8490
- **server_port**: The port of the Libre Hardware Monitor server.
8591
- **server_username**: The username of the Libre Hardware Monitor server. Required if auth is enabled.

src/core/validation/widgets/yasb/libre_monitor.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
'history_size': 60,
2121
'histogram_fixed_min': None,
2222
'histogram_fixed_max': None,
23+
'sensor_id_error_label': "N/A",
24+
'connection_error_label': "Connection error...",
25+
'auth_error_label': "Auth Failed...",
2326
'server_host': 'localhost',
2427
'server_port': 8085,
2528
'server_username': '',
@@ -100,6 +103,21 @@
100103
'required': False,
101104
'nullable': True
102105
},
106+
'sensor_id_error_label': {
107+
'type': 'string',
108+
'default': DEFAULTS['sensor_id_error_label'],
109+
'required': False,
110+
},
111+
'connection_error_label': {
112+
'type': 'string',
113+
'default': DEFAULTS['connection_error_label'],
114+
'required': False,
115+
},
116+
'auth_error_label': {
117+
'type': 'string',
118+
'default': DEFAULTS['auth_error_label'],
119+
'required': False,
120+
},
103121
'server_host': {
104122
'type': 'string',
105123
'default': DEFAULTS['server_host'],

src/core/widgets/yasb/libre_monitor.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ def __init__(
2626
history_size: int,
2727
histogram_fixed_min: float | None,
2828
histogram_fixed_max: float | None,
29+
sensor_id_error_label,
30+
connection_error_label,
31+
auth_error_label,
2932
server_host: str,
3033
server_port: int,
3134
server_username: str,
@@ -42,6 +45,9 @@ def __init__(
4245
self._history_long: deque[float] = deque([], maxlen=history_size)
4346
self._histogram_fixed_min = histogram_fixed_min
4447
self._histogram_fixed_max = histogram_fixed_max
48+
self._sensor_id_error_label = sensor_id_error_label
49+
self._connection_error_label = connection_error_label
50+
self._auth_error_label = auth_error_label
4551
self._histogram_icons = histogram_icons
4652
self._histogram_num_columns = histogram_num_columns
4753
self._server_host = server_host
@@ -72,6 +78,14 @@ def __init__(
7278
# Called if the server requests authentication
7379
self._network_manager.authenticationRequired.connect(self._handle_authentication)
7480

81+
# Create a request
82+
url = QUrl(f"http://{self._server_host}:{self._server_port}/Sensor?action=Get&id={self._sensor_id}")
83+
self.request = QNetworkRequest(url)
84+
self.request.setHeader(
85+
QNetworkRequest.KnownHeaders.ContentTypeHeader,
86+
"application/x-www-form-urlencoded",
87+
)
88+
7589
# Callbacks
7690
self.callback_left = callbacks["on_left"]
7791
self.callback_right = callbacks["on_right"]
@@ -132,12 +146,12 @@ def _update_label(self):
132146
"""Make a request and update the label with the received data"""
133147
self._make_request()
134148
info = {
135-
"status": "No Connection...",
136-
"value": 0.0,
149+
"status": "",
150+
"value": "",
137151
"unit": "",
138-
"min": 0.0,
139-
"max": 0.0,
140-
"histogram": "No Connection..."
152+
"min": "",
153+
"max": "",
154+
"histogram": "",
141155
}
142156
if self._data and self._data.get("result") == "ok":
143157
value = self._data.get("value", 0.0)
@@ -179,10 +193,7 @@ def _update_label(self):
179193

180194
def _make_request(self):
181195
"""Makes a post request to LibreHardwareMonitor"""
182-
url = QUrl(f"http://{self._server_host}:{self._server_port}/Sensor?action=Get&id={self._sensor_id}")
183-
request = QNetworkRequest(url)
184-
request.setHeader(QNetworkRequest.KnownHeaders.ContentTypeHeader, "application/x-www-form-urlencoded")
185-
self._network_manager.post(request, b"")
196+
self._network_manager.post(self.request, b"")
186197

187198
def _handle_network_response(self, reply: QNetworkReply):
188199
"""
@@ -195,18 +206,21 @@ def _handle_network_response(self, reply: QNetworkReply):
195206
if self._data.get("result") == "ok":
196207
self._data["status"] = "Connected..."
197208
else:
198-
self._data["status"] = "Invalid sensor id..."
209+
self._data["status"] = self._sensor_id_error_label
210+
self._data["histogram"] = self._sensor_id_error_label
199211
elif reply.error() == QNetworkReply.NetworkError.AuthenticationRequiredError:
200212
self._data = {
201-
"status": "Authentication Failed...",
213+
"status": self._auth_error_label,
202214
"result": "fail",
203-
"value": 0.0,
215+
"value": "",
216+
"histogram": self._auth_error_label,
204217
}
205218
else:
206219
self._data = {
207-
"status": "Connection Error...",
220+
"status": self._connection_error_label,
208221
"result": "fail",
209-
"value": 0.0,
222+
"value": "",
223+
"histogram": self._auth_error_label,
210224
}
211225
reply.deleteLater()
212226

0 commit comments

Comments
 (0)