hass-core/homeassistant/components/sensor/cpuspeed.py

86 lines
2.2 KiB
Python
Raw Normal View History

2015-10-15 12:13:04 +02:00
"""
2016-02-23 06:21:49 +01:00
Support for displaying the current CPU speed.
2015-10-15 12:13:04 +02:00
For more details about this platform, please refer to the documentation at
2015-11-09 13:12:18 +01:00
https://home-assistant.io/components/sensor.cpuspeed/
2015-10-15 12:13:04 +02:00
"""
import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME
2015-10-15 12:13:04 +02:00
from homeassistant.helpers.entity import Entity
2018-04-02 11:58:22 +02:00
REQUIREMENTS = ['py-cpuinfo==4.0.0']
2015-10-15 12:13:04 +02:00
_LOGGER = logging.getLogger(__name__)
2015-10-15 12:13:04 +02:00
ATTR_BRAND = 'Brand'
ATTR_HZ = 'GHz Advertised'
ATTR_ARCH = 'arch'
DEFAULT_NAME = 'CPU speed'
2016-02-05 13:08:17 +01:00
ICON = 'mdi:pulse'
2015-10-15 12:13:04 +02:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})
2015-10-15 12:13:04 +02:00
def setup_platform(hass, config, add_entities, discovery_info=None):
2017-03-14 07:54:10 +01:00
"""Set up the CPU speed sensor."""
name = config.get(CONF_NAME)
add_entities([CpuSpeedSensor(name)], True)
2015-10-15 12:13:04 +02:00
class CpuSpeedSensor(Entity):
"""Representation of a CPU sensor."""
2016-03-08 16:46:34 +01:00
2015-10-15 12:13:04 +02:00
def __init__(self, name):
2016-03-08 16:46:34 +01:00
"""Initialize the sensor."""
2015-10-15 12:13:04 +02:00
self._name = name
self._state = None
self.info = None
2015-10-15 12:13:04 +02:00
self._unit_of_measurement = 'GHz'
@property
def name(self):
2016-03-08 16:46:34 +01:00
"""Return the name of the sensor."""
2015-10-15 12:13:04 +02:00
return self._name
@property
def state(self):
2016-03-08 16:46:34 +01:00
"""Return the state of the sensor."""
2015-10-15 12:13:04 +02:00
return self._state
@property
def unit_of_measurement(self):
2016-03-09 23:34:38 -08:00
"""Return the unit the value is expressed in."""
2015-10-15 12:13:04 +02:00
return self._unit_of_measurement
@property
def device_state_attributes(self):
2016-03-08 16:46:34 +01:00
"""Return the state attributes."""
2015-10-15 12:13:04 +02:00
if self.info is not None:
return {
ATTR_ARCH: self.info['arch'],
2015-10-15 12:13:04 +02:00
ATTR_BRAND: self.info['brand'],
ATTR_HZ: round(self.info['hz_advertised_raw'][0]/10**9, 2)
}
2016-02-05 13:08:17 +01:00
@property
def icon(self):
2016-03-08 16:46:34 +01:00
"""Return the icon to use in the frontend, if any."""
2016-02-05 13:08:17 +01:00
return ICON
2015-10-15 12:13:04 +02:00
def update(self):
2016-03-08 16:46:34 +01:00
"""Get the latest data and updates the state."""
2015-10-15 12:13:04 +02:00
from cpuinfo import cpuinfo
self.info = cpuinfo.get_cpu_info()
self._state = round(float(self.info['hz_actual_raw'][0])/10**9, 2)