From d1965eef8bc406e4bb183a3d0223eb56245ffc53 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Sat, 28 Aug 2021 12:05:48 +0200 Subject: [PATCH] Activate mypy for sonar (#55327) * Please mypy. Co-authored-by: Martin Hjelmare --- .../components/sonarr/config_flow.py | 10 +++--- homeassistant/components/sonarr/sensor.py | 35 +++++++++++-------- mypy.ini | 3 -- script/hassfest/mypy_config.py | 1 - 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/sonarr/config_flow.py b/homeassistant/components/sonarr/config_flow.py index cc35a8db4af..f226d1883a5 100644 --- a/homeassistant/components/sonarr/config_flow.py +++ b/homeassistant/components/sonarr/config_flow.py @@ -35,7 +35,7 @@ from .const import ( _LOGGER = logging.getLogger(__name__) -async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]: +async def validate_input(hass: HomeAssistant, data: dict) -> None: """Validate the user input allows us to connect. Data has the keys from DATA_SCHEMA with values provided by the user. @@ -54,8 +54,6 @@ async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]: await sonarr.update() - return True - class SonarrConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for Sonarr.""" @@ -72,14 +70,14 @@ class SonarrConfigFlow(ConfigFlow, domain=DOMAIN): """Get the options flow for this handler.""" return SonarrOptionsFlowHandler(config_entry) - async def async_step_reauth(self, data: dict[str, Any] | None = None) -> FlowResult: + async def async_step_reauth(self, data: dict[str, Any]) -> FlowResult: """Handle configuration by re-auth.""" self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) return await self.async_step_reauth_confirm() async def async_step_reauth_confirm( - self, user_input: dict[str, Any] | None = None + self, user_input: dict[str, str] | None = None ) -> FlowResult: """Confirm reauth dialog.""" if user_input is None: @@ -164,7 +162,7 @@ class SonarrOptionsFlowHandler(OptionsFlow): """Initialize options flow.""" self.config_entry = config_entry - async def async_step_init(self, user_input: dict[str, Any] | None = None): + async def async_step_init(self, user_input: dict[str, int] | None = None): """Manage Sonarr options.""" if user_input is not None: return self.async_create_entry(title="", data=user_input) diff --git a/homeassistant/components/sonarr/sensor.py b/homeassistant/components/sonarr/sensor.py index 3f5ef275fef..374791304c7 100644 --- a/homeassistant/components/sonarr/sensor.py +++ b/homeassistant/components/sonarr/sensor.py @@ -3,9 +3,16 @@ from __future__ import annotations from datetime import timedelta import logging -from typing import Any from sonarr import Sonarr, SonarrConnectionError, SonarrError +from sonarr.models import ( + CommandItem, + Disk, + Episode, + QueueItem, + SeriesItem, + WantedResults, +) from homeassistant.components.sensor import SensorEntity from homeassistant.config_entries import ConfigEntry @@ -106,7 +113,7 @@ class SonarrCommandsSensor(SonarrSensor): def __init__(self, sonarr: Sonarr, entry_id: str) -> None: """Initialize Sonarr Commands sensor.""" - self._commands = [] + self._commands: list[CommandItem] = [] super().__init__( sonarr=sonarr, @@ -124,7 +131,7 @@ class SonarrCommandsSensor(SonarrSensor): self._commands = await self.sonarr.commands() @property - def extra_state_attributes(self) -> dict[str, Any] | None: + def extra_state_attributes(self) -> dict[str, str] | None: """Return the state attributes of the entity.""" attrs = {} @@ -144,7 +151,7 @@ class SonarrDiskspaceSensor(SonarrSensor): def __init__(self, sonarr: Sonarr, entry_id: str) -> None: """Initialize Sonarr Disk Space sensor.""" - self._disks = [] + self._disks: list[Disk] = [] self._total_free = 0 super().__init__( @@ -165,7 +172,7 @@ class SonarrDiskspaceSensor(SonarrSensor): self._total_free = sum(disk.free for disk in self._disks) @property - def extra_state_attributes(self) -> dict[str, Any] | None: + def extra_state_attributes(self) -> dict[str, str] | None: """Return the state attributes of the entity.""" attrs = {} @@ -192,7 +199,7 @@ class SonarrQueueSensor(SonarrSensor): def __init__(self, sonarr: Sonarr, entry_id: str) -> None: """Initialize Sonarr Queue sensor.""" - self._queue = [] + self._queue: list[QueueItem] = [] super().__init__( sonarr=sonarr, @@ -210,7 +217,7 @@ class SonarrQueueSensor(SonarrSensor): self._queue = await self.sonarr.queue() @property - def extra_state_attributes(self) -> dict[str, Any] | None: + def extra_state_attributes(self) -> dict[str, str] | None: """Return the state attributes of the entity.""" attrs = {} @@ -233,7 +240,7 @@ class SonarrSeriesSensor(SonarrSensor): def __init__(self, sonarr: Sonarr, entry_id: str) -> None: """Initialize Sonarr Series sensor.""" - self._items = [] + self._items: list[SeriesItem] = [] super().__init__( sonarr=sonarr, @@ -251,7 +258,7 @@ class SonarrSeriesSensor(SonarrSensor): self._items = await self.sonarr.series() @property - def extra_state_attributes(self) -> dict[str, Any] | None: + def extra_state_attributes(self) -> dict[str, str] | None: """Return the state attributes of the entity.""" attrs = {} @@ -272,7 +279,7 @@ class SonarrUpcomingSensor(SonarrSensor): def __init__(self, sonarr: Sonarr, entry_id: str, days: int = 1) -> None: """Initialize Sonarr Upcoming sensor.""" self._days = days - self._upcoming = [] + self._upcoming: list[Episode] = [] super().__init__( sonarr=sonarr, @@ -294,7 +301,7 @@ class SonarrUpcomingSensor(SonarrSensor): ) @property - def extra_state_attributes(self) -> dict[str, Any] | None: + def extra_state_attributes(self) -> dict[str, str] | None: """Return the state attributes of the entity.""" attrs = {} @@ -315,7 +322,7 @@ class SonarrWantedSensor(SonarrSensor): def __init__(self, sonarr: Sonarr, entry_id: str, max_items: int = 10) -> None: """Initialize Sonarr Wanted sensor.""" self._max_items = max_items - self._results = None + self._results: WantedResults | None = None self._total: int | None = None super().__init__( @@ -335,9 +342,9 @@ class SonarrWantedSensor(SonarrSensor): self._total = self._results.total @property - def extra_state_attributes(self) -> dict[str, Any] | None: + def extra_state_attributes(self) -> dict[str, str] | None: """Return the state attributes of the entity.""" - attrs = {} + attrs: dict[str, str] = {} if self._results is not None: for episode in self._results.episodes: diff --git a/mypy.ini b/mypy.ini index 02a2800a801..92247205c10 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1597,9 +1597,6 @@ ignore_errors = true [mypy-homeassistant.components.somfy_mylink.*] ignore_errors = true -[mypy-homeassistant.components.sonarr.*] -ignore_errors = true - [mypy-homeassistant.components.sonos.*] ignore_errors = true diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index 0026be479a4..91bba97fa31 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -121,7 +121,6 @@ IGNORED_MODULES: Final[list[str]] = [ "homeassistant.components.solaredge.*", "homeassistant.components.somfy.*", "homeassistant.components.somfy_mylink.*", - "homeassistant.components.sonarr.*", "homeassistant.components.sonos.*", "homeassistant.components.spotify.*", "homeassistant.components.stt.*",