Create KNX fan entities directly from config (#50702)

This commit is contained in:
Matthias Alphart 2021-05-16 08:34:14 +02:00 committed by GitHub
parent 0556c35e24
commit 1e11bfae05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 32 deletions

View File

@ -8,7 +8,6 @@ from xknx.devices import (
ClimateMode as XknxClimateMode,
Cover as XknxCover,
Device as XknxDevice,
Fan as XknxFan,
Light as XknxLight,
Notification as XknxNotification,
Sensor as XknxSensor,
@ -23,7 +22,6 @@ from .schema import (
BinarySensorSchema,
ClimateSchema,
CoverSchema,
FanSchema,
LightSchema,
SensorSchema,
WeatherSchema,
@ -57,9 +55,6 @@ def create_knx_device(
if platform is SupportedPlatforms.WEATHER:
return _create_weather(knx_module, config)
if platform is SupportedPlatforms.FAN:
return _create_fan(knx_module, config)
return None
@ -335,20 +330,3 @@ def _create_weather(knx_module: XKNX, config: ConfigType) -> XknxWeather:
),
group_address_humidity=config.get(WeatherSchema.CONF_KNX_HUMIDITY_ADDRESS),
)
def _create_fan(knx_module: XKNX, config: ConfigType) -> XknxFan:
"""Return a KNX Fan device to be used within XKNX."""
fan = XknxFan(
knx_module,
name=config[CONF_NAME],
group_address_speed=config.get(KNX_ADDRESS),
group_address_speed_state=config.get(FanSchema.CONF_STATE_ADDRESS),
group_address_oscillation=config.get(FanSchema.CONF_OSCILLATION_ADDRESS),
group_address_oscillation_state=config.get(
FanSchema.CONF_OSCILLATION_STATE_ADDRESS
),
max_step=config.get(FanSchema.CONF_MAX_STEP),
)
return fan

View File

@ -4,9 +4,11 @@ from __future__ import annotations
import math
from typing import Any
from xknx import XKNX
from xknx.devices import Fan as XknxFan
from homeassistant.components.fan import SUPPORT_OSCILLATE, SUPPORT_SET_SPEED, FanEntity
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -16,8 +18,9 @@ from homeassistant.util.percentage import (
ranged_value_to_percentage,
)
from .const import DOMAIN
from .const import DOMAIN, KNX_ADDRESS
from .knx_entity import KnxEntity
from .schema import FanSchema
DEFAULT_PERCENTAGE = 50
@ -29,25 +32,44 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up fans for KNX platform."""
if not discovery_info or not discovery_info["platform_config"]:
return
platform_config = discovery_info["platform_config"]
xknx: XKNX = hass.data[DOMAIN].xknx
entities = []
for device in hass.data[DOMAIN].xknx.devices:
if isinstance(device, XknxFan):
entities.append(KNXFan(device))
for entity_config in platform_config:
entities.append(KNXFan(xknx, entity_config))
async_add_entities(entities)
class KNXFan(KnxEntity, FanEntity):
"""Representation of a KNX fan."""
def __init__(self, device: XknxFan) -> None:
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
"""Initialize of KNX fan."""
self._device: XknxFan
super().__init__(device)
max_step = config.get(FanSchema.CONF_MAX_STEP)
super().__init__(
device=XknxFan(
xknx,
name=config[CONF_NAME],
group_address_speed=config.get(KNX_ADDRESS),
group_address_speed_state=config.get(FanSchema.CONF_STATE_ADDRESS),
group_address_oscillation=config.get(
FanSchema.CONF_OSCILLATION_ADDRESS
),
group_address_oscillation_state=config.get(
FanSchema.CONF_OSCILLATION_STATE_ADDRESS
),
max_step=max_step,
)
)
self._unique_id = f"{self._device.speed.group_address}"
self._step_range: tuple[int, int] | None = None
if device.max_step:
# FanSpeedMode.STEP:
self._step_range = (1, device.max_step)
# FanSpeedMode.STEP if max_step is set
self._step_range: tuple[int, int] | None = (1, max_step) if max_step else None
async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed of the fan, as a percentage."""