From 16a4f05e27f583fe5d05c66516487cdc2765b385 Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Fri, 19 Mar 2021 20:55:08 +0100 Subject: [PATCH] Type check KNX integration fan (#48056) --- homeassistant/components/knx/fan.py | 33 +++++++++++++++++++---------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/knx/fan.py b/homeassistant/components/knx/fan.py index 2d9f48fe804..7f68c2a7d51 100644 --- a/homeassistant/components/knx/fan.py +++ b/homeassistant/components/knx/fan.py @@ -2,12 +2,17 @@ from __future__ import annotations import math -from typing import Any +from typing import Any, Callable, Iterable from xknx.devices import Fan as XknxFan -from xknx.devices.fan import FanSpeedMode from homeassistant.components.fan import SUPPORT_OSCILLATE, SUPPORT_SET_SPEED, FanEntity +from homeassistant.helpers.entity import Entity +from homeassistant.helpers.typing import ( + ConfigType, + DiscoveryInfoType, + HomeAssistantType, +) from homeassistant.util.percentage import ( int_states_in_range, percentage_to_ranged_value, @@ -20,7 +25,12 @@ from .knx_entity import KnxEntity DEFAULT_PERCENTAGE = 50 -async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): +async def async_setup_platform( + hass: HomeAssistantType, + config: ConfigType, + async_add_entities: Callable[[Iterable[Entity]], None], + discovery_info: DiscoveryInfoType | None = None, +) -> None: """Set up fans for KNX platform.""" entities = [] for device in hass.data[DOMAIN].xknx.devices: @@ -32,18 +42,19 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= class KNXFan(KnxEntity, FanEntity): """Representation of a KNX fan.""" - def __init__(self, device: XknxFan): + def __init__(self, device: XknxFan) -> None: """Initialize of KNX fan.""" + self._device: XknxFan super().__init__(device) - if self._device.mode == FanSpeedMode.STEP: + self._step_range: tuple[int, int] | None = None + if device.max_step: + # FanSpeedMode.STEP: self._step_range = (1, device.max_step) - else: - self._step_range = None async def async_set_percentage(self, percentage: int) -> None: """Set the speed of the fan, as a percentage.""" - if self._device.mode == FanSpeedMode.STEP: + if self._step_range: step = math.ceil(percentage_to_ranged_value(self._step_range, percentage)) await self._device.set_speed(step) else: @@ -65,7 +76,7 @@ class KNXFan(KnxEntity, FanEntity): if self._device.current_speed is None: return None - if self._device.mode == FanSpeedMode.STEP: + if self._step_range: return ranged_value_to_percentage( self._step_range, self._device.current_speed ) @@ -83,7 +94,7 @@ class KNXFan(KnxEntity, FanEntity): speed: str | None = None, percentage: int | None = None, preset_mode: str | None = None, - **kwargs, + **kwargs: Any, ) -> None: """Turn on the fan.""" if percentage is None: @@ -100,6 +111,6 @@ class KNXFan(KnxEntity, FanEntity): await self._device.set_oscillation(oscillating) @property - def oscillating(self): + def oscillating(self) -> bool | None: """Return whether or not the fan is currently oscillating.""" return self._device.current_oscillation