Skip to content

Commit

Permalink
fix: improve rounding to match blueair fan speeds
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlb committed Nov 29, 2024
1 parent c415fa8 commit d6c306a
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions custom_components/ha_blueair/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,13 @@ def is_on(self) -> int:
@property
def percentage(self) -> int:
"""Return the current speed percentage."""
return int(self._device.fan_speed / self._device.speed_count * 100)
current_speed = int(round(self._device.fan_speed / self._device.speed_count * 100))
if current_speed == 51:
current_speed = 50
return current_speed

async def async_set_percentage(self, percentage: int) -> None:
await self._device.set_fan_speed(int(percentage / 100 * self._device.speed_count))
await self._device.set_fan_speed(int(round(percentage / 100 * self._device.speed_count)))
self.async_write_ha_state()

async def async_turn_off(self, **kwargs: any) -> None:
Expand Down

2 comments on commit d6c306a

@rainwoodman
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 111 seems to point to some kind of round to zero vs round at half, or integer divide vs float divide kind of difference -- in that case, I suspect it also affects other percentages not just 51%. I suspect int((fan_speed * 100) // self._device.speed_count) might fix it without needing the patch at 111 -- as it is the most popular integer version of the formula when floating points were considered expensive, that is most likely used by BlueAir given that they use a single byte to represent the speeds to ensure multiple by 100 does not overflow a single word.

@dahlb
Copy link
Owner Author

@dahlb dahlb commented on d6c306a Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, that sounds good and looks much cleaner, pushed out in 1.11.5

Please sign in to comment.