Type check KNX integration fan (#48056)

This commit is contained in:
Matthias Alphart 2021-03-19 20:55:08 +01:00 committed by GitHub
parent b03c97cdd0
commit 16a4f05e27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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