core/homeassistant/components/esphome/fan.py

171 lines
5.3 KiB
Python
Raw Normal View History

"""Support for ESPHome fans."""
2021-03-17 22:49:01 +00:00
from __future__ import annotations
import math
from typing import Any
2021-01-27 09:40:33 +00:00
from aioesphomeapi import FanDirection, FanInfo, FanSpeed, FanState
from homeassistant.components.fan import (
2021-01-27 09:40:33 +00:00
DIRECTION_FORWARD,
DIRECTION_REVERSE,
SUPPORT_DIRECTION,
2019-07-31 19:25:30 +00:00
SUPPORT_OSCILLATE,
SUPPORT_SET_SPEED,
FanEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
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 (
2019-07-31 19:25:30 +00:00
EsphomeEntity,
EsphomeEnumMapper,
2019-07-31 19:25:30 +00:00
esphome_state_property,
platform_async_setup_entry,
)
ORDERED_NAMED_FAN_SPEEDS = [FanSpeed.LOW, FanSpeed.MEDIUM, FanSpeed.HIGH]
2019-07-31 19:25:30 +00:00
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
2019-07-31 19:25:30 +00:00
) -> None:
"""Set up ESPHome fans based on a config entry."""
await platform_async_setup_entry(
2019-07-31 19:25:30 +00:00
hass,
entry,
async_add_entities,
component_key="fan",
info_type=FanInfo,
entity_type=EsphomeFan,
state_type=FanState,
)
_FAN_DIRECTIONS: EsphomeEnumMapper[FanDirection, str] = EsphomeEnumMapper(
{
2021-01-27 09:40:33 +00:00
FanDirection.FORWARD: DIRECTION_FORWARD,
FanDirection.REVERSE: DIRECTION_REVERSE,
}
)
2021-01-27 09:40:33 +00:00
# https://github.com/PyCQA/pylint/issues/3150 for all @esphome_state_property
# pylint: disable=invalid-overridden-method
class EsphomeFan(EsphomeEntity[FanInfo, FanState], FanEntity):
"""A fan implementation for ESPHome."""
@property
def _supports_speed_levels(self) -> bool:
api_version = self._api_version
return api_version.major == 1 and api_version.minor > 3
async def async_set_percentage(self, percentage: int | None) -> None:
"""Set the speed percentage of the fan."""
if percentage == 0:
await self.async_turn_off()
return
Add ESPHome Cover position/tilt support (#22858) ## Description: Add ESPHome cover position and tilt support. The aioesphomeapi also received a small refactor for these changes and those are part of this PR (constants were refactored into enums and optimistic was renamed to assumed_state). If possible, I'd like to include those in this PR because: 1. It's mostly just very simple changes 2. Because of the new position change the dev branch would be in a non-working state for a while until the split PR is merged (unless I write some temporary glue logic, but I'd prefer to avoid that) ## Checklist: - [x] The code change is tested and works locally. - [x] Local tests pass with `tox`. **Your PR cannot be merged unless tests pass** - [x] There is no commented out code in this PR. If the code communicates with devices, web services, or third-party tools: - [x] [_The manifest file_][manifest-docs] has all fields filled out correctly ([example][ex-manifest]). - [x] New dependencies have been added to `requirements` in the manifest ([example][ex-requir]). - [x] New or updated dependencies have been added to `requirements_all.txt` by running `script/gen_requirements_all.py`. [ex-manifest]: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/mobile_app/manifest.json [ex-requir]: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/mobile_app/manifest.json#L5 [ex-import]: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/keyboard/__init__.py#L23 [manifest-docs]: https://developers.home-assistant.io/docs/en/development_checklist.html#_the-manifest-file_
2019-04-08 13:44:24 +00:00
data: dict[str, Any] = {"key": self._static_info.key, "state": True}
if percentage is not None:
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
await self._client.fan_command(**data)
async def async_turn_on(
self,
2021-03-17 22:49:01 +00:00
speed: str | None = None,
percentage: int | None = None,
preset_mode: str | None = None,
**kwargs: Any,
) -> None:
"""Turn on the fan."""
await self.async_set_percentage(percentage)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the fan."""
await self._client.fan_command(key=self._static_info.key, state=False)
async def async_oscillate(self, oscillating: bool) -> None:
"""Oscillate the fan."""
2019-07-31 19:25:30 +00:00
await self._client.fan_command(
key=self._static_info.key, oscillating=oscillating
)
async def async_set_direction(self, direction: str) -> None:
2021-01-27 09:40:33 +00:00
"""Set direction of the fan."""
await self._client.fan_command(
key=self._static_info.key, direction=_FAN_DIRECTIONS.from_hass(direction)
2021-01-27 09:40:33 +00:00
)
@esphome_state_property
def is_on(self) -> bool | None: # type: ignore[override]
"""Return true if the entity is on."""
return self._state.state
@esphome_state_property
2021-03-17 22:49:01 +00:00
def percentage(self) -> int | None:
"""Return the current speed percentage."""
if not self._static_info.supports_speed:
return None
if not self._supports_speed_levels:
return ordered_list_item_to_percentage(
ORDERED_NAMED_FAN_SPEEDS, self._state.speed # type: ignore[misc]
)
return ranged_value_to_percentage(
(1, self._static_info.supported_speed_levels), self._state.speed_level
)
@property
def speed_count(self) -> int:
"""Return the number of speeds the fan supports."""
if not self._supports_speed_levels:
return len(ORDERED_NAMED_FAN_SPEEDS)
return self._static_info.supported_speed_levels
@esphome_state_property
def oscillating(self) -> bool | None:
"""Return the oscillation state."""
if not self._static_info.supports_oscillation:
return None
return self._state.oscillating
2021-01-27 09:40:33 +00:00
@esphome_state_property
def current_direction(self) -> str | None:
2021-01-27 09:40:33 +00:00
"""Return the current fan direction."""
if not self._static_info.supports_direction:
return None
return _FAN_DIRECTIONS.from_esphome(self._state.direction)
2021-01-27 09:40:33 +00:00
@property
def supported_features(self) -> int:
"""Flag supported features."""
flags = 0
if self._static_info.supports_oscillation:
flags |= SUPPORT_OSCILLATE
if self._static_info.supports_speed:
flags |= SUPPORT_SET_SPEED
2021-01-27 09:40:33 +00:00
if self._static_info.supports_direction:
flags |= SUPPORT_DIRECTION
return flags