mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Fix setting and reading percentage for MIOT based fans (#77626)
This commit is contained in:
parent
fa987564a7
commit
5d7e9a6695
@ -112,6 +112,15 @@ MODELS_FAN_MIOT = [
|
|||||||
MODEL_FAN_ZA5,
|
MODEL_FAN_ZA5,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# number of speed levels each fan has
|
||||||
|
SPEEDS_FAN_MIOT = {
|
||||||
|
MODEL_FAN_1C: 3,
|
||||||
|
MODEL_FAN_P10: 4,
|
||||||
|
MODEL_FAN_P11: 4,
|
||||||
|
MODEL_FAN_P9: 4,
|
||||||
|
MODEL_FAN_ZA5: 4,
|
||||||
|
}
|
||||||
|
|
||||||
MODELS_PURIFIER_MIOT = [
|
MODELS_PURIFIER_MIOT = [
|
||||||
MODEL_AIRPURIFIER_3,
|
MODEL_AIRPURIFIER_3,
|
||||||
MODEL_AIRPURIFIER_3C,
|
MODEL_AIRPURIFIER_3C,
|
||||||
|
@ -85,6 +85,7 @@ from .const import (
|
|||||||
MODELS_PURIFIER_MIOT,
|
MODELS_PURIFIER_MIOT,
|
||||||
SERVICE_RESET_FILTER,
|
SERVICE_RESET_FILTER,
|
||||||
SERVICE_SET_EXTRA_FEATURES,
|
SERVICE_SET_EXTRA_FEATURES,
|
||||||
|
SPEEDS_FAN_MIOT,
|
||||||
)
|
)
|
||||||
from .device import XiaomiCoordinatedMiioEntity
|
from .device import XiaomiCoordinatedMiioEntity
|
||||||
|
|
||||||
@ -234,9 +235,13 @@ async def async_setup_entry(
|
|||||||
elif model in MODELS_FAN_MIIO:
|
elif model in MODELS_FAN_MIIO:
|
||||||
entity = XiaomiFan(device, config_entry, unique_id, coordinator)
|
entity = XiaomiFan(device, config_entry, unique_id, coordinator)
|
||||||
elif model == MODEL_FAN_ZA5:
|
elif model == MODEL_FAN_ZA5:
|
||||||
entity = XiaomiFanZA5(device, config_entry, unique_id, coordinator)
|
speed_count = SPEEDS_FAN_MIOT[model]
|
||||||
|
entity = XiaomiFanZA5(device, config_entry, unique_id, coordinator, speed_count)
|
||||||
elif model in MODELS_FAN_MIOT:
|
elif model in MODELS_FAN_MIOT:
|
||||||
entity = XiaomiFanMiot(device, config_entry, unique_id, coordinator)
|
speed_count = SPEEDS_FAN_MIOT[model]
|
||||||
|
entity = XiaomiFanMiot(
|
||||||
|
device, config_entry, unique_id, coordinator, speed_count
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -1044,6 +1049,11 @@ class XiaomiFanP5(XiaomiGenericFan):
|
|||||||
class XiaomiFanMiot(XiaomiGenericFan):
|
class XiaomiFanMiot(XiaomiGenericFan):
|
||||||
"""Representation of a Xiaomi Fan Miot."""
|
"""Representation of a Xiaomi Fan Miot."""
|
||||||
|
|
||||||
|
def __init__(self, device, entry, unique_id, coordinator, speed_count):
|
||||||
|
"""Initialize MIOT fan with speed count."""
|
||||||
|
super().__init__(device, entry, unique_id, coordinator)
|
||||||
|
self._speed_count = speed_count
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def operation_mode_class(self):
|
def operation_mode_class(self):
|
||||||
"""Hold operation mode class."""
|
"""Hold operation mode class."""
|
||||||
@ -1061,7 +1071,9 @@ class XiaomiFanMiot(XiaomiGenericFan):
|
|||||||
self._preset_mode = self.coordinator.data.mode.name
|
self._preset_mode = self.coordinator.data.mode.name
|
||||||
self._oscillating = self.coordinator.data.oscillate
|
self._oscillating = self.coordinator.data.oscillate
|
||||||
if self.coordinator.data.is_on:
|
if self.coordinator.data.is_on:
|
||||||
self._percentage = self.coordinator.data.speed
|
self._percentage = ranged_value_to_percentage(
|
||||||
|
(1, self._speed_count), self.coordinator.data.speed
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self._percentage = 0
|
self._percentage = 0
|
||||||
|
|
||||||
@ -1087,16 +1099,22 @@ class XiaomiFanMiot(XiaomiGenericFan):
|
|||||||
await self.async_turn_off()
|
await self.async_turn_off()
|
||||||
return
|
return
|
||||||
|
|
||||||
await self._try_command(
|
speed = math.ceil(
|
||||||
"Setting fan speed percentage of the miio device failed.",
|
percentage_to_ranged_value((1, self._speed_count), percentage)
|
||||||
self._device.set_speed,
|
|
||||||
percentage,
|
|
||||||
)
|
)
|
||||||
self._percentage = percentage
|
|
||||||
|
|
||||||
|
# if the fan is not on, we have to turn it on first
|
||||||
if not self.is_on:
|
if not self.is_on:
|
||||||
await self.async_turn_on()
|
await self.async_turn_on()
|
||||||
else:
|
|
||||||
|
result = await self._try_command(
|
||||||
|
"Setting fan speed percentage of the miio device failed.",
|
||||||
|
self._device.set_speed,
|
||||||
|
speed,
|
||||||
|
)
|
||||||
|
|
||||||
|
if result:
|
||||||
|
self._percentage = ranged_value_to_percentage((1, self._speed_count), speed)
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user