Update tuya for new fan entity model (#45870)
parent
17a4678906
commit
1845f69729
|
@ -10,6 +10,10 @@ from homeassistant.components.fan import (
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_PLATFORM, STATE_OFF
|
from homeassistant.const import CONF_PLATFORM, STATE_OFF
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
|
from homeassistant.util.percentage import (
|
||||||
|
ordered_list_item_to_percentage,
|
||||||
|
percentage_to_ordered_list_item,
|
||||||
|
)
|
||||||
|
|
||||||
from . import TuyaDevice
|
from . import TuyaDevice
|
||||||
from .const import DOMAIN, TUYA_DATA, TUYA_DISCOVERY_NEW
|
from .const import DOMAIN, TUYA_DATA, TUYA_DISCOVERY_NEW
|
||||||
|
@ -61,27 +65,21 @@ class TuyaFanDevice(TuyaDevice, FanEntity):
|
||||||
"""Init Tuya fan device."""
|
"""Init Tuya fan device."""
|
||||||
super().__init__(tuya, platform)
|
super().__init__(tuya, platform)
|
||||||
self.entity_id = ENTITY_ID_FORMAT.format(tuya.object_id())
|
self.entity_id = ENTITY_ID_FORMAT.format(tuya.object_id())
|
||||||
self.speeds = [STATE_OFF]
|
self.speeds = []
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Create fan list when add to hass."""
|
"""Create fan list when add to hass."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
self.speeds.extend(self._tuya.speed_list())
|
self.speeds.extend(self._tuya.speed_list())
|
||||||
|
|
||||||
def set_speed(self, speed: str) -> None:
|
def set_percentage(self, percentage: int) -> None:
|
||||||
"""Set the speed of the fan."""
|
"""Set the speed percentage of the fan."""
|
||||||
if speed == STATE_OFF:
|
if percentage == 0:
|
||||||
self.turn_off()
|
self.turn_off()
|
||||||
else:
|
else:
|
||||||
self._tuya.set_speed(speed)
|
tuya_speed = percentage_to_ordered_list_item(self.speeds, percentage)
|
||||||
|
self._tuya.set_speed(tuya_speed)
|
||||||
|
|
||||||
#
|
|
||||||
# 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/
|
|
||||||
#
|
|
||||||
def turn_on(
|
def turn_on(
|
||||||
self,
|
self,
|
||||||
speed: str = None,
|
speed: str = None,
|
||||||
|
@ -90,8 +88,8 @@ class TuyaFanDevice(TuyaDevice, FanEntity):
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Turn on the fan."""
|
"""Turn on the fan."""
|
||||||
if speed is not None:
|
if percentage is not None:
|
||||||
self.set_speed(speed)
|
self.set_percentage(percentage)
|
||||||
else:
|
else:
|
||||||
self._tuya.turn_on()
|
self._tuya.turn_on()
|
||||||
|
|
||||||
|
@ -118,16 +116,13 @@ class TuyaFanDevice(TuyaDevice, FanEntity):
|
||||||
return self._tuya.state()
|
return self._tuya.state()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def speed(self) -> str:
|
def percentage(self) -> str:
|
||||||
"""Return the current speed."""
|
"""Return the current speed."""
|
||||||
if self.is_on:
|
if not self.is_on:
|
||||||
return self._tuya.speed()
|
return 0
|
||||||
return STATE_OFF
|
if self.speeds is None:
|
||||||
|
return None
|
||||||
@property
|
return ordered_list_item_to_percentage(self.speeds, self._tuya.speed())
|
||||||
def speed_list(self) -> list:
|
|
||||||
"""Get the list of available speeds."""
|
|
||||||
return self.speeds
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self) -> int:
|
def supported_features(self) -> int:
|
||||||
|
|
Loading…
Reference in New Issue