1
0
mirror of https://github.com/home-assistant/core synced 2024-07-03 08:18:59 +00:00

Add strict typing to CPU Speed (#62924)

This commit is contained in:
Franck Nijhof 2021-12-28 13:19:36 +01:00 committed by GitHub
parent 05ac2d4c3a
commit 7b5a159899
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 15 deletions

View File

@ -31,6 +31,7 @@ homeassistant.components.camera.*
homeassistant.components.canary.*
homeassistant.components.cover.*
homeassistant.components.crownstone.*
homeassistant.components.cpuspeed.*
homeassistant.components.device_automation.*
homeassistant.components.device_tracker.*
homeassistant.components.devolo_home_control.*

View File

@ -1,10 +1,17 @@
"""Support for displaying the current CPU speed."""
from __future__ import annotations
from typing import Any
from cpuinfo import cpuinfo
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import CONF_NAME, FREQUENCY_GIGAHERTZ
from homeassistant.core import HomeAssistant
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_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."""
name = config[CONF_NAME]
add_entities([CpuSpeedSensor(name)], True)
async_add_entities([CpuSpeedSensor(name)], True)
class CpuSpeedSensor(SensorEntity):
@ -32,27 +44,29 @@ class CpuSpeedSensor(SensorEntity):
_attr_native_unit_of_measurement = FREQUENCY_GIGAHERTZ
_attr_icon = "mdi:pulse"
def __init__(self, name):
def __init__(self, name: str) -> None:
"""Initialize the CPU sensor."""
self._attr_name = name
self.info = None
self.info: dict[str, Any] | None = None
@property
def extra_state_attributes(self):
def extra_state_attributes(self) -> dict[str, float | str | None] | None:
"""Return the state attributes."""
if self.info is not 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
if self.info is None:
return None
def update(self):
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:
"""Get the latest data and updates the state."""
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)
else:
self._attr_native_value = None

View File

@ -352,6 +352,17 @@ no_implicit_optional = true
warn_return_any = 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.*]
check_untyped_defs = true
disallow_incomplete_defs = true