1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-15 07:36:16 +00:00

Add media_player conditions (#161384)

This commit is contained in:
Erik Montnemery
2026-01-27 09:32:42 +01:00
committed by GitHub
parent cc6c506995
commit 27b8274d3e
6 changed files with 349 additions and 0 deletions

View File

@@ -132,6 +132,7 @@ _EXPERIMENTAL_CONDITION_PLATFORMS = {
"lawn_mower",
"light",
"lock",
"media_player",
"person",
"siren",
"switch",

View File

@@ -0,0 +1,37 @@
"""Provides conditions for media players."""
from homeassistant.core import HomeAssistant
from homeassistant.helpers.condition import Condition, make_entity_state_condition
from .const import DOMAIN, MediaPlayerState
CONDITIONS: dict[str, type[Condition]] = {
"is_off": make_entity_state_condition(DOMAIN, MediaPlayerState.OFF),
"is_on": make_entity_state_condition(
DOMAIN,
{
MediaPlayerState.BUFFERING,
MediaPlayerState.IDLE,
MediaPlayerState.ON,
MediaPlayerState.PAUSED,
MediaPlayerState.PLAYING,
},
),
"is_not_playing": make_entity_state_condition(
DOMAIN,
{
MediaPlayerState.BUFFERING,
MediaPlayerState.IDLE,
MediaPlayerState.OFF,
MediaPlayerState.ON,
MediaPlayerState.PAUSED,
},
),
"is_paused": make_entity_state_condition(DOMAIN, MediaPlayerState.PAUSED),
"is_playing": make_entity_state_condition(DOMAIN, MediaPlayerState.PLAYING),
}
async def async_get_conditions(hass: HomeAssistant) -> dict[str, type[Condition]]:
"""Return the conditions for media players."""
return CONDITIONS

View File

@@ -0,0 +1,20 @@
.condition_common: &condition_common
target:
entity:
domain: media_player
fields:
behavior:
required: true
default: any
selector:
select:
translation_key: condition_behavior
options:
- all
- any
is_off: *condition_common
is_on: *condition_common
is_not_playing: *condition_common
is_paused: *condition_common
is_playing: *condition_common

View File

@@ -1,4 +1,21 @@
{
"conditions": {
"is_not_playing": {
"condition": "mdi:stop"
},
"is_off": {
"condition": "mdi:cast-off"
},
"is_on": {
"condition": "mdi:cast"
},
"is_paused": {
"condition": "mdi:pause"
},
"is_playing": {
"condition": "mdi:play"
}
},
"entity_component": {
"_": {
"default": "mdi:cast",

View File

@@ -1,8 +1,62 @@
{
"common": {
"condition_behavior_description": "How the state should match on the targeted media players.",
"condition_behavior_name": "Behavior",
"trigger_behavior_description": "The behavior of the targeted media players to trigger on.",
"trigger_behavior_name": "Behavior"
},
"conditions": {
"is_not_playing": {
"description": "Tests if one or more media players are not playing.",
"fields": {
"behavior": {
"description": "[%key:component::media_player::common::condition_behavior_description%]",
"name": "[%key:component::media_player::common::condition_behavior_name%]"
}
},
"name": "Media player is not playing"
},
"is_off": {
"description": "Tests if one or more media players are off.",
"fields": {
"behavior": {
"description": "[%key:component::media_player::common::condition_behavior_description%]",
"name": "[%key:component::media_player::common::condition_behavior_name%]"
}
},
"name": "Media player is off"
},
"is_on": {
"description": "Tests if one or more media players are on.",
"fields": {
"behavior": {
"description": "[%key:component::media_player::common::condition_behavior_description%]",
"name": "[%key:component::media_player::common::condition_behavior_name%]"
}
},
"name": "Media player is on"
},
"is_paused": {
"description": "Tests if one or more media players are paused.",
"fields": {
"behavior": {
"description": "[%key:component::media_player::common::condition_behavior_description%]",
"name": "[%key:component::media_player::common::condition_behavior_name%]"
}
},
"name": "Media player is paused"
},
"is_playing": {
"description": "Tests if one or more media players are playing.",
"fields": {
"behavior": {
"description": "[%key:component::media_player::common::condition_behavior_description%]",
"name": "[%key:component::media_player::common::condition_behavior_name%]"
}
},
"name": "Media player is playing"
}
},
"device_automation": {
"condition_type": {
"is_buffering": "{entity_name} is buffering",
@@ -167,6 +221,12 @@
}
},
"selector": {
"condition_behavior": {
"options": {
"all": "All",
"any": "Any"
}
},
"enqueue": {
"options": {
"add": "Add to queue",

View File

@@ -0,0 +1,214 @@
"""Test media player conditions."""
from typing import Any
import pytest
from homeassistant.components.media_player.const import MediaPlayerState
from homeassistant.core import HomeAssistant
from tests.components import (
ConditionStateDescription,
assert_condition_gated_by_labs_flag,
create_target_condition,
other_states,
parametrize_condition_states_all,
parametrize_condition_states_any,
parametrize_target_entities,
set_or_remove_state,
target_entities,
)
@pytest.fixture
async def target_media_players(hass: HomeAssistant) -> list[str]:
"""Create multiple media player entities associated with different targets."""
return (await target_entities(hass, "media_player"))["included"]
@pytest.mark.parametrize(
"condition",
[
"media_player.is_off",
"media_player.is_on",
"media_player.is_not_playing",
"media_player.is_paused",
"media_player.is_playing",
],
)
async def test_media_player_conditions_gated_by_labs_flag(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, condition: str
) -> None:
"""Test the media player conditions are gated by the labs flag."""
await assert_condition_gated_by_labs_flag(hass, caplog, condition)
@pytest.mark.usefixtures("enable_labs_preview_features")
@pytest.mark.parametrize(
("condition_target_config", "entity_id", "entities_in_target"),
parametrize_target_entities("media_player"),
)
@pytest.mark.parametrize(
("condition", "condition_options", "states"),
[
*parametrize_condition_states_any(
condition="media_player.is_off",
target_states=[MediaPlayerState.OFF],
other_states=other_states(MediaPlayerState.OFF),
),
*parametrize_condition_states_any(
condition="media_player.is_on",
target_states=[
MediaPlayerState.BUFFERING,
MediaPlayerState.IDLE,
MediaPlayerState.ON,
MediaPlayerState.PAUSED,
MediaPlayerState.PLAYING,
],
other_states=[MediaPlayerState.OFF],
),
*parametrize_condition_states_any(
condition="media_player.is_not_playing",
target_states=[
MediaPlayerState.BUFFERING,
MediaPlayerState.IDLE,
MediaPlayerState.OFF,
MediaPlayerState.ON,
MediaPlayerState.PAUSED,
],
other_states=[MediaPlayerState.PLAYING],
),
*parametrize_condition_states_any(
condition="media_player.is_paused",
target_states=[MediaPlayerState.PAUSED],
other_states=other_states(MediaPlayerState.PAUSED),
),
*parametrize_condition_states_any(
condition="media_player.is_playing",
target_states=[MediaPlayerState.PLAYING],
other_states=other_states(MediaPlayerState.PLAYING),
),
],
)
async def test_media_player_state_condition_behavior_any(
hass: HomeAssistant,
target_media_players: list[str],
condition_target_config: dict,
entity_id: str,
entities_in_target: int,
condition: str,
condition_options: dict[str, Any],
states: list[ConditionStateDescription],
) -> None:
"""Test the media player state condition with the 'any' behavior."""
other_entity_ids = set(target_media_players) - {entity_id}
# Set all media players, including the tested media player, to the initial state
for eid in target_media_players:
set_or_remove_state(hass, eid, states[0]["included"])
await hass.async_block_till_done()
condition = await create_target_condition(
hass,
condition=condition,
target=condition_target_config,
behavior="any",
)
for state in states:
included_state = state["included"]
set_or_remove_state(hass, entity_id, included_state)
await hass.async_block_till_done()
assert condition(hass) == state["condition_true"]
# Check if changing other media players also passes the condition
for other_entity_id in other_entity_ids:
set_or_remove_state(hass, other_entity_id, included_state)
await hass.async_block_till_done()
assert condition(hass) == state["condition_true"]
@pytest.mark.usefixtures("enable_labs_preview_features")
@pytest.mark.parametrize(
("condition_target_config", "entity_id", "entities_in_target"),
parametrize_target_entities("media_player"),
)
@pytest.mark.parametrize(
("condition", "condition_options", "states"),
[
*parametrize_condition_states_all(
condition="media_player.is_off",
target_states=[MediaPlayerState.OFF],
other_states=other_states(MediaPlayerState.OFF),
),
*parametrize_condition_states_all(
condition="media_player.is_on",
target_states=[
MediaPlayerState.BUFFERING,
MediaPlayerState.IDLE,
MediaPlayerState.ON,
MediaPlayerState.PAUSED,
MediaPlayerState.PLAYING,
],
other_states=[MediaPlayerState.OFF],
),
*parametrize_condition_states_all(
condition="media_player.is_not_playing",
target_states=[
MediaPlayerState.BUFFERING,
MediaPlayerState.IDLE,
MediaPlayerState.OFF,
MediaPlayerState.ON,
MediaPlayerState.PAUSED,
],
other_states=[MediaPlayerState.PLAYING],
),
*parametrize_condition_states_all(
condition="media_player.is_paused",
target_states=[MediaPlayerState.PAUSED],
other_states=other_states(MediaPlayerState.PAUSED),
),
*parametrize_condition_states_all(
condition="media_player.is_playing",
target_states=[MediaPlayerState.PLAYING],
other_states=other_states(MediaPlayerState.PLAYING),
),
],
)
async def test_media_player_state_condition_behavior_all(
hass: HomeAssistant,
target_media_players: list[str],
condition_target_config: dict,
entity_id: str,
entities_in_target: int,
condition: str,
condition_options: dict[str, Any],
states: list[ConditionStateDescription],
) -> None:
"""Test the media player state condition with the 'all' behavior."""
other_entity_ids = set(target_media_players) - {entity_id}
# Set all media players, including the tested media player, to the initial state
for eid in target_media_players:
set_or_remove_state(hass, eid, states[0]["included"])
await hass.async_block_till_done()
condition = await create_target_condition(
hass,
condition=condition,
target=condition_target_config,
behavior="all",
)
for state in states:
included_state = state["included"]
set_or_remove_state(hass, entity_id, included_state)
await hass.async_block_till_done()
assert condition(hass) == state["condition_true_first_entity"]
for other_entity_id in other_entity_ids:
set_or_remove_state(hass, other_entity_id, included_state)
await hass.async_block_till_done()
assert condition(hass) == state["condition_true"]