mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 11:33:11 -04:00
Fix Assist volume up and down for step-only media players (#195924)
This commit is contained in:
@@ -448,7 +448,14 @@ class MediaSetVolumeRelativeHandler(intent.IntentHandler):
|
||||
floor_name=floor_id,
|
||||
domains={DOMAIN},
|
||||
assistant=intent_obj.assistant,
|
||||
features=MediaPlayerEntityFeature.VOLUME_SET,
|
||||
features=(
|
||||
(
|
||||
MediaPlayerEntityFeature.VOLUME_STEP,
|
||||
MediaPlayerEntityFeature.VOLUME_SET,
|
||||
)
|
||||
if volume_step in ("up", "down")
|
||||
else MediaPlayerEntityFeature.VOLUME_SET
|
||||
),
|
||||
)
|
||||
match_preferences = intent.MatchTargetsPreferences(
|
||||
area_id=slots.get("preferred_area_id", {}).get("value"),
|
||||
|
||||
@@ -326,8 +326,8 @@ class MatchTargetsConstraints:
|
||||
device_classes: Collection[str] | None = None
|
||||
"""Device class names."""
|
||||
|
||||
features: int | None = None
|
||||
"""Required supported features."""
|
||||
features: int | tuple[int, ...] | None = None
|
||||
"""Required feature mask, or alternative masks of which one must match."""
|
||||
|
||||
states: Collection[str] | None = None
|
||||
"""Required states for entities."""
|
||||
@@ -504,21 +504,40 @@ def _filter_by_name(
|
||||
|
||||
|
||||
def _filter_by_features(
|
||||
features: int,
|
||||
features: int | tuple[int, ...],
|
||||
candidates: Iterable[MatchTargetsCandidate],
|
||||
) -> Iterable[MatchTargetsCandidate]:
|
||||
"""Filter candidates by supported features."""
|
||||
"""Filter candidates by supported features.
|
||||
|
||||
A tuple lists alternative feature masks. A candidate matches if it supports
|
||||
every feature in at least one mask.
|
||||
"""
|
||||
if isinstance(features, int):
|
||||
for candidate in candidates:
|
||||
if candidate.entity is not None and (
|
||||
candidate.entity.supported_features & features == features
|
||||
):
|
||||
yield candidate
|
||||
continue
|
||||
|
||||
state_features = candidate.state.attributes.get(
|
||||
EntityStateAttribute.SUPPORTED_FEATURES, 0
|
||||
)
|
||||
if state_features & features == features:
|
||||
yield candidate
|
||||
return
|
||||
|
||||
for candidate in candidates:
|
||||
if (candidate.entity is not None) and (
|
||||
(candidate.entity.supported_features & features) == features
|
||||
if (entity := candidate.entity) is not None and any(
|
||||
entity.supported_features & mask == mask for mask in features
|
||||
):
|
||||
yield candidate
|
||||
continue
|
||||
|
||||
supported_features = candidate.state.attributes.get(
|
||||
state_features = candidate.state.attributes.get(
|
||||
EntityStateAttribute.SUPPORTED_FEATURES, 0
|
||||
)
|
||||
if (supported_features & features) == features:
|
||||
if any(state_features & mask == mask for mask in features):
|
||||
yield candidate
|
||||
|
||||
|
||||
|
||||
@@ -1167,6 +1167,41 @@ async def test_volume_relative_media_player_intent(
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("direction", ["up", "down"])
|
||||
async def test_volume_relative_step_only_player(
|
||||
hass: HomeAssistant, direction: str
|
||||
) -> None:
|
||||
"""Test a player with volume buttons and no volume level."""
|
||||
assert await async_setup_component(hass, DOMAIN, {})
|
||||
await media_player_intent.async_setup_intents(hass)
|
||||
|
||||
class StepMediaPlayer(MediaPlayerEntity):
|
||||
_attr_name = "Amplifier"
|
||||
_attr_state = MediaPlayerState.ON
|
||||
_attr_supported_features = MediaPlayerEntityFeature.VOLUME_STEP
|
||||
|
||||
player = StepMediaPlayer()
|
||||
component: EntityComponent[MediaPlayerEntity] = hass.data[DOMAIN]
|
||||
await component.async_add_entities([player])
|
||||
|
||||
slots = {"name": {"value": "Amplifier"}, "volume_step": {"value": direction}}
|
||||
with patch.object(player, f"async_volume_{direction}") as volume_button:
|
||||
response = await intent.async_handle(
|
||||
hass, "test", media_player_intent.INTENT_SET_VOLUME_RELATIVE, slots
|
||||
)
|
||||
volume_button.assert_awaited_once_with()
|
||||
assert [target.id for target in response.success_results] == [player.entity_id]
|
||||
|
||||
with pytest.raises(intent.MatchFailedError) as err:
|
||||
await intent.async_handle(
|
||||
hass,
|
||||
"test",
|
||||
media_player_intent.INTENT_SET_VOLUME_RELATIVE,
|
||||
{**slots, "volume_step": {"value": 10}},
|
||||
)
|
||||
assert err.value.result.no_match_reason is intent.MatchFailedReason.FEATURE
|
||||
|
||||
|
||||
async def test_volume_relative_multiple_media_players_report_all_success(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user