[media_player] Add new media player conditions (#7667)

This commit is contained in:
Kevin Ahrendt 2024-10-24 17:26:39 -04:00 committed by GitHub
parent c20e1975d1
commit 4101d5dad1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 29 additions and 0 deletions

View file

@ -21,6 +21,7 @@ media_player_ns = cg.esphome_ns.namespace("media_player")
MediaPlayer = media_player_ns.class_("MediaPlayer")
PlayAction = media_player_ns.class_(
"PlayAction", automation.Action, cg.Parented.template(MediaPlayer)
)
@ -60,7 +61,11 @@ AnnoucementTrigger = media_player_ns.class_(
"AnnouncementTrigger", automation.Trigger.template()
)
IsIdleCondition = media_player_ns.class_("IsIdleCondition", automation.Condition)
IsPausedCondition = media_player_ns.class_("IsPausedCondition", automation.Condition)
IsPlayingCondition = media_player_ns.class_("IsPlayingCondition", automation.Condition)
IsAnnouncingCondition = media_player_ns.class_(
"IsAnnouncingCondition", automation.Condition
)
async def setup_media_player_core_(var, config):
@ -159,9 +164,15 @@ async def media_player_play_media_action(config, action_id, template_arg, args):
@automation.register_condition(
"media_player.is_idle", IsIdleCondition, MEDIA_PLAYER_ACTION_SCHEMA
)
@automation.register_condition(
"media_player.is_paused", IsPausedCondition, MEDIA_PLAYER_ACTION_SCHEMA
)
@automation.register_condition(
"media_player.is_playing", IsPlayingCondition, MEDIA_PLAYER_ACTION_SCHEMA
)
@automation.register_condition(
"media_player.is_announcing", IsAnnouncingCondition, MEDIA_PLAYER_ACTION_SCHEMA
)
async def media_player_action(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])

View file

@ -68,5 +68,15 @@ template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, pub
bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; }
};
template<typename... Ts> class IsPausedCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
public:
bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PAUSED; }
};
template<typename... Ts> class IsAnnouncingCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
public:
bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_ANNOUNCING; }
};
} // namespace media_player
} // namespace esphome

View file

@ -37,6 +37,10 @@ const char *media_player_command_to_string(MediaPlayerCommand command) {
return "UNMUTE";
case MEDIA_PLAYER_COMMAND_TOGGLE:
return "TOGGLE";
case MEDIA_PLAYER_COMMAND_VOLUME_UP:
return "VOLUME_UP";
case MEDIA_PLAYER_COMMAND_VOLUME_DOWN:
return "VOLUME_DOWN";
default:
return "UNKNOWN";
}

View file

@ -27,6 +27,10 @@ media_player:
media_player.is_idle:
- wait_until:
media_player.is_playing:
- wait_until:
media_player.is_announcing:
- wait_until:
media_player.is_paused:
- media_player.volume_up:
- media_player.volume_down:
- media_player.volume_set: 50%