|
|
|
|
@@ -20,6 +20,8 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|
|
|
|
from homeassistant.util.percentage import (
|
|
|
|
|
ordered_list_item_to_percentage,
|
|
|
|
|
percentage_to_ordered_list_item,
|
|
|
|
|
percentage_to_ranged_value,
|
|
|
|
|
ranged_value_to_percentage,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
from . import ThinqConfigEntry
|
|
|
|
|
@@ -35,6 +37,11 @@ class ThinQFanEntityDescription(FanEntityDescription):
|
|
|
|
|
preset_modes: list[str] | None = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HOOD_FAN_DESC = FanEntityDescription(
|
|
|
|
|
key=ThinQProperty.FAN_SPEED,
|
|
|
|
|
translation_key=ThinQProperty.FAN_SPEED,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
DEVICE_TYPE_FAN_MAP: dict[DeviceType, tuple[ThinQFanEntityDescription, ...]] = {
|
|
|
|
|
DeviceType.CEILING_FAN: (
|
|
|
|
|
ThinQFanEntityDescription(
|
|
|
|
|
@@ -54,6 +61,8 @@ DEVICE_TYPE_FAN_MAP: dict[DeviceType, tuple[ThinQFanEntityDescription, ...]] = {
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HOOD_DEVICE_TYPES: set[DeviceType] = {DeviceType.HOOD, DeviceType.MICROWAVE_OVEN}
|
|
|
|
|
|
|
|
|
|
ORDERED_NAMED_FAN_SPEEDS = ["low", "mid", "high", "turbo", "power"]
|
|
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
@@ -65,11 +74,20 @@ async def async_setup_entry(
|
|
|
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Set up an entry for fan platform."""
|
|
|
|
|
entities: list[ThinQFanEntity] = []
|
|
|
|
|
entities: list[ThinQFanEntity | ThinQHoodFanEntity] = []
|
|
|
|
|
for coordinator in entry.runtime_data.coordinators.values():
|
|
|
|
|
if (
|
|
|
|
|
descriptions := DEVICE_TYPE_FAN_MAP.get(coordinator.api.device.device_type)
|
|
|
|
|
) is not None:
|
|
|
|
|
device_type = coordinator.api.device.device_type
|
|
|
|
|
|
|
|
|
|
# Handle hood-type devices with numeric fan speed
|
|
|
|
|
if device_type in HOOD_DEVICE_TYPES:
|
|
|
|
|
entities.extend(
|
|
|
|
|
ThinQHoodFanEntity(coordinator, HOOD_FAN_DESC, property_id)
|
|
|
|
|
for property_id in coordinator.api.get_active_idx(
|
|
|
|
|
HOOD_FAN_DESC.key, ActiveMode.READ_WRITE
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
# Handle other fan devices with named speeds
|
|
|
|
|
elif (descriptions := DEVICE_TYPE_FAN_MAP.get(device_type)) is not None:
|
|
|
|
|
for description in descriptions:
|
|
|
|
|
entities.extend(
|
|
|
|
|
ThinQFanEntity(coordinator, description, property_id)
|
|
|
|
|
@@ -212,3 +230,120 @@ class ThinQFanEntity(ThinQEntity, FanEntity):
|
|
|
|
|
await self.async_call_api(
|
|
|
|
|
self.coordinator.api.async_turn_off(self._operation_id)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ThinQHoodFanEntity(ThinQEntity, FanEntity):
|
|
|
|
|
"""Represent a thinq hood fan platform.
|
|
|
|
|
|
|
|
|
|
Hood fans use numeric speed values (e.g., 0=off, 1=low, 2=high)
|
|
|
|
|
rather than named speed presets.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
_attr_supported_features = (
|
|
|
|
|
FanEntityFeature.SET_SPEED
|
|
|
|
|
| FanEntityFeature.TURN_ON
|
|
|
|
|
| FanEntityFeature.TURN_OFF
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
coordinator: DeviceDataUpdateCoordinator,
|
|
|
|
|
entity_description: FanEntityDescription,
|
|
|
|
|
property_id: str,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Initialize hood fan platform."""
|
|
|
|
|
super().__init__(coordinator, entity_description, property_id)
|
|
|
|
|
|
|
|
|
|
# Get min/max from data, default to 0-2 if not available
|
|
|
|
|
self._min_speed: int = int(self.data.min) if self.data.min is not None else 0
|
|
|
|
|
self._max_speed: int = int(self.data.max) if self.data.max is not None else 2
|
|
|
|
|
|
|
|
|
|
# Speed count is the number of non-zero speeds
|
|
|
|
|
self._attr_speed_count = self._max_speed - self._min_speed
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def _speed_range(self) -> tuple[int, int]:
|
|
|
|
|
"""Return the speed range excluding off (0)."""
|
|
|
|
|
return (self._min_speed + 1, self._max_speed)
|
|
|
|
|
|
|
|
|
|
def _update_status(self) -> None:
|
|
|
|
|
"""Update status itself."""
|
|
|
|
|
super()._update_status()
|
|
|
|
|
|
|
|
|
|
# Update min/max if available from data
|
|
|
|
|
if self.data.min is not None:
|
|
|
|
|
self._min_speed = int(self.data.min)
|
|
|
|
|
if self.data.max is not None:
|
|
|
|
|
self._max_speed = int(self.data.max)
|
|
|
|
|
self._attr_speed_count = self._max_speed - self._min_speed
|
|
|
|
|
|
|
|
|
|
# Get current speed value
|
|
|
|
|
current_speed = self.data.value
|
|
|
|
|
if current_speed is None or current_speed == self._min_speed:
|
|
|
|
|
self._attr_is_on = False
|
|
|
|
|
self._attr_percentage = 0
|
|
|
|
|
else:
|
|
|
|
|
self._attr_is_on = True
|
|
|
|
|
self._attr_percentage = ranged_value_to_percentage(
|
|
|
|
|
self._speed_range, current_speed
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
_LOGGER.debug(
|
|
|
|
|
"[%s:%s] update status: is_on=%s, percentage=%s, speed=%s, min=%s, max=%s",
|
|
|
|
|
self.coordinator.device_name,
|
|
|
|
|
self.property_id,
|
|
|
|
|
self.is_on,
|
|
|
|
|
self.percentage,
|
|
|
|
|
current_speed,
|
|
|
|
|
self._min_speed,
|
|
|
|
|
self._max_speed,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def async_set_percentage(self, percentage: int) -> None:
|
|
|
|
|
"""Set the speed percentage of the fan."""
|
|
|
|
|
if percentage == 0:
|
|
|
|
|
await self.async_turn_off()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
speed = round(percentage_to_ranged_value(self._speed_range, percentage))
|
|
|
|
|
|
|
|
|
|
_LOGGER.debug(
|
|
|
|
|
"[%s:%s] async_set_percentage: percentage=%s -> speed=%s",
|
|
|
|
|
self.coordinator.device_name,
|
|
|
|
|
self.property_id,
|
|
|
|
|
percentage,
|
|
|
|
|
speed,
|
|
|
|
|
)
|
|
|
|
|
await self.async_call_api(self.coordinator.api.post(self.property_id, speed))
|
|
|
|
|
|
|
|
|
|
async def async_turn_on(
|
|
|
|
|
self,
|
|
|
|
|
percentage: int | None = None,
|
|
|
|
|
preset_mode: str | None = None,
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Turn on the fan."""
|
|
|
|
|
if percentage is not None:
|
|
|
|
|
await self.async_set_percentage(percentage)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Default to lowest non-zero speed
|
|
|
|
|
speed = self._min_speed + 1
|
|
|
|
|
_LOGGER.debug(
|
|
|
|
|
"[%s:%s] async_turn_on: speed=%s",
|
|
|
|
|
self.coordinator.device_name,
|
|
|
|
|
self.property_id,
|
|
|
|
|
speed,
|
|
|
|
|
)
|
|
|
|
|
await self.async_call_api(self.coordinator.api.post(self.property_id, speed))
|
|
|
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
|
|
|
"""Turn the fan off."""
|
|
|
|
|
_LOGGER.debug(
|
|
|
|
|
"[%s:%s] async_turn_off",
|
|
|
|
|
self.coordinator.device_name,
|
|
|
|
|
self.property_id,
|
|
|
|
|
)
|
|
|
|
|
await self.async_call_api(
|
|
|
|
|
self.coordinator.api.post(self.property_id, self._min_speed)
|
|
|
|
|
)
|
|
|
|
|
|