mirror of
https://github.com/home-assistant/core
synced 2024-10-04 19:12:19 +00:00
Add strict typing to CPU Speed (#62924)
This commit is contained in:
parent
05ac2d4c3a
commit
7b5a159899
|
@ -31,6 +31,7 @@ homeassistant.components.camera.*
|
||||||
homeassistant.components.canary.*
|
homeassistant.components.canary.*
|
||||||
homeassistant.components.cover.*
|
homeassistant.components.cover.*
|
||||||
homeassistant.components.crownstone.*
|
homeassistant.components.crownstone.*
|
||||||
|
homeassistant.components.cpuspeed.*
|
||||||
homeassistant.components.device_automation.*
|
homeassistant.components.device_automation.*
|
||||||
homeassistant.components.device_tracker.*
|
homeassistant.components.device_tracker.*
|
||||||
homeassistant.components.devolo_home_control.*
|
homeassistant.components.devolo_home_control.*
|
||||||
|
|
|
@ -1,10 +1,17 @@
|
||||||
"""Support for displaying the current CPU speed."""
|
"""Support for displaying the current CPU speed."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from cpuinfo import cpuinfo
|
from cpuinfo import cpuinfo
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
||||||
from homeassistant.const import CONF_NAME, FREQUENCY_GIGAHERTZ
|
from homeassistant.const import CONF_NAME, FREQUENCY_GIGAHERTZ
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
ATTR_BRAND = "brand"
|
ATTR_BRAND = "brand"
|
||||||
ATTR_HZ = "ghz_advertised"
|
ATTR_HZ = "ghz_advertised"
|
||||||
|
@ -20,10 +27,15 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
async def async_setup_platform(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config: ConfigType,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
discovery_info: DiscoveryInfoType | None = None,
|
||||||
|
) -> None:
|
||||||
"""Set up the CPU speed sensor."""
|
"""Set up the CPU speed sensor."""
|
||||||
name = config[CONF_NAME]
|
name = config[CONF_NAME]
|
||||||
add_entities([CpuSpeedSensor(name)], True)
|
async_add_entities([CpuSpeedSensor(name)], True)
|
||||||
|
|
||||||
|
|
||||||
class CpuSpeedSensor(SensorEntity):
|
class CpuSpeedSensor(SensorEntity):
|
||||||
|
@ -32,15 +44,17 @@ class CpuSpeedSensor(SensorEntity):
|
||||||
_attr_native_unit_of_measurement = FREQUENCY_GIGAHERTZ
|
_attr_native_unit_of_measurement = FREQUENCY_GIGAHERTZ
|
||||||
_attr_icon = "mdi:pulse"
|
_attr_icon = "mdi:pulse"
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name: str) -> None:
|
||||||
"""Initialize the CPU sensor."""
|
"""Initialize the CPU sensor."""
|
||||||
self._attr_name = name
|
self._attr_name = name
|
||||||
self.info = None
|
self.info: dict[str, Any] | None = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self) -> dict[str, float | str | None] | None:
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
if self.info is not None:
|
if self.info is None:
|
||||||
|
return None
|
||||||
|
|
||||||
attrs = {
|
attrs = {
|
||||||
ATTR_ARCH: self.info["arch_string_raw"],
|
ATTR_ARCH: self.info["arch_string_raw"],
|
||||||
ATTR_BRAND: self.info["brand_raw"],
|
ATTR_BRAND: self.info["brand_raw"],
|
||||||
|
@ -49,10 +63,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
|
||||||
|
|
||||||
def update(self):
|
def update(self) -> None:
|
||||||
"""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 self.info is not None and HZ_ACTUAL in self.info:
|
||||||
self._attr_native_value = 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._attr_native_value = None
|
self._attr_native_value = None
|
||||||
|
|
11
mypy.ini
11
mypy.ini
|
@ -352,6 +352,17 @@ no_implicit_optional = true
|
||||||
warn_return_any = true
|
warn_return_any = true
|
||||||
warn_unreachable = true
|
warn_unreachable = true
|
||||||
|
|
||||||
|
[mypy-homeassistant.components.cpuspeed.*]
|
||||||
|
check_untyped_defs = true
|
||||||
|
disallow_incomplete_defs = true
|
||||||
|
disallow_subclassing_any = true
|
||||||
|
disallow_untyped_calls = true
|
||||||
|
disallow_untyped_decorators = true
|
||||||
|
disallow_untyped_defs = true
|
||||||
|
no_implicit_optional = true
|
||||||
|
warn_return_any = true
|
||||||
|
warn_unreachable = true
|
||||||
|
|
||||||
[mypy-homeassistant.components.device_automation.*]
|
[mypy-homeassistant.components.device_automation.*]
|
||||||
check_untyped_defs = true
|
check_untyped_defs = true
|
||||||
disallow_incomplete_defs = true
|
disallow_incomplete_defs = true
|
||||||
|
|
Loading…
Reference in a new issue