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

72 lines
1.9 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
from homeassistant.helpers.entity import Entity
2016-02-28 16:16:03 +01:00
REQUIREMENTS = ['py-cpuinfo==0.2.3']
2015-10-15 12:13:04 +02:00
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "CPU speed"
ATTR_VENDOR = 'Vendor ID'
ATTR_BRAND = 'Brand'
ATTR_HZ = 'GHz Advertised'
2016-02-05 13:08:17 +01:00
ICON = 'mdi:pulse'
2015-10-15 12:13:04 +02:00
# pylint: disable=unused-variable
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-02-23 06:21:49 +01:00
"""Sets up the CPU speed sensor."""
2015-10-15 12:13:04 +02:00
add_devices([CpuSpeedSensor(config.get('name', DEFAULT_NAME))])
class CpuSpeedSensor(Entity):
2016-02-28 16:16:03 +01:00
"""Represents a CPU sensor."""
2015-10-15 12:13:04 +02:00
def __init__(self, name):
self._name = name
self._state = None
self._unit_of_measurement = 'GHz'
self.update()
@property
def name(self):
2016-02-23 06:21:49 +01:00
"""The name of the sensor."""
2015-10-15 12:13:04 +02:00
return self._name
@property
def state(self):
2016-02-23 06:21:49 +01:00
"""Returns the state of the sensor."""
2015-10-15 12:13:04 +02:00
return self._state
@property
def unit_of_measurement(self):
2016-02-23 06:21:49 +01:00
"""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-02-23 06:21:49 +01:00
"""Returns the state attributes."""
2015-10-15 12:13:04 +02: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 13:08:17 +01:00
@property
def icon(self):
2016-02-23 06:21:49 +01:00
"""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-02-23 06:21:49 +01:00
"""Gets 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)