Update wilight for new fan entity model (#45869)

This commit is contained in:
J. Nick Koston 2021-02-10 12:49:52 -10:00 committed by GitHub
parent d8a2e0e051
commit 281fbe1dfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,19 +14,20 @@ from pywilight.const import (
from homeassistant.components.fan import ( from homeassistant.components.fan import (
DIRECTION_FORWARD, DIRECTION_FORWARD,
SPEED_HIGH,
SPEED_LOW,
SPEED_MEDIUM,
SUPPORT_DIRECTION, SUPPORT_DIRECTION,
SUPPORT_SET_SPEED, SUPPORT_SET_SPEED,
FanEntity, FanEntity,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.util.percentage import (
ordered_list_item_to_percentage,
percentage_to_ordered_list_item,
)
from . import WiLightDevice from . import WiLightDevice
SUPPORTED_SPEEDS = [SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH] ORDERED_NAMED_FAN_SPEEDS = [WL_SPEED_LOW, WL_SPEED_MEDIUM, WL_SPEED_HIGH]
SUPPORTED_FEATURES = SUPPORT_SET_SPEED | SUPPORT_DIRECTION SUPPORTED_FEATURES = SUPPORT_SET_SPEED | SUPPORT_DIRECTION
@ -77,14 +78,12 @@ class WiLightFan(WiLightDevice, FanEntity):
return self._status.get("direction", WL_DIRECTION_OFF) != WL_DIRECTION_OFF return self._status.get("direction", WL_DIRECTION_OFF) != WL_DIRECTION_OFF
@property @property
def speed(self) -> str: def percentage(self) -> str:
"""Return the current speed.""" """Return the current speed percentage."""
return self._status.get("speed", SPEED_HIGH) wl_speed = self._status.get("speed")
if wl_speed is None:
@property return None
def speed_list(self) -> list: return ordered_list_item_to_percentage(ORDERED_NAMED_FAN_SPEEDS, wl_speed)
"""Get the list of available speeds."""
return SUPPORTED_SPEEDS
@property @property
def current_direction(self) -> str: def current_direction(self) -> str:
@ -94,13 +93,6 @@ class WiLightFan(WiLightDevice, FanEntity):
self._direction = self._status["direction"] self._direction = self._status["direction"]
return self._direction return self._direction
#
# 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: str = None, speed: str = None,
@ -109,18 +101,17 @@ class WiLightFan(WiLightDevice, FanEntity):
**kwargs, **kwargs,
) -> None: ) -> None:
"""Turn on the fan.""" """Turn on the fan."""
if speed is None: if percentage is None:
await self._client.set_fan_direction(self._index, self._direction) await self._client.set_fan_direction(self._index, self._direction)
else: else:
await self.async_set_speed(speed) await self.async_set_percentage(percentage)
async def async_set_speed(self, speed: str): async def async_set_percentage(self, percentage: int):
"""Set the speed of the fan.""" """Set the speed of the fan."""
wl_speed = WL_SPEED_HIGH if percentage == 0:
if speed == SPEED_LOW: await self._client.set_fan_direction(self._index, WL_DIRECTION_OFF)
wl_speed = WL_SPEED_LOW return
if speed == SPEED_MEDIUM: wl_speed = percentage_to_ordered_list_item(ORDERED_NAMED_FAN_SPEEDS, percentage)
wl_speed = WL_SPEED_MEDIUM
await self._client.set_fan_speed(self._index, wl_speed) await self._client.set_fan_speed(self._index, wl_speed)
async def async_set_direction(self, direction: str): async def async_set_direction(self, direction: str):