Convert fan platform to asyncio

This commit is contained in:
Pierre Ståhl
2020-10-14 13:45:41 +02:00
committed by rospogrigio
parent 136e44c549
commit 5d64cb4e18

View File

@@ -1,113 +1,111 @@
"""Platform to locally control Tuya-based fan devices.""" """Platform to locally control Tuya-based fan devices."""
import logging import logging
from functools import partial from functools import partial
from homeassistant.components.fan import ( from homeassistant.components.fan import (
FanEntity, FanEntity,
DOMAIN, DOMAIN,
SPEED_OFF, SPEED_OFF,
SPEED_LOW, SPEED_LOW,
SPEED_MEDIUM, SPEED_MEDIUM,
SPEED_HIGH, SPEED_HIGH,
SUPPORT_SET_SPEED, SUPPORT_SET_SPEED,
SUPPORT_OSCILLATE, SUPPORT_OSCILLATE,
) )
from .common import LocalTuyaEntity, async_setup_entry from .common import LocalTuyaEntity, async_setup_entry
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
def flow_schema(dps): def flow_schema(dps):
"""Return schema used in config flow.""" """Return schema used in config flow."""
return {} return {}
class LocaltuyaFan(LocalTuyaEntity, FanEntity): class LocaltuyaFan(LocalTuyaEntity, FanEntity):
"""Representation of a Tuya fan.""" """Representation of a Tuya fan."""
def __init__( def __init__(
self, self,
device, device,
config_entry, config_entry,
fanid, fanid,
**kwargs, **kwargs,
): ):
"""Initialize the entity.""" """Initialize the entity."""
super().__init__(device, config_entry, fanid, **kwargs) super().__init__(device, config_entry, fanid, **kwargs)
self._is_on = False self._is_on = False
self._speed = SPEED_OFF self._speed = None
self._oscillating = False self._oscillating = None
@property @property
def oscillating(self): def oscillating(self):
"""Return current oscillating status.""" """Return current oscillating status."""
return self._oscillating return self._oscillating
@property @property
def is_on(self): def is_on(self):
"""Check if Tuya fan is on.""" """Check if Tuya fan is on."""
return self._is_on return self._is_on
@property @property
def speed(self) -> str: def speed(self) -> str:
"""Return the current speed.""" """Return the current speed."""
return self._speed return self._speed
@property @property
def speed_list(self) -> list: def speed_list(self) -> list:
"""Get the list of available speeds.""" """Get the list of available speeds."""
return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH] return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
def turn_on(self, speed: str = None, **kwargs) -> None: async def async_turn_on(self, speed: str = None, **kwargs) -> None:
"""Turn on the entity.""" """Turn on the entity."""
self._device.set_dps(True, "1") await self._device.set_dps(True, "1")
if speed is not None: if speed is not None:
self.set_speed(speed) await self.async_set_speed(speed)
else: else:
self.schedule_update_ha_state() self.schedule_update_ha_state()
def turn_off(self, **kwargs) -> None: async def async_turn_off(self, **kwargs) -> None:
"""Turn off the entity.""" """Turn off the entity."""
self._device.set_dps(False, "1") await self._device.set_dps(False, "1")
self.schedule_update_ha_state() self.schedule_update_ha_state()
def set_speed(self, speed: str) -> None: async def async_set_speed(self, speed: str) -> None:
"""Set the speed of the fan.""" """Set the speed of the fan."""
self._speed = speed if speed == SPEED_OFF:
if speed == SPEED_OFF: await self._device.set_dps(False, "1")
self._device.set_dps(False, "1") elif speed == SPEED_LOW:
elif speed == SPEED_LOW: await self._device.set_dps("1", "2")
self._device.set_dps("1", "2") elif speed == SPEED_MEDIUM:
elif speed == SPEED_MEDIUM: await self._device.set_dps("2", "2")
self._device.set_dps("2", "2") elif speed == SPEED_HIGH:
elif speed == SPEED_HIGH: await self._device.set_dps("3", "2")
self._device.set_dps("3", "2") self.schedule_update_ha_state()
self.schedule_update_ha_state()
async def async_oscillate(self, oscillating: bool) -> None:
def oscillate(self, oscillating: bool) -> None: """Set oscillation."""
"""Set oscillation.""" await self._device.set_dps(oscillating, "8")
self._oscillating = oscillating self.schedule_update_ha_state()
self._device.set_value("8", oscillating)
self.schedule_update_ha_state() @property
def supported_features(self) -> int:
@property """Flag supported features."""
def supported_features(self) -> int: return SUPPORT_SET_SPEED | SUPPORT_OSCILLATE
"""Flag supported features."""
return SUPPORT_SET_SPEED | SUPPORT_OSCILLATE def status_updated(self):
"""Get state of Tuya fan."""
def status_updated(self): self._is_on = self._status["dps"]["1"]
"""Get state of Tuya fan.""" if not self._status["dps"]["1"]:
self._is_on = self._status["dps"]["1"] self._speed = SPEED_OFF
if not self._status["dps"]["1"]: elif self._status["dps"]["2"] == "1":
self._speed = SPEED_OFF self._speed = SPEED_LOW
elif self._status["dps"]["2"] == "1": elif self._status["dps"]["2"] == "2":
self._speed = SPEED_LOW self._speed = SPEED_MEDIUM
elif self._status["dps"]["2"] == "2": elif self._status["dps"]["2"] == "3":
self._speed = SPEED_MEDIUM self._speed = SPEED_HIGH
elif self._status["dps"]["2"] == "3": self._oscillating = self._status["dps"]["8"]
self._speed = SPEED_HIGH
self._oscillating = self._status["dps"]["8"]
async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaFan, flow_schema)
async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaFan, flow_schema)