#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(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 class PlayMediaAction : public Action, public Parented { TEMPLATABLE_VALUE(std::string, media_url) TEMPLATABLE_VALUE(bool, announcement) void play(Ts... x) override { this->parent_->make_call() .set_media_url(this->media_url_.value(x...)) .set_announcement(this->announcement_.value(x...)) .perform(); } }; template class VolumeSetAction : public Action, public Parented { TEMPLATABLE_VALUE(float, volume) void play(Ts... x) override { this->parent_->make_call().set_volume(this->volume_.value(x...)).perform(); } }; class StateTrigger : public Trigger<> { public: explicit StateTrigger(MediaPlayer *player) { player->add_on_state_callback([this]() { this->trigger(); }); } }; template class MediaPlayerStateTrigger : public Trigger<> { public: explicit MediaPlayerStateTrigger(MediaPlayer *player) { player->add_on_state_callback([this, player]() { if (player->state == State) this->trigger(); }); } }; using IdleTrigger = MediaPlayerStateTrigger; using PlayTrigger = MediaPlayerStateTrigger; using PauseTrigger = MediaPlayerStateTrigger; using AnnouncementTrigger = MediaPlayerStateTrigger; template class IsIdleCondition : public Condition, public Parented { public: bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_IDLE; } }; template class IsPlayingCondition : public Condition, public Parented { public: bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; } }; template class IsPausedCondition : public Condition, public Parented { public: bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PAUSED; } }; template class IsAnnouncingCondition : public Condition, public Parented { public: bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_ANNOUNCING; } }; } // namespace media_player } // namespace esphome