Skip to content

Commit

Permalink
Use new fan model
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi committed Mar 14, 2021
1 parent 243161d commit f230f5f
Showing 1 changed file with 41 additions and 27 deletions.
68 changes: 41 additions & 27 deletions custom_components/xiaomi_miio_airpurifier/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
SPEED_OFF,
SUPPORT_DIRECTION,
SUPPORT_OSCILLATE,
SUPPORT_PRESET_MODE,
SUPPORT_SET_SPEED,
SUPPORT_PRESET_MODE,
FanEntity,
Expand Down Expand Up @@ -2528,19 +2527,19 @@ def __init__(self, name, device, model, unique_id, retries):

self._device_features = FEATURE_FLAGS_FAN_LESHOW_SS4
self._available_attributes = AVAILABLE_ATTRIBUTES_FAN_LESHOW_SS4
self._speed_list = list(FAN_SPEED_LIST)
self._speed = None
self._percentage = None
self._preset_modes = list(FAN_PRESET_MODES)
self._preset_mode = None
self._oscillate = None

self._state_attrs[ATTR_SPEED] = None
self._state_attrs.update(
{attribute: None for attribute in self._available_attributes}
)

@property
def supported_features(self) -> int:
"""Supported features."""
return SUPPORT_SET_SPEED | SUPPORT_OSCILLATE
return SUPPORT_SET_SPEED | SUPPORT_PRESET_MODE | SUPPORT_OSCILLATE

async def async_update(self):
"""Fetch state from the device."""
Expand All @@ -2557,10 +2556,10 @@ async def async_update(self):
self._oscillate = state.oscillate
self._state = state.is_on

for level, range in FAN_SPEED_LIST.items():
for preset_mode, range in FAN_PRESET_MODES.items():
if state.speed in range:
self._speed = level
self._state_attrs[ATTR_SPEED] = level
self._preset_mode = preset_mode
self._percentage = state.speed
break

self._state_attrs.update(
Expand Down Expand Up @@ -2588,37 +2587,52 @@ async def async_update(self):
)

@property
def speed_list(self) -> list:
"""Get the list of available speeds."""
return self._speed_list
def percentage(self):
"""Return the current speed."""
return self._percentage

@property
def speed(self):
"""Return the current speed."""
return self._speed
def preset_modes(self):
"""Get the list of available preset modes."""
return self._preset_modes

async def async_set_speed(self, speed: str) -> None:
"""Set the speed of the fan."""
if self.supported_features & SUPPORT_SET_SPEED == 0:
return
@property
def preset_mode(self):
"""Get the current preset mode."""
return self._preset_mode

_LOGGER.debug("Setting the fan speed to: %s", speed)
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set the preset mode of the fan."""
if self.supported_features & SUPPORT_PRESET_MODE == 0:
return

if speed.isdigit():
speed = int(speed)
_LOGGER.debug("Setting the preset mode to: %s", preset_mode)

if speed in [SPEED_OFF, 0]:
if preset_mode == SPEED_OFF:
await self.async_turn_off()
return

# Map speed level to speed
if speed in FAN_SPEED_VALUES:
speed = FAN_SPEED_VALUES[speed]

await self._try_command(
"Setting fan speed of the miio device failed.",
self._device.set_speed,
speed,
FAN_PRESET_MODE_VALUES[preset_mode],
)

async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed percentage of the fan."""
if self.supported_features & SUPPORT_SET_SPEED == 0:
return

_LOGGER.debug("Setting the fan speed percentage to: %s", percentage)

if percentage == 0:
await self.async_turn_off()
return

await self._try_command(
"Setting fan speed percentage of the miio device failed.",
self._device.set_speed,
percentage,
)

@property
Expand Down

0 comments on commit f230f5f

Please sign in to comment.