Update vesync to use new fan entity model (#45585)
parent
a4c8cb6f84
commit
fdcf1fccf8
|
@ -1,16 +1,14 @@
|
|||
"""Support for VeSync fans."""
|
||||
import logging
|
||||
import math
|
||||
|
||||
from homeassistant.components.fan import (
|
||||
SPEED_HIGH,
|
||||
SPEED_LOW,
|
||||
SPEED_MEDIUM,
|
||||
SPEED_OFF,
|
||||
SUPPORT_SET_SPEED,
|
||||
FanEntity,
|
||||
)
|
||||
from homeassistant.components.fan import SUPPORT_SET_SPEED, FanEntity
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.util.percentage import (
|
||||
percentage_to_ranged_value,
|
||||
ranged_value_to_percentage,
|
||||
)
|
||||
|
||||
from .common import VeSyncDevice
|
||||
from .const import DOMAIN, VS_DISCOVERY, VS_DISPATCHERS, VS_FANS
|
||||
|
@ -21,8 +19,11 @@ DEV_TYPE_TO_HA = {
|
|||
"LV-PUR131S": "fan",
|
||||
}
|
||||
|
||||
FAN_SPEEDS = [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
|
||||
FAN_MODE_AUTO = "auto"
|
||||
FAN_MODE_SLEEP = "sleep"
|
||||
|
||||
PRESET_MODES = [FAN_MODE_AUTO, FAN_MODE_SLEEP]
|
||||
SPEED_RANGE = (1, 3) # off is not included
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
|
@ -68,20 +69,25 @@ class VeSyncFanHA(VeSyncDevice, FanEntity):
|
|||
return SUPPORT_SET_SPEED
|
||||
|
||||
@property
|
||||
def speed(self):
|
||||
def percentage(self):
|
||||
"""Return the current speed."""
|
||||
if self.smartfan.mode == FAN_MODE_AUTO:
|
||||
return None
|
||||
if self.smartfan.mode == "manual":
|
||||
current_level = self.smartfan.fan_level
|
||||
if current_level is not None:
|
||||
return FAN_SPEEDS[current_level]
|
||||
return ranged_value_to_percentage(SPEED_RANGE, current_level)
|
||||
return None
|
||||
|
||||
@property
|
||||
def speed_list(self):
|
||||
"""Get the list of available speeds."""
|
||||
return FAN_SPEEDS
|
||||
def preset_modes(self):
|
||||
"""Get the list of available preset modes."""
|
||||
return PRESET_MODES
|
||||
|
||||
@property
|
||||
def preset_mode(self):
|
||||
"""Get the current preset mode."""
|
||||
if self.smartfan.mode == FAN_MODE_AUTO:
|
||||
return FAN_MODE_AUTO
|
||||
return None
|
||||
|
||||
@property
|
||||
def unique_info(self):
|
||||
|
@ -99,21 +105,34 @@ class VeSyncFanHA(VeSyncDevice, FanEntity):
|
|||
"screen_status": self.smartfan.screen_status,
|
||||
}
|
||||
|
||||
def set_speed(self, speed):
|
||||
def set_percentage(self, percentage):
|
||||
"""Set the speed of the device."""
|
||||
if percentage == 0:
|
||||
self.smartfan.turn_off()
|
||||
return
|
||||
|
||||
if not self.smartfan.is_on:
|
||||
self.smartfan.turn_on()
|
||||
|
||||
self.smartfan.manual_mode()
|
||||
self.smartfan.change_fan_speed(FAN_SPEEDS.index(speed))
|
||||
self.smartfan.change_fan_speed(
|
||||
math.ceil(percentage_to_ranged_value(SPEED_RANGE, percentage))
|
||||
)
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
def set_preset_mode(self, preset_mode):
|
||||
"""Set the preset mode of device."""
|
||||
if preset_mode not in self.preset_modes:
|
||||
raise ValueError(
|
||||
"{preset_mode} is not one of the valid preset modes: {self.preset_modes}"
|
||||
)
|
||||
|
||||
if not self.smartfan.is_on:
|
||||
self.smartfan.turn_on()
|
||||
|
||||
self.smartfan.auto_mode()
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
#
|
||||
# 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(
|
||||
self,
|
||||
speed: str = None,
|
||||
|
@ -122,5 +141,7 @@ class VeSyncFanHA(VeSyncDevice, FanEntity):
|
|||
**kwargs,
|
||||
) -> None:
|
||||
"""Turn the device on."""
|
||||
self.smartfan.turn_on()
|
||||
self.set_speed(speed)
|
||||
if preset_mode:
|
||||
self.set_preset_mode(preset_mode)
|
||||
return
|
||||
self.set_percentage(percentage)
|
||||
|
|
Loading…
Reference in New Issue