Skip to content

Fix KeyError when sys_tempc/sys_tempf missing from system stats - #109

Open
agu2347 wants to merge 1 commit into
colinodell:masterfrom
agu2347:fix-system-temp-keyerror-issue-108
Open

Fix KeyError when sys_tempc/sys_tempf missing from system stats#109
agu2347 wants to merge 1 commit into
colinodell:masterfrom
agu2347:fix-system-temp-keyerror-issue-108

Conversation

@agu2347

@agu2347 agu2347 commented Aug 24, 2026

Copy link
Copy Markdown

Problem

get_system_stats() accesses root["sys_tempc"] and root["sys_tempf"] unconditionally when parsing the QNAP systemstats API response (#108). On some QNAP models/firmware versions the device has no readable system temperature sensor, and the API omits these fields from the response entirely rather than returning a zero or null value. This causes a KeyError, crashing the entire get_system_stats() call for affected users (not just the temperature reading).

Fix

Mirror the existing defensive-access pattern already used for cpu_model a few lines above in the same function:

"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,

instead of the previous unconditional int(root["sys_tempc"]) / int(root["sys_tempf"]).

Testing

Added a new fixture directory tests/responses/TS-X53-4.5.4-faulty-sys-sensor/ (login.xml / login_with_get.xml copied from an existing fixture, systemstats.xml derived from an existing fixture with the sys_tempc/sys_tempf lines removed to reproduce the missing-field response) plus the expected systemstats.json output (temp_c/temp_f both null, all other fields populated normally), following the existing data-driven pattern in tests/test-models.py.

Verified with a baseline-vs-fixed comparison across the full fixture set:

  • Before fix: this new fixture produced an unhandled KeyError, and one existing pre-existing test category showed a corresponding error.
  • After fix: the new fixture passes, producing temp_c/temp_f as null as expected, with no other fixture's result changed (the pre-existing 7 unrelated MISMATCH results in the fixture set are unchanged before/after, confirming no regressions).

Fixes #108.

…nodell#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KeyError: 'sys_tempc' when the NAS does not report a system temperature (faulty sensor, or model without one)

1 participant