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
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2019-10-19 20:33:05 +02:00
|
|
|
from cpuinfo import cpuinfo
|
2016-08-19 14:57:14 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 12:37:16 +01:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2021-12-28 21:17:53 +01:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2020-04-12 22:44:31 +02:00
|
|
|
from homeassistant.const import CONF_NAME, FREQUENCY_GIGAHERTZ
|
2021-12-28 13:19:36 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-10-19 20:33:05 +02:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-12-28 13:19:36 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2015-10-15 12:13:04 +02:00
|
|
|
|
2021-12-28 21:17:53 +01:00
|
|
|
from .const import DOMAIN, LOGGER
|
|
|
|
|
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
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
DEFAULT_NAME = "CPU speed"
|
2017-06-05 17:35:26 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}
|
|
|
|
)
|
2016-08-19 14:57:14 +02:00
|
|
|
|
2015-10-15 12:13:04 +02:00
|
|
|
|
2021-12-28 13:19:36 +01:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-03-14 07:54:10 +01:00
|
|
|
"""Set up the CPU speed sensor."""
|
2021-12-28 21:17:53 +01:00
|
|
|
LOGGER.warning(
|
|
|
|
"Configuration of the CPU Speed platform in YAML is deprecated and will be "
|
|
|
|
"removed in Home Assistant 2022.4; Your existing configuration "
|
|
|
|
"has been imported into the UI automatically and can be safely removed "
|
|
|
|
"from your configuration.yaml file"
|
|
|
|
)
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data={CONF_NAME: config[CONF_NAME]},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the platform from config_entry."""
|
|
|
|
async_add_entities([CPUSpeedSensor("CPU Speed")], 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
|
|
|
|
2021-12-28 11:38:42 +01:00
|
|
|
_attr_native_unit_of_measurement = FREQUENCY_GIGAHERTZ
|
|
|
|
_attr_icon = "mdi:pulse"
|
|
|
|
|
2021-12-28 13:19:36 +01:00
|
|
|
def __init__(self, name: str) -> None:
|
2020-08-23 18:44:11 +02:00
|
|
|
"""Initialize the CPU sensor."""
|
2021-12-28 11:38:42 +01:00
|
|
|
self._attr_name = name
|
2021-12-28 13:19:36 +01:00
|
|
|
self.info: dict[str, Any] | None = None
|
2015-10-15 12:13:04 +02:00
|
|
|
|
|
|
|
@property
|
2021-12-28 13:19:36 +01:00
|
|
|
def extra_state_attributes(self) -> dict[str, float | str | None] | None:
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the state attributes."""
|
2021-12-28 13:19:36 +01:00
|
|
|
if self.info is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
attrs = {
|
|
|
|
ATTR_ARCH: self.info["arch_string_raw"],
|
|
|
|
ATTR_BRAND: self.info["brand_raw"],
|
|
|
|
}
|
|
|
|
if HZ_ADVERTISED in self.info:
|
|
|
|
attrs[ATTR_HZ] = round(self.info[HZ_ADVERTISED][0] / 10 ** 9, 2)
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
def update(self) -> None:
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Get the latest data and updates the state."""
|
2015-10-15 12:13:04 +02:00
|
|
|
self.info = cpuinfo.get_cpu_info()
|
2021-12-28 13:19:36 +01:00
|
|
|
if self.info is not None and HZ_ACTUAL in self.info:
|
2021-12-28 11:38:42 +01:00
|
|
|
self._attr_native_value = round(float(self.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
|