Define ViCare fan entity presets based on the actual by the device supported presets (#130886)

* only show supported presets

* update snapshot

* Apply suggestions from code review

* move code to init

* async executor

* Revert "update snapshot"

This reverts commit ca92b5ed27.

* Update fan.py
pull/131478/head
Christopher Fenner 2024-11-24 19:37:40 +01:00 committed by GitHub
parent 1dc99ebc05
commit 84630ef8cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 26 additions and 14 deletions

View File

@ -29,6 +29,7 @@ from homeassistant.util.percentage import (
from .const import DEVICE_LIST, DOMAIN
from .entity import ViCareEntity
from .types import ViCareDevice
from .utils import get_device_serial
_LOGGER = logging.getLogger(__name__)
@ -90,6 +91,17 @@ ORDERED_NAMED_FAN_SPEEDS = [
]
def _build_entities(
device_list: list[ViCareDevice],
) -> list[ViCareFan]:
"""Create ViCare climate entities for a device."""
return [
ViCareFan(get_device_serial(device.api), device.config, device.api)
for device in device_list
if isinstance(device.api, PyViCareVentilationDevice)
]
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
@ -100,27 +112,18 @@ async def async_setup_entry(
device_list = hass.data[DOMAIN][config_entry.entry_id][DEVICE_LIST]
async_add_entities(
[
ViCareFan(get_device_serial(device.api), device.config, device.api)
for device in device_list
if isinstance(device.api, PyViCareVentilationDevice)
]
await hass.async_add_executor_job(
_build_entities,
device_list,
)
)
class ViCareFan(ViCareEntity, FanEntity):
"""Representation of the ViCare ventilation device."""
_attr_preset_modes = list[str](
[
VentilationMode.PERMANENT,
VentilationMode.VENTILATION,
VentilationMode.SENSOR_DRIVEN,
VentilationMode.SENSOR_OVERRIDE,
]
)
_attr_speed_count = len(ORDERED_NAMED_FAN_SPEEDS)
_attr_supported_features = FanEntityFeature.SET_SPEED | FanEntityFeature.PRESET_MODE
_attr_supported_features = FanEntityFeature.SET_SPEED
_attr_translation_key = "ventilation"
_enable_turn_on_off_backwards_compatibility = False
@ -134,6 +137,15 @@ class ViCareFan(ViCareEntity, FanEntity):
super().__init__(
self._attr_translation_key, device_serial, device_config, device
)
# init presets
supported_modes = list[str](self._api.getAvailableModes())
self._attr_preset_modes = [
mode
for mode in VentilationMode
if VentilationMode.to_vicare_mode(mode) in supported_modes
]
if len(self._attr_preset_modes) > 0:
self._attr_supported_features |= FanEntityFeature.PRESET_MODE
def update(self) -> None:
"""Update state of fan."""