From e7e53b879e62224d5d61baac15b82a20799f1d5d Mon Sep 17 00:00:00 2001 From: Phil Hollenback Date: Fri, 9 Apr 2021 01:25:03 -0700 Subject: [PATCH] Fix cpu temperature reporting for Armbian on Odroid (#48903) Some systems expose cpu temperatures differently in psutil. Specifically, running armbian on the Odroid xu4 sbc gives the following temerature output: >>> pp.pprint(psutil.sensors_temperatures()) { 'cpu0-thermal': [ shwtemp(label='', current=54.0, high=115.0, critical=115.0)], 'cpu1-thermal': [ shwtemp(label='', current=56.0, high=115.0, critical=115.0)], 'cpu2-thermal': [ shwtemp(label='', current=58.0, high=115.0, critical=115.0)], 'cpu3-thermal': [ shwtemp(label='', current=56.0, high=115.0, critical=115.0)], } Since the cpu number is embedded inside the name, the current code can't find it. To fix this, check both the name and the constructed label for matches against CPU_SENSOR_PREFIXES, and add the appropriate label cpu0-thermal in the prefix list. While this is slightly less efficient that just generating the label and checking it, it results in easier to understand code. --- homeassistant/components/systemmonitor/sensor.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/systemmonitor/sensor.py b/homeassistant/components/systemmonitor/sensor.py index dea7d371b4b..94f747014a4 100644 --- a/homeassistant/components/systemmonitor/sensor.py +++ b/homeassistant/components/systemmonitor/sensor.py @@ -180,6 +180,7 @@ CPU_SENSOR_PREFIXES = [ "soc-thermal 1", "soc_thermal 1", "Tctl", + "cpu0-thermal", ] @@ -504,7 +505,9 @@ def _read_cpu_temperature() -> float | None: # In case the label is empty (e.g. on Raspberry PI 4), # construct it ourself here based on the sensor key name. _label = f"{name} {i}" if not entry.label else entry.label - if _label in CPU_SENSOR_PREFIXES: + # check both name and label because some systems embed cpu# in the + # name, which makes label not match because label adds cpu# at end. + if _label in CPU_SENSOR_PREFIXES or name in CPU_SENSOR_PREFIXES: return cast(float, round(entry.current, 1)) return None