2018-12-17 20:46:57 +01:00
|
|
|
"""Support for ESPHome fans."""
|
2021-03-17 23:49:01 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-03-17 08:50:34 +01:00
|
|
|
import math
|
2021-07-12 22:56:10 +02:00
|
|
|
from typing import Any
|
2019-05-29 13:33:49 +02:00
|
|
|
|
2021-01-27 10:40:33 +01:00
|
|
|
from aioesphomeapi import FanDirection, FanInfo, FanSpeed, FanState
|
2018-12-17 20:46:57 +01:00
|
|
|
|
2019-03-20 22:56:46 -07:00
|
|
|
from homeassistant.components.fan import (
|
2021-01-27 10:40:33 +01:00
|
|
|
DIRECTION_FORWARD,
|
|
|
|
DIRECTION_REVERSE,
|
2019-07-31 12:25:30 -07:00
|
|
|
FanEntity,
|
2022-04-06 11:52:59 +02:00
|
|
|
FanEntityFeature,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2018-12-17 20:46:57 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-04-21 12:18:42 +02:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-07-12 22:56:10 +02:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-01-28 10:25:08 -06:00
|
|
|
from homeassistant.util.percentage import (
|
|
|
|
ordered_list_item_to_percentage,
|
|
|
|
percentage_to_ordered_list_item,
|
2021-03-17 08:50:34 +01:00
|
|
|
percentage_to_ranged_value,
|
|
|
|
ranged_value_to_percentage,
|
2021-01-28 10:25:08 -06:00
|
|
|
)
|
2018-12-17 20:46:57 +01:00
|
|
|
|
2019-05-29 13:33:49 +02:00
|
|
|
from . import (
|
2019-07-31 12:25:30 -07:00
|
|
|
EsphomeEntity,
|
2021-06-22 06:22:38 +02:00
|
|
|
EsphomeEnumMapper,
|
2019-07-31 12:25:30 -07:00
|
|
|
esphome_state_property,
|
|
|
|
platform_async_setup_entry,
|
|
|
|
)
|
2018-12-17 20:46:57 +01:00
|
|
|
|
2021-01-28 10:25:08 -06:00
|
|
|
ORDERED_NAMED_FAN_SPEEDS = [FanSpeed.LOW, FanSpeed.MEDIUM, FanSpeed.HIGH]
|
|
|
|
|
2018-12-17 20:46:57 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
async def async_setup_entry(
|
2021-07-12 22:56:10 +02:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2019-07-31 12:25:30 -07:00
|
|
|
) -> None:
|
2018-12-17 20:46:57 +01:00
|
|
|
"""Set up ESPHome fans based on a config entry."""
|
|
|
|
await platform_async_setup_entry(
|
2019-07-31 12:25:30 -07:00
|
|
|
hass,
|
|
|
|
entry,
|
|
|
|
async_add_entities,
|
|
|
|
component_key="fan",
|
|
|
|
info_type=FanInfo,
|
|
|
|
entity_type=EsphomeFan,
|
|
|
|
state_type=FanState,
|
2018-12-17 20:46:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
_FAN_DIRECTIONS: EsphomeEnumMapper[FanDirection, str] = EsphomeEnumMapper(
|
2021-06-22 06:22:38 +02:00
|
|
|
{
|
2021-01-27 10:40:33 +01:00
|
|
|
FanDirection.FORWARD: DIRECTION_FORWARD,
|
|
|
|
FanDirection.REVERSE: DIRECTION_REVERSE,
|
|
|
|
}
|
2021-06-22 06:22:38 +02:00
|
|
|
)
|
2021-01-27 10:40:33 +01:00
|
|
|
|
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
class EsphomeFan(EsphomeEntity[FanInfo, FanState], FanEntity):
|
|
|
|
"""A fan implementation for ESPHome."""
|
2018-12-17 20:46:57 +01:00
|
|
|
|
2021-03-17 08:50:34 +01:00
|
|
|
@property
|
|
|
|
def _supports_speed_levels(self) -> bool:
|
2021-06-28 13:43:45 +02:00
|
|
|
api_version = self._api_version
|
2021-03-17 08:50:34 +01:00
|
|
|
return api_version.major == 1 and api_version.minor > 3
|
|
|
|
|
2022-06-22 18:22:01 +02:00
|
|
|
async def async_set_percentage(self, percentage: int) -> None:
|
2021-01-28 10:25:08 -06:00
|
|
|
"""Set the speed percentage of the fan."""
|
2022-06-22 18:22:01 +02:00
|
|
|
await self._async_set_percentage(percentage)
|
|
|
|
|
|
|
|
async def _async_set_percentage(self, percentage: int | None) -> None:
|
2021-01-28 10:25:08 -06:00
|
|
|
if percentage == 0:
|
2018-12-17 20:46:57 +01:00
|
|
|
await self.async_turn_off()
|
|
|
|
return
|
2019-04-08 15:44:24 +02:00
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
data: dict[str, Any] = {"key": self._static_info.key, "state": True}
|
2021-01-28 10:25:08 -06:00
|
|
|
if percentage is not None:
|
2021-03-17 08:50:34 +01:00
|
|
|
if self._supports_speed_levels:
|
|
|
|
data["speed_level"] = math.ceil(
|
|
|
|
percentage_to_ranged_value(
|
|
|
|
(1, self._static_info.supported_speed_levels), percentage
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
named_speed = percentage_to_ordered_list_item(
|
|
|
|
ORDERED_NAMED_FAN_SPEEDS, percentage
|
|
|
|
)
|
|
|
|
data["speed"] = named_speed
|
2021-01-28 10:25:08 -06:00
|
|
|
await self._client.fan_command(**data)
|
2018-12-17 20:46:57 +01:00
|
|
|
|
2021-01-27 17:44:36 -06:00
|
|
|
async def async_turn_on(
|
|
|
|
self,
|
2021-03-17 23:49:01 +01:00
|
|
|
percentage: int | None = None,
|
|
|
|
preset_mode: str | None = None,
|
2021-07-12 22:56:10 +02:00
|
|
|
**kwargs: Any,
|
2021-01-27 17:44:36 -06:00
|
|
|
) -> None:
|
2018-12-17 20:46:57 +01:00
|
|
|
"""Turn on the fan."""
|
2022-06-22 18:22:01 +02:00
|
|
|
await self._async_set_percentage(percentage)
|
2018-12-17 20:46:57 +01:00
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2018-12-17 20:46:57 +01:00
|
|
|
"""Turn off the fan."""
|
|
|
|
await self._client.fan_command(key=self._static_info.key, state=False)
|
|
|
|
|
2019-04-16 22:48:46 +02:00
|
|
|
async def async_oscillate(self, oscillating: bool) -> None:
|
2018-12-17 20:46:57 +01:00
|
|
|
"""Oscillate the fan."""
|
2019-07-31 12:25:30 -07:00
|
|
|
await self._client.fan_command(
|
|
|
|
key=self._static_info.key, oscillating=oscillating
|
|
|
|
)
|
2018-12-17 20:46:57 +01:00
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
async def async_set_direction(self, direction: str) -> None:
|
2021-01-27 10:40:33 +01:00
|
|
|
"""Set direction of the fan."""
|
|
|
|
await self._client.fan_command(
|
2021-06-22 06:22:38 +02:00
|
|
|
key=self._static_info.key, direction=_FAN_DIRECTIONS.from_hass(direction)
|
2021-01-27 10:40:33 +01:00
|
|
|
)
|
|
|
|
|
2022-09-26 22:10:06 +02:00
|
|
|
@property
|
2019-04-16 22:48:46 +02:00
|
|
|
@esphome_state_property
|
2022-01-23 11:31:01 +01:00
|
|
|
def is_on(self) -> bool | None:
|
2018-12-17 20:46:57 +01:00
|
|
|
"""Return true if the entity is on."""
|
|
|
|
return self._state.state
|
|
|
|
|
2022-09-26 22:10:06 +02:00
|
|
|
@property
|
2019-04-16 22:48:46 +02:00
|
|
|
@esphome_state_property
|
2021-03-17 23:49:01 +01:00
|
|
|
def percentage(self) -> int | None:
|
2021-01-28 10:25:08 -06:00
|
|
|
"""Return the current speed percentage."""
|
2019-01-13 15:52:23 +01:00
|
|
|
if not self._static_info.supports_speed:
|
|
|
|
return None
|
2021-03-17 08:50:34 +01:00
|
|
|
|
|
|
|
if not self._supports_speed_levels:
|
|
|
|
return ordered_list_item_to_percentage(
|
2021-07-12 22:56:10 +02:00
|
|
|
ORDERED_NAMED_FAN_SPEEDS, self._state.speed # type: ignore[misc]
|
2021-03-17 08:50:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return ranged_value_to_percentage(
|
|
|
|
(1, self._static_info.supported_speed_levels), self._state.speed_level
|
2021-01-28 10:25:08 -06:00
|
|
|
)
|
2018-12-17 20:46:57 +01:00
|
|
|
|
2021-02-18 21:05:09 -10:00
|
|
|
@property
|
2021-02-19 19:57:21 -10:00
|
|
|
def speed_count(self) -> int:
|
2021-02-18 21:05:09 -10:00
|
|
|
"""Return the number of speeds the fan supports."""
|
2021-03-17 08:50:34 +01:00
|
|
|
if not self._supports_speed_levels:
|
|
|
|
return len(ORDERED_NAMED_FAN_SPEEDS)
|
|
|
|
return self._static_info.supported_speed_levels
|
2021-02-18 21:05:09 -10:00
|
|
|
|
2022-09-26 22:10:06 +02:00
|
|
|
@property
|
2019-04-16 22:48:46 +02:00
|
|
|
@esphome_state_property
|
2021-06-22 06:22:38 +02:00
|
|
|
def oscillating(self) -> bool | None:
|
2018-12-17 20:46:57 +01:00
|
|
|
"""Return the oscillation state."""
|
2019-01-13 15:52:23 +01:00
|
|
|
if not self._static_info.supports_oscillation:
|
|
|
|
return None
|
2018-12-17 20:46:57 +01:00
|
|
|
return self._state.oscillating
|
|
|
|
|
2022-09-26 22:10:06 +02:00
|
|
|
@property
|
2021-01-27 10:40:33 +01:00
|
|
|
@esphome_state_property
|
2021-06-22 06:22:38 +02:00
|
|
|
def current_direction(self) -> str | None:
|
2021-01-27 10:40:33 +01:00
|
|
|
"""Return the current fan direction."""
|
|
|
|
if not self._static_info.supports_direction:
|
|
|
|
return None
|
2021-06-22 06:22:38 +02:00
|
|
|
return _FAN_DIRECTIONS.from_esphome(self._state.direction)
|
2021-01-27 10:40:33 +01:00
|
|
|
|
2018-12-17 20:46:57 +01:00
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
|
|
|
flags = 0
|
|
|
|
if self._static_info.supports_oscillation:
|
2022-04-06 11:52:59 +02:00
|
|
|
flags |= FanEntityFeature.OSCILLATE
|
2018-12-17 20:46:57 +01:00
|
|
|
if self._static_info.supports_speed:
|
2022-04-06 11:52:59 +02:00
|
|
|
flags |= FanEntityFeature.SET_SPEED
|
2021-01-27 10:40:33 +01:00
|
|
|
if self._static_info.supports_direction:
|
2022-04-06 11:52:59 +02:00
|
|
|
flags |= FanEntityFeature.DIRECTION
|
2018-12-17 20:46:57 +01:00
|
|
|
return flags
|