#pragma once #include "esphome/core/automation.h" #include "media_player.h" namespace esphome { namespace media_player { template class MediaPlayerCommandAction : public Action, public Parented { public: TEMPLATABLE_VALUE(bool, announcement); void play(const Ts &...x) override { this->parent_->make_call().set_command(Command).set_announcement(this->announcement_.value(x...)).perform(); } }; template using PlayAction = MediaPlayerCommandAction; template using PauseAction = MediaPlayerCommandAction; template using StopAction = MediaPlayerCommandAction; template using ToggleAction = MediaPlayerCommandAction; template using VolumeUpAction = MediaPlayerCommandAction; template using VolumeDownAction = MediaPlayerCommandAction; template using TurnOnAction = MediaPlayerCommandAction; template using TurnOffAction = MediaPlayerCommandAction; template using NextAction = MediaPlayerCommandAction; template using PreviousAction = MediaPlayerCommandAction; template using MuteAction = MediaPlayerCommandAction; template using UnmuteAction = MediaPlayerCommandAction; template using RepeatOffAction = MediaPlayerCommandAction; template using RepeatOneAction = MediaPlayerCommandAction; template using RepeatAllAction = MediaPlayerCommandAction; template using ShuffleAction = MediaPlayerCommandAction; template using UnshuffleAction = MediaPlayerCommandAction; template using GroupJoinAction = MediaPlayerCommandAction; template using ClearPlaylistAction = MediaPlayerCommandAction; template class MediaPlayerMediaAction : public Action, public Parented { TEMPLATABLE_VALUE(std::string, media_url) TEMPLATABLE_VALUE(bool, announcement) void play(const Ts &...x) override { auto call = this->parent_->make_call(); if constexpr (Command != MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PLAY) call.set_command(Command); call.set_media_url(this->media_url_.value(x...)).set_announcement(this->announcement_.value(x...)).perform(); } }; template using PlayMediaAction = MediaPlayerMediaAction; template using EnqueueMediaAction = MediaPlayerMediaAction; template class VolumeSetAction : public Action, public Parented { TEMPLATABLE_VALUE(float, volume) void play(const Ts &...x) override { this->parent_->make_call().set_volume(this->volume_.value(x...)).perform(); } }; /// Callback forwarder that triggers an Automation<> on any state change. /// Pointer-sized (single Automation* field) to fit inline in Callback::ctx_. struct StateAnyForwarder { Automation<> *automation; void operator()(MediaPlayerState /*state*/) const { this->automation->trigger(); } }; /// Callback forwarder that triggers an Automation<> only when a specific media player state is entered. /// Pointer-sized (single Automation* field) to fit inline in Callback::ctx_. template struct StateEnterForwarder { Automation<> *automation; void operator()(MediaPlayerState state) const { if (state == State) this->automation->trigger(); } }; static_assert(sizeof(StateAnyForwarder) <= sizeof(void *)); static_assert(std::is_trivially_copyable_v); static_assert(sizeof(StateEnterForwarder) <= sizeof(void *)); static_assert(std::is_trivially_copyable_v>); template class IsIdleCondition : public Condition, public Parented { public: bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_IDLE; } }; template class IsPlayingCondition : public Condition, public Parented { public: bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; } }; template class IsPausedCondition : public Condition, public Parented { public: bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PAUSED; } }; template class IsAnnouncingCondition : public Condition, public Parented { public: bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_ANNOUNCING; } }; template class IsOnCondition : public Condition, public Parented { public: bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_ON; } }; template class IsOffCondition : public Condition, public Parented { public: bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_OFF; } }; template class IsMutedCondition : public Condition, public Parented { public: bool check(const Ts &...x) override { return this->parent_->is_muted(); } }; } // namespace media_player } // namespace esphome