From 7b5a159899fbc3aa323468ffa65335f6c292b751 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 28 Dec 2021 13:19:36 +0100 Subject: [PATCH] Add strict typing to CPU Speed (#62924) --- .strict-typing | 1 + homeassistant/components/cpuspeed/sensor.py | 44 ++++++++++++++------- mypy.ini | 11 ++++++ 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/.strict-typing b/.strict-typing index cc64f5ca5b0..3117c6ed908 100644 --- a/.strict-typing +++ b/.strict-typing @@ -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.* diff --git a/homeassistant/components/cpuspeed/sensor.py b/homeassistant/components/cpuspeed/sensor.py index d26e3278396..e832d35fbd8 100644 --- a/homeassistant/components/cpuspeed/sensor.py +++ b/homeassistant/components/cpuspeed/sensor.py @@ -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 diff --git a/mypy.ini b/mypy.ini index 4cfa08ca38a..53b022dd98c 100644 --- a/mypy.ini +++ b/mypy.ini @@ -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