mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Use shorthand attributes in the CPU Speed integration (#62896)
This commit is contained in:
parent
41c497ee6e
commit
f6c1266af6
@ -15,8 +15,6 @@ HZ_ADVERTISED = "hz_advertised"
|
|||||||
|
|
||||||
DEFAULT_NAME = "CPU speed"
|
DEFAULT_NAME = "CPU speed"
|
||||||
|
|
||||||
ICON = "mdi:pulse"
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}
|
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}
|
||||||
)
|
)
|
||||||
@ -31,27 +29,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
class CpuSpeedSensor(SensorEntity):
|
class CpuSpeedSensor(SensorEntity):
|
||||||
"""Representation of a CPU sensor."""
|
"""Representation of a CPU sensor."""
|
||||||
|
|
||||||
|
_attr_native_unit_of_measurement = FREQUENCY_GIGAHERTZ
|
||||||
|
_attr_icon = "mdi:pulse"
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
"""Initialize the CPU sensor."""
|
"""Initialize the CPU sensor."""
|
||||||
self._name = name
|
self._attr_name = name
|
||||||
self._state = None
|
|
||||||
self.info = None
|
self.info = None
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self):
|
|
||||||
"""Return the unit the value is expressed in."""
|
|
||||||
return FREQUENCY_GIGAHERTZ
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
@ -64,15 +49,10 @@ class CpuSpeedSensor(SensorEntity):
|
|||||||
attrs[ATTR_HZ] = round(self.info[HZ_ADVERTISED][0] / 10 ** 9, 2)
|
attrs[ATTR_HZ] = round(self.info[HZ_ADVERTISED][0] / 10 ** 9, 2)
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Return the icon to use in the frontend, if any."""
|
|
||||||
return ICON
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data and updates the state."""
|
"""Get the latest data and updates the state."""
|
||||||
self.info = cpuinfo.get_cpu_info()
|
self.info = cpuinfo.get_cpu_info()
|
||||||
if HZ_ACTUAL in self.info:
|
if HZ_ACTUAL in self.info:
|
||||||
self._state = round(float(self.info[HZ_ACTUAL][0]) / 10 ** 9, 2)
|
self._attr_native_value = round(float(self.info[HZ_ACTUAL][0]) / 10 ** 9, 2)
|
||||||
else:
|
else:
|
||||||
self._state = None
|
self._attr_native_value = None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user