mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Update esphome to use new fan entity model (#45590)
This commit is contained in:
parent
d148f9aa85
commit
e7ddaec468
@ -1,15 +1,11 @@
|
||||
"""Support for ESPHome fans."""
|
||||
from typing import List, Optional
|
||||
from typing import Optional
|
||||
|
||||
from aioesphomeapi import FanDirection, FanInfo, FanSpeed, FanState
|
||||
|
||||
from homeassistant.components.fan import (
|
||||
DIRECTION_FORWARD,
|
||||
DIRECTION_REVERSE,
|
||||
SPEED_HIGH,
|
||||
SPEED_LOW,
|
||||
SPEED_MEDIUM,
|
||||
SPEED_OFF,
|
||||
SUPPORT_DIRECTION,
|
||||
SUPPORT_OSCILLATE,
|
||||
SUPPORT_SET_SPEED,
|
||||
@ -17,6 +13,10 @@ from homeassistant.components.fan import (
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.util.percentage import (
|
||||
ordered_list_item_to_percentage,
|
||||
percentage_to_ordered_list_item,
|
||||
)
|
||||
|
||||
from . import (
|
||||
EsphomeEntity,
|
||||
@ -25,6 +25,8 @@ from . import (
|
||||
platform_async_setup_entry,
|
||||
)
|
||||
|
||||
ORDERED_NAMED_FAN_SPEEDS = [FanSpeed.LOW, FanSpeed.MEDIUM, FanSpeed.HIGH]
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
|
||||
@ -41,15 +43,6 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
|
||||
@esphome_map_enum
|
||||
def _fan_speeds():
|
||||
return {
|
||||
FanSpeed.LOW: SPEED_LOW,
|
||||
FanSpeed.MEDIUM: SPEED_MEDIUM,
|
||||
FanSpeed.HIGH: SPEED_HIGH,
|
||||
}
|
||||
|
||||
|
||||
@esphome_map_enum
|
||||
def _fan_directions():
|
||||
return {
|
||||
@ -69,23 +62,20 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
||||
def _state(self) -> Optional[FanState]:
|
||||
return super()._state
|
||||
|
||||
async def async_set_speed(self, speed: str) -> None:
|
||||
"""Set the speed of the fan."""
|
||||
if speed == SPEED_OFF:
|
||||
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
|
||||
|
||||
await self._client.fan_command(
|
||||
self._static_info.key, speed=_fan_speeds.from_hass(speed)
|
||||
)
|
||||
data = {"key": self._static_info.key, "state": True}
|
||||
if percentage is not None:
|
||||
named_speed = percentage_to_ordered_list_item(
|
||||
ORDERED_NAMED_FAN_SPEEDS, percentage
|
||||
)
|
||||
data["speed"] = named_speed
|
||||
await self._client.fan_command(**data)
|
||||
|
||||
#
|
||||
# The fan entity model has changed to use percentages and preset_modes
|
||||
# instead of speeds.
|
||||
#
|
||||
# Please review
|
||||
# https://developers.home-assistant.io/docs/core/entity/fan/
|
||||
#
|
||||
async def async_turn_on(
|
||||
self,
|
||||
speed: Optional[str] = None,
|
||||
@ -94,13 +84,7 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
||||
**kwargs,
|
||||
) -> None:
|
||||
"""Turn on the fan."""
|
||||
if speed == SPEED_OFF:
|
||||
await self.async_turn_off()
|
||||
return
|
||||
data = {"key": self._static_info.key, "state": True}
|
||||
if speed is not None:
|
||||
data["speed"] = _fan_speeds.from_hass(speed)
|
||||
await self._client.fan_command(**data)
|
||||
await self.async_set_percentage(percentage)
|
||||
|
||||
async def async_turn_off(self, **kwargs) -> None:
|
||||
"""Turn off the fan."""
|
||||
@ -127,11 +111,13 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
||||
return self._state.state
|
||||
|
||||
@esphome_state_property
|
||||
def speed(self) -> Optional[str]:
|
||||
"""Return the current speed."""
|
||||
def percentage(self) -> Optional[str]:
|
||||
"""Return the current speed percentage."""
|
||||
if not self._static_info.supports_speed:
|
||||
return None
|
||||
return _fan_speeds.from_esphome(self._state.speed)
|
||||
return ordered_list_item_to_percentage(
|
||||
ORDERED_NAMED_FAN_SPEEDS, self._state.speed
|
||||
)
|
||||
|
||||
@esphome_state_property
|
||||
def oscillating(self) -> None:
|
||||
@ -147,13 +133,6 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
||||
return None
|
||||
return _fan_directions.from_esphome(self._state.direction)
|
||||
|
||||
@property
|
||||
def speed_list(self) -> Optional[List[str]]:
|
||||
"""Get the list of available speeds."""
|
||||
if not self._static_info.supports_speed:
|
||||
return None
|
||||
return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
|
||||
|
||||
@property
|
||||
def supported_features(self) -> int:
|
||||
"""Flag supported features."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user