ESPHome enable static type checking (#52348)
This commit is contained in:
parent
9b2107b71f
commit
4d16cda957
16 changed files with 364 additions and 304 deletions
|
@ -2,6 +2,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import Any
|
||||
|
||||
from aioesphomeapi import FanDirection, FanInfo, FanSpeed, FanState
|
||||
|
||||
|
@ -15,6 +16,7 @@ from homeassistant.components.fan import (
|
|||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util.percentage import (
|
||||
ordered_list_item_to_percentage,
|
||||
percentage_to_ordered_list_item,
|
||||
|
@ -33,7 +35,7 @@ ORDERED_NAMED_FAN_SPEEDS = [FanSpeed.LOW, FanSpeed.MEDIUM, FanSpeed.HIGH]
|
|||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up ESPHome fans based on a config entry."""
|
||||
await platform_async_setup_entry(
|
||||
|
@ -47,7 +49,7 @@ async def async_setup_entry(
|
|||
)
|
||||
|
||||
|
||||
_FAN_DIRECTIONS: EsphomeEnumMapper[FanDirection] = EsphomeEnumMapper(
|
||||
_FAN_DIRECTIONS: EsphomeEnumMapper[FanDirection, str] = EsphomeEnumMapper(
|
||||
{
|
||||
FanDirection.FORWARD: DIRECTION_FORWARD,
|
||||
FanDirection.REVERSE: DIRECTION_REVERSE,
|
||||
|
@ -55,29 +57,26 @@ _FAN_DIRECTIONS: EsphomeEnumMapper[FanDirection] = EsphomeEnumMapper(
|
|||
)
|
||||
|
||||
|
||||
class EsphomeFan(EsphomeEntity, FanEntity):
|
||||
# https://github.com/PyCQA/pylint/issues/3150 for all @esphome_state_property
|
||||
# Pylint gets confused with the EsphomeEntity generics -> let mypy handle member checking
|
||||
# pylint: disable=invalid-overridden-method,no-member
|
||||
|
||||
|
||||
class EsphomeFan(EsphomeEntity[FanInfo, FanState], FanEntity):
|
||||
"""A fan implementation for ESPHome."""
|
||||
|
||||
@property
|
||||
def _static_info(self) -> FanInfo:
|
||||
return super()._static_info
|
||||
|
||||
@property
|
||||
def _state(self) -> FanState | None:
|
||||
return super()._state
|
||||
|
||||
@property
|
||||
def _supports_speed_levels(self) -> bool:
|
||||
api_version = self._api_version
|
||||
return api_version.major == 1 and api_version.minor > 3
|
||||
|
||||
async def async_set_percentage(self, percentage: int) -> None:
|
||||
async def async_set_percentage(self, percentage: int | None) -> None:
|
||||
"""Set the speed percentage of the fan."""
|
||||
if percentage == 0:
|
||||
await self.async_turn_off()
|
||||
return
|
||||
|
||||
data = {"key": self._static_info.key, "state": True}
|
||||
data: dict[str, Any] = {"key": self._static_info.key, "state": True}
|
||||
if percentage is not None:
|
||||
if self._supports_speed_levels:
|
||||
data["speed_level"] = math.ceil(
|
||||
|
@ -97,12 +96,12 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
|||
speed: str | None = None,
|
||||
percentage: int | None = None,
|
||||
preset_mode: str | None = None,
|
||||
**kwargs,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
"""Turn on the fan."""
|
||||
await self.async_set_percentage(percentage)
|
||||
|
||||
async def async_turn_off(self, **kwargs) -> None:
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn off the fan."""
|
||||
await self._client.fan_command(key=self._static_info.key, state=False)
|
||||
|
||||
|
@ -112,17 +111,14 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
|||
key=self._static_info.key, oscillating=oscillating
|
||||
)
|
||||
|
||||
async def async_set_direction(self, direction: str):
|
||||
async def async_set_direction(self, direction: str) -> None:
|
||||
"""Set direction of the fan."""
|
||||
await self._client.fan_command(
|
||||
key=self._static_info.key, direction=_FAN_DIRECTIONS.from_hass(direction)
|
||||
)
|
||||
|
||||
# https://github.com/PyCQA/pylint/issues/3150 for all @esphome_state_property
|
||||
# pylint: disable=invalid-overridden-method
|
||||
|
||||
@esphome_state_property
|
||||
def is_on(self) -> bool | None:
|
||||
def is_on(self) -> bool | None: # type: ignore[override]
|
||||
"""Return true if the entity is on."""
|
||||
return self._state.state
|
||||
|
||||
|
@ -134,7 +130,7 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
|||
|
||||
if not self._supports_speed_levels:
|
||||
return ordered_list_item_to_percentage(
|
||||
ORDERED_NAMED_FAN_SPEEDS, self._state.speed
|
||||
ORDERED_NAMED_FAN_SPEEDS, self._state.speed # type: ignore[misc]
|
||||
)
|
||||
|
||||
return ranged_value_to_percentage(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue