core/homeassistant/components/sensor/cpuspeed.py

74 lines
1.9 KiB
Python
Raw Normal View History

2015-10-15 10:13:04 +00:00
"""
2016-02-23 05:21:49 +00:00
Support for displaying the current CPU speed.
2015-10-15 10:13:04 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/sensor.cpuspeed/
2015-10-15 10:13:04 +00:00
"""
import logging
from homeassistant.helpers.entity import Entity
2016-02-28 15:16:03 +00:00
REQUIREMENTS = ['py-cpuinfo==0.2.3']
2015-10-15 10:13:04 +00:00
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "CPU speed"
ATTR_VENDOR = 'Vendor ID'
ATTR_BRAND = 'Brand'
ATTR_HZ = 'GHz Advertised'
2016-02-05 12:08:17 +00:00
ICON = 'mdi:pulse'
2015-10-15 10:13:04 +00:00
# pylint: disable=unused-variable
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-08 15:46:34 +00:00
"""Setup the CPU speed sensor."""
2015-10-15 10:13:04 +00:00
add_devices([CpuSpeedSensor(config.get('name', DEFAULT_NAME))])
class CpuSpeedSensor(Entity):
2016-03-08 15:46:34 +00:00
"""Representation a CPU sensor."""
2015-10-15 10:13:04 +00:00
def __init__(self, name):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2015-10-15 10:13:04 +00:00
self._name = name
self._state = None
self._unit_of_measurement = 'GHz'
self.update()
@property
def name(self):
2016-03-08 15:46:34 +00:00
"""Return the name of the sensor."""
2015-10-15 10:13:04 +00:00
return self._name
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state of the sensor."""
2015-10-15 10:13:04 +00:00
return self._state
@property
def unit_of_measurement(self):
2016-03-08 15:46:34 +00:00
"""return the unit the value is expressed in."""
2015-10-15 10:13:04 +00:00
return self._unit_of_measurement
@property
def device_state_attributes(self):
2016-03-08 15:46:34 +00:00
"""Return the state attributes."""
2015-10-15 10:13:04 +00:00
if self.info is not None:
return {
ATTR_VENDOR: self.info['vendor_id'],
ATTR_BRAND: self.info['brand'],
ATTR_HZ: round(self.info['hz_advertised_raw'][0]/10**9, 2)
}
2016-02-05 12:08:17 +00:00
@property
def icon(self):
2016-03-08 15:46:34 +00:00
"""Return the icon to use in the frontend, if any."""
2016-02-05 12:08:17 +00:00
return ICON
2015-10-15 10:13:04 +00:00
def update(self):
2016-03-08 15:46:34 +00:00
"""Get the latest data and updates the state."""
2015-10-15 10:13:04 +00:00
from cpuinfo import cpuinfo
self.info = cpuinfo.get_cpu_info()
self._state = round(float(self.info['hz_actual_raw'][0])/10**9, 2)