This commit is contained in:
Tomasz Duda 2024-08-09 18:10:09 +02:00
parent d355fecbaa
commit 355ff1cf4d

View file

@ -7,36 +7,24 @@ namespace esphome {
namespace media_player { namespace media_player {
#define MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(ACTION_CLASS, ACTION_COMMAND) \ template<MediaPlayerCommand Command, typename... Ts>
template<typename... Ts> \ class MediaPlayerCommandAction : public Action<Ts...>, public Parented<MediaPlayer> {
class ACTION_CLASS /* NOLINT(bugprone-macro-parentheses)*/ \ public:
: public Action<Ts...>, \ void play(Ts... x) override { this->parent_->make_call().set_command(Command).perform(); }
public Parented<MediaPlayer>{ \ };
void play(Ts... x) override{this->parent_->make_call() \
.set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_##ACTION_COMMAND) \
.perform(); \
} \
} \
;
#define MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(TRIGGER_CLASS, TRIGGER_STATE) \ template<typename... Ts>
class TRIGGER_CLASS /* NOLINT(bugprone-macro-parentheses)*/ \ using PlayAction = MediaPlayerCommandAction<MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PLAY, Ts...>;
: public Trigger<> { \ template<typename... Ts>
public: \ using PauseAction = MediaPlayerCommandAction<MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PAUSE, Ts...>;
explicit TRIGGER_CLASS(MediaPlayer *player) { \ template<typename... Ts>
player->add_on_state_callback([this, player]() { \ using StopAction = MediaPlayerCommandAction<MediaPlayerCommand::MEDIA_PLAYER_COMMAND_STOP, Ts...>;
if (player->state == MediaPlayerState::MEDIA_PLAYER_STATE_##TRIGGER_STATE) \ template<typename... Ts>
this->trigger(); \ using ToggleAction = MediaPlayerCommandAction<MediaPlayerCommand::MEDIA_PLAYER_COMMAND_TOGGLE, Ts...>;
}); \ template<typename... Ts>
} \ using VolumeUpAction = MediaPlayerCommandAction<MediaPlayerCommand::MEDIA_PLAYER_COMMAND_VOLUME_UP, Ts...>;
}; template<typename... Ts>
using VolumeDownAction = MediaPlayerCommandAction<MediaPlayerCommand::MEDIA_PLAYER_COMMAND_VOLUME_DOWN, Ts...>;
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(PlayAction, PLAY)
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(PauseAction, PAUSE)
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(StopAction, STOP)
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(ToggleAction, TOGGLE)
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(VolumeUpAction, VOLUME_UP)
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(VolumeDownAction, VOLUME_DOWN)
template<typename... Ts> class PlayMediaAction : public Action<Ts...>, public Parented<MediaPlayer> { template<typename... Ts> class PlayMediaAction : public Action<Ts...>, public Parented<MediaPlayer> {
TEMPLATABLE_VALUE(std::string, media_url) TEMPLATABLE_VALUE(std::string, media_url)
@ -55,10 +43,20 @@ class StateTrigger : public Trigger<> {
} }
}; };
MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(IdleTrigger, IDLE) template<MediaPlayerState State> class MediaPlayerStateTrigger : public Trigger<> {
MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(PlayTrigger, PLAYING) public:
MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(PauseTrigger, PAUSED) explicit MediaPlayerStateTrigger(MediaPlayer *player) {
MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(AnnouncementTrigger, ANNOUNCING) player->add_on_state_callback([this, player]() {
if (player->state == State)
this->trigger();
});
}
};
using IdleTrigger = MediaPlayerStateTrigger<MediaPlayerState::MEDIA_PLAYER_STATE_IDLE>;
using PlayTrigger = MediaPlayerStateTrigger<MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING>;
using PauseTrigger = MediaPlayerStateTrigger<MediaPlayerState::MEDIA_PLAYER_STATE_PAUSED>;
using AnnouncementTrigger = MediaPlayerStateTrigger<MediaPlayerState::MEDIA_PLAYER_STATE_ANNOUNCING>;
template<typename... Ts> class IsIdleCondition : public Condition<Ts...>, public Parented<MediaPlayer> { template<typename... Ts> class IsIdleCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
public: public: