feat(sensors): numeric -n mode + lmsensors_n extend for HA SNMP migration [#618]

This commit is contained in:
2026-09-01 18:02:09 -05:00
parent 196d0f597e
commit 7b922c507c
3 changed files with 99 additions and 1 deletions
+34 -1
View File
@@ -37,6 +37,39 @@ format_block() {
}
# True when a TEMPer USB dongle (0c45:7401) is attached and the venv exists.
# Numeric mode: print ONE Celsius float for snmpd extend lmsensors_n [#618].
# Preference: TEMPer external > TEMPer internal > first native/sysfs temp
# reading. Consumed by HA's SNMP config-entry integration (device_class
# temperature; HA renders C in the instance display unit).
numeric_pick() {
local internal="${1:-}" external="${2:-}" native="${3:-}" pick
local num_re='^-?[0-9]+(\.[0-9]+)?$'
for pick in "$external" "$internal"; do
pick="${pick#+}"
[[ "$pick" =~ $num_re ]] && { printf '%s\n' "$pick"; return 0; }
done
if [[ -n "$native" ]]; then
pick="$(grep -oE '\-?[0-9]+(\.[0-9]+)?' <<<"$native" | head -1)"
[[ "$pick" =~ $num_re ]] && { printf '%s\n' "$pick"; return 0; }
fi
return 1
}
numeric_main() {
local internal external line
if has_temper_probe; then
if read -r internal external <<<"$(read_temper 2>/dev/null)"; then
numeric_pick "${internal:-}" "${external:-}" "" && return 0
fi
fi
line="$(run_native_sensors | grep -m1 '°C')"
[[ -n "$line" ]] && numeric_pick "" "" "$line" && return 0
line="$(read_sysfs_thermal 2>/dev/null | head -1 | awk '{printf "+%.1f°C", $2/1000}')"
[[ -n "$line" ]] && numeric_pick "" "" "$line" && return 0
echo "error: numeric mode found no sensor source" >&2
return 1
}
has_temper_probe() {
command -v lsusb >/dev/null 2>&1 || return 1
[[ -x "$VENV_PY" ]] || return 1
@@ -130,5 +163,5 @@ main() {
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
main
if [[ "${1:-}" == "-n" ]]; then numeric_main; else main; fi
fi