core/homeassistant/components/knx/fan.py

114 lines
3.5 KiB
Python
Raw Normal View History

2021-02-10 08:09:34 +00:00
"""Support for KNX/IP fans."""
2021-03-18 12:07:04 +00:00
from __future__ import annotations
2021-02-10 08:09:34 +00:00
import math
from typing import Any
2021-02-10 08:09:34 +00:00
from xknx.devices import Fan as XknxFan
from homeassistant.components.fan import SUPPORT_OSCILLATE, SUPPORT_SET_SPEED, FanEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2021-02-10 08:09:34 +00:00
from homeassistant.util.percentage import (
int_states_in_range,
2021-02-10 08:09:34 +00:00
percentage_to_ranged_value,
ranged_value_to_percentage,
)
from .const import DOMAIN
from .knx_entity import KnxEntity
DEFAULT_PERCENTAGE = 50
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
2021-02-10 08:09:34 +00:00
"""Set up fans for KNX platform."""
entities = []
for device in hass.data[DOMAIN].xknx.devices:
if isinstance(device, XknxFan):
entities.append(KNXFan(device))
async_add_entities(entities)
class KNXFan(KnxEntity, FanEntity):
"""Representation of a KNX fan."""
def __init__(self, device: XknxFan) -> None:
2021-02-10 08:09:34 +00:00
"""Initialize of KNX fan."""
self._device: XknxFan
2021-02-10 08:09:34 +00:00
super().__init__(device)
self._unique_id = f"{self._device.speed.group_address}"
self._step_range: tuple[int, int] | None = None
if device.max_step:
# FanSpeedMode.STEP:
2021-02-10 08:09:34 +00:00
self._step_range = (1, device.max_step)
async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed of the fan, as a percentage."""
if self._step_range:
2021-02-10 08:09:34 +00:00
step = math.ceil(percentage_to_ranged_value(self._step_range, percentage))
await self._device.set_speed(step)
else:
await self._device.set_speed(percentage)
@property
def supported_features(self) -> int:
"""Flag supported features."""
flags = SUPPORT_SET_SPEED
if self._device.supports_oscillation:
flags |= SUPPORT_OSCILLATE
return flags
@property
2021-03-18 12:07:04 +00:00
def percentage(self) -> int | None:
2021-02-10 08:09:34 +00:00
"""Return the current speed as a percentage."""
if self._device.current_speed is None:
return None
if self._step_range:
2021-02-10 08:09:34 +00:00
return ranged_value_to_percentage(
self._step_range, self._device.current_speed
)
return self._device.current_speed
@property
def speed_count(self) -> int:
"""Return the number of speeds the fan supports."""
if self._step_range is None:
return super().speed_count
return int_states_in_range(self._step_range)
2021-02-10 08:09:34 +00:00
async def async_turn_on(
self,
2021-03-18 12:07:04 +00:00
speed: str | None = None,
percentage: int | None = None,
preset_mode: str | None = None,
**kwargs: Any,
2021-02-10 08:09:34 +00:00
) -> None:
"""Turn on the fan."""
if percentage is None:
await self.async_set_percentage(DEFAULT_PERCENTAGE)
else:
await self.async_set_percentage(percentage)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the fan off."""
await self.async_set_percentage(0)
async def async_oscillate(self, oscillating: bool) -> None:
"""Oscillate the fan."""
await self._device.set_oscillation(oscillating)
@property
def oscillating(self) -> bool | None:
2021-02-10 08:09:34 +00:00
"""Return whether or not the fan is currently oscillating."""
return self._device.current_oscillation