From e50a47621fa56cf459cc97a1e19210e50e817b05 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 3 Dec 2021 19:08:23 +0100 Subject: [PATCH] Enable basic type checking for climacell (#55334) --- homeassistant/components/climacell/__init__.py | 4 ++-- homeassistant/components/climacell/sensor.py | 6 +++++- homeassistant/components/climacell/weather.py | 11 ++++++----- mypy.ini | 3 +++ script/hassfest/mypy_config.py | 1 + 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/climacell/__init__.py b/homeassistant/components/climacell/__init__.py index aceb24b695f..e3edc778955 100644 --- a/homeassistant/components/climacell/__init__.py +++ b/homeassistant/components/climacell/__init__.py @@ -112,7 +112,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up ClimaCell API from a config entry.""" hass.data.setdefault(DOMAIN, {}) - params = {} + params: dict[str, Any] = {} # If config entry options not set up, set them up if not entry.options: params["options"] = { @@ -206,7 +206,7 @@ class ClimaCellDataUpdateCoordinator(DataUpdateCoordinator): async def _async_update_data(self) -> dict[str, Any]: """Update data via library.""" - data = {FORECASTS: {}} + data: dict[str, Any] = {FORECASTS: {}} try: if self._api_version == 3: data[CURRENT] = await self._api.realtime( diff --git a/homeassistant/components/climacell/sensor.py b/homeassistant/components/climacell/sensor.py index f934449fdb0..597e1095f89 100644 --- a/homeassistant/components/climacell/sensor.py +++ b/homeassistant/components/climacell/sensor.py @@ -28,6 +28,8 @@ async def async_setup_entry( ) -> None: """Set up a config entry.""" coordinator = hass.data[DOMAIN][config_entry.entry_id] + api_class: type[BaseClimaCellSensorEntity] + sensor_types: tuple[ClimaCellSensorEntityDescription, ...] if (api_version := config_entry.data[CONF_API_VERSION]) == 3: api_class = ClimaCellV3SensorEntity @@ -81,6 +83,7 @@ class BaseClimaCellSensorEntity(ClimaCellEntity, SensorEntity): state = self._state if ( state is not None + and not isinstance(state, str) and self.entity_description.unit_imperial is not None and self.entity_description.metric_conversion != 1.0 and self.entity_description.is_metric_check is not None @@ -95,7 +98,8 @@ class BaseClimaCellSensorEntity(ClimaCellEntity, SensorEntity): return round(state * conversion, 4) if self.entity_description.value_map is not None and state is not None: - return self.entity_description.value_map(state).name.lower() + # mypy bug: "Literal[IntEnum.value]" not callable + return self.entity_description.value_map(state).name.lower() # type: ignore[misc] return state diff --git a/homeassistant/components/climacell/weather.py b/homeassistant/components/climacell/weather.py index bf1c95b524a..eafb47aac99 100644 --- a/homeassistant/components/climacell/weather.py +++ b/homeassistant/components/climacell/weather.py @@ -4,7 +4,7 @@ from __future__ import annotations from abc import abstractmethod from collections.abc import Mapping from datetime import datetime -from typing import Any +from typing import Any, cast from pyclimacell.const import ( CURRENT, @@ -136,7 +136,7 @@ class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity): @staticmethod @abstractmethod def _translate_condition( - condition: int | None, sun_is_up: bool = True + condition: str | int | None, sun_is_up: bool = True ) -> str | None: """Translate ClimaCell condition into an HA condition.""" @@ -144,7 +144,7 @@ class BaseClimaCellWeatherEntity(ClimaCellEntity, WeatherEntity): self, forecast_dt: datetime, use_datetime: bool, - condition: str, + condition: int | str, precipitation: float | None, precipitation_probability: float | None, temp: float | None, @@ -274,7 +274,7 @@ class ClimaCellWeatherEntity(BaseClimaCellWeatherEntity): @staticmethod def _translate_condition( - condition: int | None, sun_is_up: bool = True + condition: int | str | None, sun_is_up: bool = True ) -> str | None: """Translate ClimaCell condition into an HA condition.""" if condition is None: @@ -422,11 +422,12 @@ class ClimaCellV3WeatherEntity(BaseClimaCellWeatherEntity): @staticmethod def _translate_condition( - condition: str | None, sun_is_up: bool = True + condition: int | str | None, sun_is_up: bool = True ) -> str | None: """Translate ClimaCell condition into an HA condition.""" if not condition: return None + condition = cast(str, condition) if "clear" in condition.lower(): if sun_is_up: return CLEAR_CONDITIONS["day"] diff --git a/mypy.ini b/mypy.ini index 483b8bd22f1..7346cc83ba9 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1711,6 +1711,9 @@ ignore_errors = true [mypy-homeassistant.components.climacell.*] ignore_errors = true +[mypy-homeassistant.components.cloud.*] +ignore_errors = true + [mypy-homeassistant.components.config.*] ignore_errors = true diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index 41dfa1ad05e..6fc6c0e3993 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -16,6 +16,7 @@ from .model import Config, Integration IGNORED_MODULES: Final[list[str]] = [ "homeassistant.components.blueprint.*", "homeassistant.components.climacell.*", + "homeassistant.components.cloud.*", "homeassistant.components.config.*", "homeassistant.components.conversation.*", "homeassistant.components.deconz.*",