2018-12-17 19:46:57 +00:00
|
|
|
"""Support for ESPHome fans."""
|
|
|
|
import logging
|
2019-05-29 11:33:49 +00:00
|
|
|
from typing import List, Optional
|
|
|
|
|
|
|
|
from aioesphomeapi import FanInfo, FanSpeed, FanState
|
2018-12-17 19:46:57 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.components.fan import (
|
|
|
|
SPEED_HIGH, SPEED_LOW, SPEED_MEDIUM, SPEED_OFF, SUPPORT_OSCILLATE,
|
|
|
|
SUPPORT_SET_SPEED, FanEntity)
|
2018-12-17 19:46:57 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
|
|
|
|
2019-05-29 11:33:49 +00:00
|
|
|
from . import (
|
|
|
|
EsphomeEntity, esphome_map_enum, esphome_state_property,
|
|
|
|
platform_async_setup_entry)
|
2018-12-17 19:46:57 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistantType,
|
|
|
|
entry: ConfigEntry, async_add_entities) -> None:
|
|
|
|
"""Set up ESPHome fans based on a config entry."""
|
|
|
|
await platform_async_setup_entry(
|
|
|
|
hass, entry, async_add_entities,
|
|
|
|
component_key='fan',
|
|
|
|
info_type=FanInfo, entity_type=EsphomeFan,
|
|
|
|
state_type=FanState
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-04-16 20:48:46 +00:00
|
|
|
@esphome_map_enum
|
|
|
|
def _fan_speeds():
|
2019-04-08 13:44:24 +00:00
|
|
|
return {
|
|
|
|
FanSpeed.LOW: SPEED_LOW,
|
|
|
|
FanSpeed.MEDIUM: SPEED_MEDIUM,
|
|
|
|
FanSpeed.HIGH: SPEED_HIGH,
|
2019-04-16 20:48:46 +00:00
|
|
|
}
|
2018-12-17 19:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EsphomeFan(EsphomeEntity, FanEntity):
|
|
|
|
"""A fan implementation for ESPHome."""
|
|
|
|
|
|
|
|
@property
|
2019-05-29 11:33:49 +00:00
|
|
|
def _static_info(self) -> FanInfo:
|
2018-12-17 19:46:57 +00:00
|
|
|
return super()._static_info
|
|
|
|
|
|
|
|
@property
|
2019-05-29 11:33:49 +00:00
|
|
|
def _state(self) -> Optional[FanState]:
|
2018-12-17 19:46:57 +00:00
|
|
|
return super()._state
|
|
|
|
|
|
|
|
async def async_set_speed(self, speed: str) -> None:
|
|
|
|
"""Set the speed of the fan."""
|
|
|
|
if speed == SPEED_OFF:
|
|
|
|
await self.async_turn_off()
|
|
|
|
return
|
2019-04-08 13:44:24 +00:00
|
|
|
|
2018-12-17 19:46:57 +00:00
|
|
|
await self._client.fan_command(
|
2019-04-16 20:48:46 +00:00
|
|
|
self._static_info.key, speed=_fan_speeds.from_hass(speed))
|
2018-12-17 19:46:57 +00:00
|
|
|
|
|
|
|
async def async_turn_on(self, speed: Optional[str] = None,
|
|
|
|
**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:
|
2019-04-16 20:48:46 +00:00
|
|
|
data['speed'] = _fan_speeds.from_hass(speed)
|
2018-12-17 19:46:57 +00:00
|
|
|
await self._client.fan_command(**data)
|
|
|
|
|
|
|
|
# pylint: disable=arguments-differ
|
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
|
|
|
"""Turn off the fan."""
|
|
|
|
await self._client.fan_command(key=self._static_info.key, state=False)
|
|
|
|
|
2019-04-16 20:48:46 +00:00
|
|
|
async def async_oscillate(self, oscillating: bool) -> None:
|
2018-12-17 19:46:57 +00:00
|
|
|
"""Oscillate the fan."""
|
|
|
|
await self._client.fan_command(key=self._static_info.key,
|
|
|
|
oscillating=oscillating)
|
|
|
|
|
2019-04-16 20:48:46 +00:00
|
|
|
@esphome_state_property
|
2018-12-17 19:46:57 +00:00
|
|
|
def is_on(self) -> Optional[bool]:
|
|
|
|
"""Return true if the entity is on."""
|
|
|
|
return self._state.state
|
|
|
|
|
2019-04-16 20:48:46 +00:00
|
|
|
@esphome_state_property
|
2018-12-17 19:46:57 +00:00
|
|
|
def speed(self) -> Optional[str]:
|
|
|
|
"""Return the current speed."""
|
2019-01-13 14:52:23 +00:00
|
|
|
if not self._static_info.supports_speed:
|
|
|
|
return None
|
2019-04-16 20:48:46 +00:00
|
|
|
return _fan_speeds.from_esphome(self._state.speed)
|
2018-12-17 19:46:57 +00:00
|
|
|
|
2019-04-16 20:48:46 +00:00
|
|
|
@esphome_state_property
|
2018-12-17 19:46:57 +00:00
|
|
|
def oscillating(self) -> None:
|
|
|
|
"""Return the oscillation state."""
|
2019-01-13 14:52:23 +00:00
|
|
|
if not self._static_info.supports_oscillation:
|
|
|
|
return None
|
2018-12-17 19:46:57 +00:00
|
|
|
return self._state.oscillating
|
|
|
|
|
|
|
|
@property
|
2019-01-13 14:52:23 +00:00
|
|
|
def speed_list(self) -> Optional[List[str]]:
|
2018-12-17 19:46:57 +00:00
|
|
|
"""Get the list of available speeds."""
|
2019-01-13 14:52:23 +00:00
|
|
|
if not self._static_info.supports_speed:
|
|
|
|
return None
|
2018-12-17 19:46:57 +00:00
|
|
|
return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
|
|
|
|
|
|
|
|
@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
|
|
|
|
return flags
|