Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fan set_speed support for Xiaomi Mi Air Purifier 3C #126870

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions homeassistant/components/xiaomi_miio/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
# Air Fresh A1
ATTR_FAVORITE_SPEED = "favorite_speed"

# Air Purifier 3C
ATTR_FAVORITE_RPM = "favorite_rpm"
ATTR_MOTOR_SPEED = "motor_speed"

Check warning on line 121 in homeassistant/components/xiaomi_miio/fan.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/xiaomi_miio/fan.py#L120-L121

Added lines #L120 - L121 were not covered by tests

# Map attributes to properties of the state object
AVAILABLE_ATTRIBUTES_AIRPURIFIER_COMMON = {
ATTR_EXTRA_FEATURES: "extra_features",
Expand Down Expand Up @@ -607,28 +611,68 @@
class XiaomiAirPurifierMB4(XiaomiGenericAirPurifier):
"""Representation of a Xiaomi Air Purifier MB4."""

def __init__(self, device, entry, unique_id, coordinator):
def __init__(self, device, entry, unique_id, coordinator) -> None:

Check warning on line 614 in homeassistant/components/xiaomi_miio/fan.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/xiaomi_miio/fan.py#L614

Added line #L614 was not covered by tests
"""Initialize Air Purifier MB4."""
super().__init__(device, entry, unique_id, coordinator)

self._device_features = FEATURE_FLAGS_AIRPURIFIER_3C
self._preset_modes = PRESET_MODES_AIRPURIFIER_3C
self._attr_supported_features = (
FanEntityFeature.PRESET_MODE
FanEntityFeature.SET_SPEED
| FanEntityFeature.PRESET_MODE
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)

self._state = self.coordinator.data.is_on
self._mode = self.coordinator.data.mode.value
self._favorite_rpm: int | None = None
self._speed_range = (300, 2200)
self._motor_speed = 0

Check warning on line 631 in homeassistant/components/xiaomi_miio/fan.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/xiaomi_miio/fan.py#L629-L631

Added lines #L629 - L631 were not covered by tests

@property
def operation_mode_class(self):
"""Hold operation mode class."""
return AirpurifierMiotOperationMode

@property
def percentage(self) -> int | None:

Check warning on line 639 in homeassistant/components/xiaomi_miio/fan.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/xiaomi_miio/fan.py#L638-L639

Added lines #L638 - L639 were not covered by tests
"""Return the current percentage based speed."""
# show the actual fan speed in silent or auto preset mode
if self._mode != self.operation_mode_class["Favorite"].value:
return ranged_value_to_percentage(self._speed_range, self._motor_speed)
if self._favorite_rpm is None:
return None
if self._state:
return ranged_value_to_percentage(self._speed_range, self._favorite_rpm)

Check warning on line 647 in homeassistant/components/xiaomi_miio/fan.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/xiaomi_miio/fan.py#L642-L647

Added lines #L642 - L647 were not covered by tests

return None

Check warning on line 649 in homeassistant/components/xiaomi_miio/fan.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/xiaomi_miio/fan.py#L649

Added line #L649 was not covered by tests

async def async_set_percentage(self, percentage: int) -> None:

Check warning on line 651 in homeassistant/components/xiaomi_miio/fan.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/xiaomi_miio/fan.py#L651

Added line #L651 was not covered by tests
"""Set the percentage of the fan. This method is a coroutine."""
if percentage == 0:
await self.async_turn_off()
return

Check warning on line 655 in homeassistant/components/xiaomi_miio/fan.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/xiaomi_miio/fan.py#L653-L655

Added lines #L653 - L655 were not covered by tests

favorite_rpm = int(

Check warning on line 657 in homeassistant/components/xiaomi_miio/fan.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/xiaomi_miio/fan.py#L657

Added line #L657 was not covered by tests
round(percentage_to_ranged_value(self._speed_range, percentage), -1)
)
if not favorite_rpm:
return
if await self._try_command(

Check warning on line 662 in homeassistant/components/xiaomi_miio/fan.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/xiaomi_miio/fan.py#L660-L662

Added lines #L660 - L662 were not covered by tests
"Setting fan level of the miio device failed.",
self._device.set_favorite_rpm,
favorite_rpm,
):
self._favorite_rpm = favorite_rpm
self._mode = self.operation_mode_class["Favorite"].value
self.async_write_ha_state()

Check warning on line 669 in homeassistant/components/xiaomi_miio/fan.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/xiaomi_miio/fan.py#L667-L669

Added lines #L667 - L669 were not covered by tests

async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set the preset mode of the fan."""
if not self._state:
await self.async_turn_on()

Check warning on line 674 in homeassistant/components/xiaomi_miio/fan.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/xiaomi_miio/fan.py#L673-L674

Added lines #L673 - L674 were not covered by tests

if await self._try_command(
"Setting operation mode of the miio device failed.",
self._device.set_mode,
Expand All @@ -642,6 +686,14 @@
"""Fetch state from the device."""
self._state = self.coordinator.data.is_on
self._mode = self.coordinator.data.mode.value
self._favorite_rpm = getattr(self.coordinator.data, ATTR_FAVORITE_RPM, None)
self._motor_speed = min(

Check warning on line 690 in homeassistant/components/xiaomi_miio/fan.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/xiaomi_miio/fan.py#L689-L690

Added lines #L689 - L690 were not covered by tests
self._speed_range[1],
max(
self._speed_range[0],
getattr(self.coordinator.data, ATTR_MOTOR_SPEED, 0),
),
)
self.async_write_ha_state()


Expand Down