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 48d207f250f99d8126702342c05a6be6e877e4d5.

* 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.esphome.*
homeassistant.components.energy.* homeassistant.components.energy.*
homeassistant.components.evil_genius_labs.* homeassistant.components.evil_genius_labs.*
homeassistant.components.fan.*
homeassistant.components.fastdotcom.* homeassistant.components.fastdotcom.*
homeassistant.components.filesize.* homeassistant.components.filesize.*
homeassistant.components.fitbit.* homeassistant.components.fitbit.*

View File

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

View File

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