Skip to content

Commit

Permalink
Improve handling of roon media players with fixed and incremental vol…
Browse files Browse the repository at this point in the history
…ume (#99819)
  • Loading branch information
pavoni authored and frenck committed Oct 12, 2023
1 parent b3080ae commit c11dd58
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions homeassistant/components/roon/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class RoonDevice(MediaPlayerEntity):
MediaPlayerEntityFeature.BROWSE_MEDIA
| MediaPlayerEntityFeature.GROUPING
| MediaPlayerEntityFeature.PAUSE
| MediaPlayerEntityFeature.VOLUME_SET
| MediaPlayerEntityFeature.STOP
| MediaPlayerEntityFeature.PREVIOUS_TRACK
| MediaPlayerEntityFeature.NEXT_TRACK
Expand All @@ -104,7 +103,6 @@ class RoonDevice(MediaPlayerEntity):
| MediaPlayerEntityFeature.VOLUME_MUTE
| MediaPlayerEntityFeature.PLAY
| MediaPlayerEntityFeature.PLAY_MEDIA
| MediaPlayerEntityFeature.VOLUME_STEP
)

def __init__(self, server, player_data):
Expand All @@ -124,6 +122,8 @@ def __init__(self, server, player_data):
self._attr_shuffle = False
self._attr_media_image_url = None
self._attr_volume_level = 0
self._volume_fixed = True
self._volume_incremental = False
self.update_data(player_data)

async def async_added_to_hass(self) -> None:
Expand Down Expand Up @@ -190,12 +190,21 @@ def _parse_volume(cls, player_data):
"level": 0,
"step": 0,
"muted": False,
"fixed": True,
"incremental": False,
}

try:
volume_data = player_data["volume"]
volume_muted = volume_data["is_muted"]
volume_step = convert(volume_data["step"], int, 0)
except KeyError:
return volume

volume["fixed"] = False
volume["incremental"] = volume_data["type"] == "incremental"
volume["muted"] = volume_data.get("is_muted", False)
volume["step"] = convert(volume_data.get("step"), int, 0)

try:
volume_max = volume_data["max"]
volume_min = volume_data["min"]
raw_level = convert(volume_data["value"], float, 0)
Expand All @@ -204,15 +213,9 @@ def _parse_volume(cls, player_data):
volume_percentage_factor = volume_range / 100

level = (raw_level - volume_min) / volume_percentage_factor
volume_level = convert(level, int, 0) / 100

volume["level"] = convert(level, int, 0) / 100
except KeyError:
# catch KeyError
pass
else:
volume["muted"] = volume_muted
volume["step"] = volume_step
volume["level"] = volume_level

return volume

Expand Down Expand Up @@ -288,6 +291,16 @@ def update_state(self):
self._attr_is_volume_muted = volume["muted"]
self._attr_volume_step = volume["step"]
self._attr_volume_level = volume["level"]
self._volume_fixed = volume["fixed"]
self._volume_incremental = volume["incremental"]
if not self._volume_fixed:
self._attr_supported_features = (
self._attr_supported_features | MediaPlayerEntityFeature.VOLUME_STEP
)
if not self._volume_incremental:
self._attr_supported_features = (
self._attr_supported_features | MediaPlayerEntityFeature.VOLUME_SET
)

now_playing = self._parse_now_playing(self.player_data)
self._attr_media_title = now_playing["title"]
Expand Down Expand Up @@ -359,11 +372,17 @@ def mute_volume(self, mute=True):

def volume_up(self) -> None:
"""Send new volume_level to device."""
self._server.roonapi.change_volume_percent(self.output_id, 3)
if self._volume_incremental:
self._server.roonapi.change_volume_raw(self.output_id, 1, "relative_step")
else:
self._server.roonapi.change_volume_percent(self.output_id, 3)

def volume_down(self) -> None:
"""Send new volume_level to device."""
self._server.roonapi.change_volume_percent(self.output_id, -3)
if self._volume_incremental:
self._server.roonapi.change_volume_raw(self.output_id, -1, "relative_step")
else:
self._server.roonapi.change_volume_percent(self.output_id, -3)

def turn_on(self) -> None:
"""Turn on device (if supported)."""
Expand Down

0 comments on commit c11dd58

Please sign in to comment.