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