From 9e7d7354ed86826b51aef842f9bfd59d5c9c3fd0 Mon Sep 17 00:00:00 2001 From: Rohan Kapoor Date: Thu, 31 Jan 2019 23:58:29 -0800 Subject: [PATCH] Fix sensor.cpuspeed inside docker container (#20614) (#20656) --- homeassistant/components/sensor/cpuspeed.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/sensor/cpuspeed.py b/homeassistant/components/sensor/cpuspeed.py index e97972dae3b..f69d0b285ba 100644 --- a/homeassistant/components/sensor/cpuspeed.py +++ b/homeassistant/components/sensor/cpuspeed.py @@ -21,6 +21,9 @@ ATTR_BRAND = 'Brand' ATTR_HZ = 'GHz Advertised' ATTR_ARCH = 'arch' +HZ_ACTUAL_RAW = 'hz_actual_raw' +HZ_ADVERTISED_RAW = 'hz_advertised_raw' + DEFAULT_NAME = 'CPU speed' ICON = 'mdi:pulse' @@ -66,12 +69,17 @@ class CpuSpeedSensor(Entity): def device_state_attributes(self): """Return the state attributes.""" if self.info is not None: - return { + attrs = { ATTR_ARCH: self.info['arch'], ATTR_BRAND: self.info['brand'], - ATTR_HZ: round(self.info['hz_advertised_raw'][0]/10**9, 2) } + if HZ_ADVERTISED_RAW in self.info: + attrs[ATTR_HZ] = round( + self.info[HZ_ADVERTISED_RAW][0] / 10 ** 9, 2 + ) + return attrs + @property def icon(self): """Return the icon to use in the frontend, if any.""" @@ -82,4 +90,9 @@ class CpuSpeedSensor(Entity): from cpuinfo import cpuinfo self.info = cpuinfo.get_cpu_info() - self._state = round(float(self.info['hz_actual_raw'][0])/10**9, 2) + if HZ_ACTUAL_RAW in self.info: + self._state = round( + float(self.info[HZ_ACTUAL_RAW][0]) / 10 ** 9, 2 + ) + else: + self._state = None