mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +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."""
|
"""Support for ESPHome fans."""
|
||||||
from typing import List, Optional
|
from typing import Optional
|
||||||
|
|
||||||
from aioesphomeapi import FanDirection, FanInfo, FanSpeed, FanState
|
from aioesphomeapi import FanDirection, FanInfo, FanSpeed, FanState
|
||||||
|
|
||||||
from homeassistant.components.fan import (
|
from homeassistant.components.fan import (
|
||||||
DIRECTION_FORWARD,
|
DIRECTION_FORWARD,
|
||||||
DIRECTION_REVERSE,
|
DIRECTION_REVERSE,
|
||||||
SPEED_HIGH,
|
|
||||||
SPEED_LOW,
|
|
||||||
SPEED_MEDIUM,
|
|
||||||
SPEED_OFF,
|
|
||||||
SUPPORT_DIRECTION,
|
SUPPORT_DIRECTION,
|
||||||
SUPPORT_OSCILLATE,
|
SUPPORT_OSCILLATE,
|
||||||
SUPPORT_SET_SPEED,
|
SUPPORT_SET_SPEED,
|
||||||
@ -17,6 +13,10 @@ from homeassistant.components.fan import (
|
|||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.helpers.typing import HomeAssistantType
|
from homeassistant.helpers.typing import HomeAssistantType
|
||||||
|
from homeassistant.util.percentage import (
|
||||||
|
ordered_list_item_to_percentage,
|
||||||
|
percentage_to_ordered_list_item,
|
||||||
|
)
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
EsphomeEntity,
|
EsphomeEntity,
|
||||||
@ -25,6 +25,8 @@ from . import (
|
|||||||
platform_async_setup_entry,
|
platform_async_setup_entry,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ORDERED_NAMED_FAN_SPEEDS = [FanSpeed.LOW, FanSpeed.MEDIUM, FanSpeed.HIGH]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
|
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
|
@esphome_map_enum
|
||||||
def _fan_directions():
|
def _fan_directions():
|
||||||
return {
|
return {
|
||||||
@ -69,23 +62,20 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
|||||||
def _state(self) -> Optional[FanState]:
|
def _state(self) -> Optional[FanState]:
|
||||||
return super()._state
|
return super()._state
|
||||||
|
|
||||||
async def async_set_speed(self, speed: str) -> None:
|
async def async_set_percentage(self, percentage: int) -> None:
|
||||||
"""Set the speed of the fan."""
|
"""Set the speed percentage of the fan."""
|
||||||
if speed == SPEED_OFF:
|
if percentage == 0:
|
||||||
await self.async_turn_off()
|
await self.async_turn_off()
|
||||||
return
|
return
|
||||||
|
|
||||||
await self._client.fan_command(
|
data = {"key": self._static_info.key, "state": True}
|
||||||
self._static_info.key, speed=_fan_speeds.from_hass(speed)
|
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(
|
async def async_turn_on(
|
||||||
self,
|
self,
|
||||||
speed: Optional[str] = None,
|
speed: Optional[str] = None,
|
||||||
@ -94,13 +84,7 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
|||||||
**kwargs,
|
**kwargs,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Turn on the fan."""
|
"""Turn on the fan."""
|
||||||
if speed == SPEED_OFF:
|
await self.async_set_percentage(percentage)
|
||||||
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)
|
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs) -> None:
|
async def async_turn_off(self, **kwargs) -> None:
|
||||||
"""Turn off the fan."""
|
"""Turn off the fan."""
|
||||||
@ -127,11 +111,13 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
|||||||
return self._state.state
|
return self._state.state
|
||||||
|
|
||||||
@esphome_state_property
|
@esphome_state_property
|
||||||
def speed(self) -> Optional[str]:
|
def percentage(self) -> Optional[str]:
|
||||||
"""Return the current speed."""
|
"""Return the current speed percentage."""
|
||||||
if not self._static_info.supports_speed:
|
if not self._static_info.supports_speed:
|
||||||
return None
|
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
|
@esphome_state_property
|
||||||
def oscillating(self) -> None:
|
def oscillating(self) -> None:
|
||||||
@ -147,13 +133,6 @@ class EsphomeFan(EsphomeEntity, FanEntity):
|
|||||||
return None
|
return None
|
||||||
return _fan_directions.from_esphome(self._state.direction)
|
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
|
@property
|
||||||
def supported_features(self) -> int:
|
def supported_features(self) -> int:
|
||||||
"""Flag supported features."""
|
"""Flag supported features."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user