Fix Chromecast volume rounding bug (#2656)
authorOzGav <gavnosp@hotmail.com>
Fri, 21 Nov 2025 15:26:29 +0000 (01:26 +1000)
committerGitHub <noreply@github.com>
Fri, 21 Nov 2025 15:26:29 +0000 (15:26 +0000)
* Fix Chromecast volume rounding bug for values 8% and below

Changed volume level conversion from int() to round() to fix floating-point
precision issues. Chromecast reports volume as float (0.0-1.0) which can have
slight imprecision (e.g., 4% = 0.03999999910593033). Using int() truncated
these values, causing displayed volume to be 1% less than actual for values
8% and below.

Fixes the issue where setting volume to 5% would display as 4%, etc.

* Fix Chromecast volume rounding bug for values 8% and below

Changed volume level conversion to use round() instead of int() in both
directions to fix floating-point precision issues:

1. When setting volume: round to 2 decimal places before sending to device
2. When reading volume: round to nearest integer when converting to percentage

This fixes the issue where Chromecast reports volume as float (0.0-1.0) with
slight imprecision (e.g., 4% = 0.03999999910593033). Using int() truncated
these values, causing displayed volume to be 1% less than actual for values
8% and below.

Fixes the issue where setting volume to 5% would display as 4%, etc.

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Marvin Schenkel <marvinschenkel@gmail.com>
music_assistant/providers/chromecast/player.py

index 506da0709801c0d35913213e2e68cad2e632983e..309d8f2908916dcb6caf3e75747bcacf46426b62 100644 (file)
@@ -161,7 +161,8 @@ class ChromecastPlayer(Player):
 
     async def volume_set(self, volume_level: int) -> None:
         """Send VOLUME_SET command to given player."""
-        await asyncio.to_thread(self.cc.set_volume, volume_level / 100)
+        # Round to 2 decimal places to avoid floating-point precision issues
+        await asyncio.to_thread(self.cc.set_volume, round(volume_level / 100, 2))
 
     async def volume_mute(self, muted: bool) -> None:
         """Send VOLUME MUTE command to given player."""
@@ -388,7 +389,7 @@ class ChromecastPlayer(Player):
 
         # update player status
         self._attr_name = self.cast_info.friendly_name
-        self._attr_volume_level = int(status.volume_level * 100)
+        self._attr_volume_level = round(status.volume_level * 100)
         self._attr_volume_muted = status.volume_muted
         new_powered = self.cc.app_id is not None and self.cc.app_id != IDLE_APP_ID
         self._attr_powered = new_powered