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
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 11:37:16 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2021-12-28 20:17:53 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2020-04-12 20:44:31 +00:00
|
|
|
from homeassistant.const import CONF_NAME, FREQUENCY_GIGAHERTZ
|
2021-12-28 12:19:36 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-10-19 18:33:05 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-12-28 12:19:36 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2015-10-15 10:13:04 +00:00
|
|
|
|
2021-12-28 20:17:53 +00:00
|
|
|
from .const import DOMAIN, LOGGER
|
|
|
|
|
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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "CPU speed"
|
2017-06-05 15:35:26 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}
|
|
|
|
)
|
2016-08-19 12:57:14 +00:00
|
|
|
|
2015-10-15 10:13:04 +00:00
|
|
|
|
2021-12-28 12:19:36 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-03-14 06:54:10 +00:00
|
|
|
"""Set up the CPU speed sensor."""
|
2021-12-28 20:17:53 +00: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."""
|
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
|
|
|
|
2021-12-28 10:38:42 +00:00
|
|
|
_attr_icon = "mdi:pulse"
|
2021-12-28 21:28:06 +00:00
|
|
|
_attr_name = "CPU Speed"
|
|
|
|
_attr_native_unit_of_measurement = FREQUENCY_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
|
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:
|
2021-12-28 21:28:06 +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 = {
|
|
|
|
ATTR_ARCH: info["arch_string_raw"],
|
|
|
|
ATTR_BRAND: info["brand_raw"],
|
|
|
|
}
|
|
|
|
if HZ_ADVERTISED in info:
|
|
|
|
self._attr_extra_state_attributes[ATTR_HZ] = round(
|
|
|
|
info[HZ_ADVERTISED][0] / 10 ** 9, 2
|
|
|
|
)
|