Add fan to strict typing (#73820)

* Add fan to strict typing

* Adjust state_attributes

* Adjust capability_attributes

* Adjust is_on

* Adjust vallox component

* Revert "Adjust is_on"

This reverts commit 48d207f250.

* Fix is_on property
This commit is contained in:
epenet 2022-06-22 14:15:44 +02:00 committed by GitHub
parent 33a84838b4
commit 754fe86dd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 12 deletions

View file

@ -87,6 +87,7 @@ homeassistant.components.emulated_hue.*
homeassistant.components.esphome.*
homeassistant.components.energy.*
homeassistant.components.evil_genius_labs.*
homeassistant.components.fan.*
homeassistant.components.fastdotcom.*
homeassistant.components.filesize.*
homeassistant.components.fitbit.*

View file

@ -7,7 +7,7 @@ from enum import IntEnum
import functools as ft
import logging
import math
from typing import final
from typing import Any, final
import voluptuous as vol
@ -80,9 +80,11 @@ class NotValidPresetModeError(ValueError):
@bind_hass
def is_on(hass, entity_id: str) -> bool:
def is_on(hass: HomeAssistant, entity_id: str) -> bool:
"""Return if the fans are on based on the statemachine."""
return hass.states.get(entity_id).state == STATE_ON
entity = hass.states.get(entity_id)
assert entity
return entity.state == STATE_ON
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
@ -235,7 +237,7 @@ class FanEntity(ToggleEntity):
"""Set new preset mode."""
await self.hass.async_add_executor_job(self.set_preset_mode, preset_mode)
def _valid_preset_mode_or_raise(self, preset_mode):
def _valid_preset_mode_or_raise(self, preset_mode: str) -> None:
"""Raise NotValidPresetModeError on invalid preset_mode."""
preset_modes = self.preset_modes
if not preset_modes or preset_mode not in preset_modes:
@ -247,7 +249,7 @@ class FanEntity(ToggleEntity):
"""Set the direction of the fan."""
raise NotImplementedError()
async def async_set_direction(self, direction: str):
async def async_set_direction(self, direction: str) -> None:
"""Set the direction of the fan."""
await self.hass.async_add_executor_job(self.set_direction, direction)
@ -255,7 +257,7 @@ class FanEntity(ToggleEntity):
self,
percentage: int | None = None,
preset_mode: str | None = None,
**kwargs,
**kwargs: Any,
) -> None:
"""Turn on the fan."""
raise NotImplementedError()
@ -264,7 +266,7 @@ class FanEntity(ToggleEntity):
self,
percentage: int | None = None,
preset_mode: str | None = None,
**kwargs,
**kwargs: Any,
) -> None:
"""Turn on the fan."""
await self.hass.async_add_executor_job(
@ -280,12 +282,12 @@ class FanEntity(ToggleEntity):
"""Oscillate the fan."""
raise NotImplementedError()
async def async_oscillate(self, oscillating: bool):
async def async_oscillate(self, oscillating: bool) -> None:
"""Oscillate the fan."""
await self.hass.async_add_executor_job(self.oscillate, oscillating)
@property
def is_on(self):
def is_on(self) -> bool | None:
"""Return true if the entity is on."""
return (
self.percentage is not None and self.percentage > 0
@ -321,7 +323,7 @@ class FanEntity(ToggleEntity):
return self._attr_oscillating
@property
def capability_attributes(self):
def capability_attributes(self) -> dict[str, list[str] | None]:
"""Return capability attributes."""
attrs = {}
@ -335,7 +337,7 @@ class FanEntity(ToggleEntity):
@final
@property
def state_attributes(self) -> dict:
def state_attributes(self) -> dict[str, float | str | None]:
"""Return optional state attributes."""
data: dict[str, float | str | None] = {}
supported_features = self.supported_features

View file

@ -132,7 +132,7 @@ class ValloxFan(ValloxEntity, FanEntity):
Returns true if the mode has been changed, false otherwise.
"""
try:
self._valid_preset_mode_or_raise(preset_mode) # type: ignore[no-untyped-call]
self._valid_preset_mode_or_raise(preset_mode)
except NotValidPresetModeError as err:
_LOGGER.error(err)

View file

@ -720,6 +720,17 @@ no_implicit_optional = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.fan.*]
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.fastdotcom.*]
check_untyped_defs = true
disallow_incomplete_defs = true