From 14271a7686f2b0ad82d5fc8e988540f2840afe24 Mon Sep 17 00:00:00 2001
From: agu2347 <94227848+agu2347@users.noreply.github.com>
Date: Mon, 24 Aug 2026 18:57:35 +0530
Subject: [PATCH] Fix KeyError when sys_tempc/sys_tempf missing from system
stats (#108)
Some QNAP models (or firmware versions) omit the sys_tempc/sys_tempf
fields from the systemstats API response entirely when the device has
no readable system temperature sensor. get_system_stats() accessed
these fields unconditionally via root["sys_tempc"]/root["sys_tempf"],
raising a KeyError and crashing the whole stats call for those users.
The codebase already established the correct defensive pattern for
this exact situation (see the pre-existing cpu_model handling), which
this change mirrors: fall back to None when the key is absent instead
of raising.
Adds a new fixture directory (TS-X53-4.5.4-faulty-sys-sensor) modeling
a device response with the temperature fields absent, verifying
get_system_stats() returns temp_c/temp_f as null instead of raising.
---
qnapstats/qnap_stats.py | 4 +--
.../TS-X53-4.5.4-faulty-sys-sensor/login.xml | 20 ++++++++++++
.../login_with_get.xml | 4 +++
.../systemstats.json | 1 +
.../systemstats.xml | 31 +++++++++++++++++++
5 files changed, 58 insertions(+), 2 deletions(-)
create mode 100644 tests/responses/TS-X53-4.5.4-faulty-sys-sensor/login.xml
create mode 100644 tests/responses/TS-X53-4.5.4-faulty-sys-sensor/login_with_get.xml
create mode 100644 tests/responses/TS-X53-4.5.4-faulty-sys-sensor/systemstats.json
create mode 100644 tests/responses/TS-X53-4.5.4-faulty-sys-sensor/systemstats.xml
diff --git a/qnapstats/qnap_stats.py b/qnapstats/qnap_stats.py
index 1758738..4987182 100644
--- a/qnapstats/qnap_stats.py
+++ b/qnapstats/qnap_stats.py
@@ -229,8 +229,8 @@ def get_system_stats(self):
"name": root["server_name"],
"model": resp["model"]["displayModelName"],
"serial_number": root["serial_number"],
- "temp_c": int(root["sys_tempc"]),
- "temp_f": int(root["sys_tempf"]),
+ "temp_c": int(root["sys_tempc"]) if "sys_tempc" in root else None,
+ "temp_f": int(root["sys_tempf"]) if "sys_tempf" in root else None,
"timezone": root["timezone"],
},
"firmware": {
diff --git a/tests/responses/TS-X53-4.5.4-faulty-sys-sensor/login.xml b/tests/responses/TS-X53-4.5.4-faulty-sys-sensor/login.xml
new file mode 100644
index 0000000..442260e
--- /dev/null
+++ b/tests/responses/TS-X53-4.5.4-faulty-sys-sensor/login.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/tests/responses/TS-X53-4.5.4-faulty-sys-sensor/login_with_get.xml b/tests/responses/TS-X53-4.5.4-faulty-sys-sensor/login_with_get.xml
new file mode 100644
index 0000000..4761d49
--- /dev/null
+++ b/tests/responses/TS-X53-4.5.4-faulty-sys-sensor/login_with_get.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/tests/responses/TS-X53-4.5.4-faulty-sys-sensor/systemstats.json b/tests/responses/TS-X53-4.5.4-faulty-sys-sensor/systemstats.json
new file mode 100644
index 0000000..687ea74
--- /dev/null
+++ b/tests/responses/TS-X53-4.5.4-faulty-sys-sensor/systemstats.json
@@ -0,0 +1 @@
+{"cpu": {"model": null, "temp_c": null, "temp_f": null, "usage_percent": 48.5}, "dns": ["192.168.1.1"], "firmware": {"build": "20170121", "build_time": "21-01-2017", "patch": "0", "version": "4.2.3"}, "memory": {"free": 60.8, "total": 249.6}, "nics": {"eth0": {"err_packets": 0, "ip": "192.168.1.101", "link_status": "Up", "mac": "00:08:9B:C1:80:6A", "mask": "255.255.255.0", "max_speed": 1000, "rx_packets": 193439491, "tx_packets": 123234929, "usage": "DHCP"}, "eth1": {"err_packets": 0, "ip": "0.0.0.0", "link_status": "Down", "mac": "00:08:9B:C1:80:6B", "mask": "0.0.0.0", "max_speed": 1000, "rx_packets": 0, "tx_packets": 0, "usage": "DHCP"}}, "sysfans": {}, "system": {"model": "TS-253 Pro", "name": "hornbill", "serial_number": "MYSERIAL", "temp_c": null, "temp_f": null, "timezone": "(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna"}, "uptime": {"days": 13, "hours": 17, "minutes": 47, "seconds": 48}}
\ No newline at end of file
diff --git a/tests/responses/TS-X53-4.5.4-faulty-sys-sensor/systemstats.xml b/tests/responses/TS-X53-4.5.4-faulty-sys-sensor/systemstats.xml
new file mode 100644
index 0000000..27f6fbd
--- /dev/null
+++ b/tests/responses/TS-X53-4.5.4-faulty-sys-sensor/systemstats.xml
@@ -0,0 +1,31 @@
+
+0
+
+
+
+193439491
+123234929
+0
+1
+1000
+192.168.1.101
+255.255.255.0
+00:08:9B:C1:80:6A
+DHCP
+0
+0
+0
+0
+1000
+0.0.0.0
+0.0.0.0
+00:08:9B:C1:80:6B
+DHCP
+
+0
+MYSERIAL
+
+
+
+
\ No newline at end of file