2019-03-23 08:00:43 +01:00
|
|
|
"""Support for displaying the current CPU speed."""
|
2021-12-28 13:19:36 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-10-19 20:33:05 +02:00
|
|
|
from cpuinfo import cpuinfo
|
2016-08-19 14:57:14 +02:00
|
|
|
|
2022-12-13 14:14:42 +01:00
|
|
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
2022-02-24 11:25:42 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-12-13 14:14:42 +01:00
|
|
|
from homeassistant.const import UnitOfFrequency
|
2021-12-28 13:19:36 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-07-20 11:31:17 +02:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2021-12-28 13:19:36 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-12-28 21:17:53 +01:00
|
|
|
|
2022-07-20 11:31:17 +02:00
|
|
|
from .const import DOMAIN
|
|
|
|
|
2020-08-23 18:44:11 +02:00
|
|
|
ATTR_BRAND = "brand"
|
|
|
|
ATTR_HZ = "ghz_advertised"
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_ARCH = "arch"
|
2016-08-21 00:40:16 +02:00
|
|
|
|
2020-08-23 18:44:11 +02:00
|
|
|
HZ_ACTUAL = "hz_actual"
|
|
|
|
HZ_ADVERTISED = "hz_advertised"
|
2019-01-31 23:58:29 -08:00
|
|
|
|
2021-12-28 21:17:53 +01:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the platform from config_entry."""
|
2021-12-28 22:28:06 +01:00
|
|
|
async_add_entities([CPUSpeedSensor(entry)], True)
|
2015-10-15 12:13:04 +02:00
|
|
|
|
|
|
|
|
2021-12-28 21:17:53 +01:00
|
|
|
class CPUSpeedSensor(SensorEntity):
|
2016-08-19 14:57:14 +02:00
|
|
|
"""Representation of a CPU sensor."""
|
2016-03-08 16:46:34 +01:00
|
|
|
|
2022-12-13 14:14:42 +01:00
|
|
|
_attr_device_class = SensorDeviceClass.FREQUENCY
|
2021-12-28 11:38:42 +01:00
|
|
|
_attr_icon = "mdi:pulse"
|
2022-07-20 11:31:17 +02:00
|
|
|
_attr_has_entity_name = True
|
2023-06-19 11:47:29 +02:00
|
|
|
_attr_name = None
|
2022-12-13 14:14:42 +01:00
|
|
|
_attr_native_unit_of_measurement = UnitOfFrequency.GIGAHERTZ
|
2021-12-28 11:38:42 +01:00
|
|
|
|
2021-12-28 22:28:06 +01:00
|
|
|
def __init__(self, entry: ConfigEntry) -> None:
|
2020-08-23 18:44:11 +02:00
|
|
|
"""Initialize the CPU sensor."""
|
2021-12-28 22:28:06 +01:00
|
|
|
self._attr_unique_id = entry.entry_id
|
2022-07-20 11:31:17 +02:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
name="CPU Speed",
|
|
|
|
identifiers={(DOMAIN, entry.entry_id)},
|
|
|
|
)
|
2021-12-28 13:19:36 +01:00
|
|
|
|
|
|
|
def update(self) -> None:
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Get the latest data and updates the state."""
|
2021-12-28 22:28:06 +01:00
|
|
|
info = cpuinfo.get_cpu_info()
|
|
|
|
|
2021-12-29 22:59:14 +01:00
|
|
|
if info and HZ_ACTUAL in info:
|
2022-02-05 14:19:37 +01:00
|
|
|
self._attr_native_value = round(float(info[HZ_ACTUAL][0]) / 10**9, 2)
|
2019-01-31 23:58:29 -08:00
|
|
|
else:
|
2021-12-28 11:38:42 +01:00
|
|
|
self._attr_native_value = None
|
2021-12-28 22:28:06 +01:00
|
|
|
|
2021-12-29 22:59:14 +01:00
|
|
|
if info:
|
2021-12-28 22:28:06 +01:00
|
|
|
self._attr_extra_state_attributes = {
|
2022-02-11 19:38:55 +01:00
|
|
|
ATTR_ARCH: info.get("arch_string_raw"),
|
|
|
|
ATTR_BRAND: info.get("brand_raw"),
|
2021-12-28 22:28:06 +01:00
|
|
|
}
|
|
|
|
if HZ_ADVERTISED in info:
|
|
|
|
self._attr_extra_state_attributes[ATTR_HZ] = round(
|
2022-02-05 14:19:37 +01:00
|
|
|
info[HZ_ADVERTISED][0] / 10**9, 2
|
2021-12-28 22:28:06 +01:00
|
|
|
)
|