Fix sensor.cpuspeed inside docker container (#20614) (#20656)

This commit is contained in:
Rohan Kapoor 2019-01-31 23:58:29 -08:00 committed by Fabian Affolter
parent 5f930debd4
commit 9e7d7354ed

View File

@ -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