mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 21:44:55 +01:00
[code-quality] clang-tidy media_player (#7238)
This commit is contained in:
parent
f2e99fa319
commit
e769804fe6
1 changed files with 31 additions and 27 deletions
|
@ -7,30 +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 ACTION_CLASS : public Action<Ts...>, public Parented<MediaPlayer> { \
|
class MediaPlayerCommandAction : public Action<Ts...>, public Parented<MediaPlayer> {
|
||||||
void play(Ts... x) override { \
|
public:
|
||||||
this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_##ACTION_COMMAND).perform(); \
|
void play(Ts... x) override { this->parent_->make_call().set_command(Command).perform(); }
|
||||||
} \
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#define MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(TRIGGER_CLASS, TRIGGER_STATE) \
|
template<typename... Ts>
|
||||||
class TRIGGER_CLASS : public Trigger<> { \
|
using PlayAction = MediaPlayerCommandAction<MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PLAY, Ts...>;
|
||||||
public: \
|
template<typename... Ts>
|
||||||
explicit TRIGGER_CLASS(MediaPlayer *player) { \
|
using PauseAction = MediaPlayerCommandAction<MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PAUSE, Ts...>;
|
||||||
player->add_on_state_callback([this, player]() { \
|
template<typename... Ts>
|
||||||
if (player->state == MediaPlayerState::MEDIA_PLAYER_STATE_##TRIGGER_STATE) \
|
using StopAction = MediaPlayerCommandAction<MediaPlayerCommand::MEDIA_PLAYER_COMMAND_STOP, Ts...>;
|
||||||
this->trigger(); \
|
template<typename... Ts>
|
||||||
}); \
|
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>
|
||||||
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(PlayAction, PLAY)
|
using VolumeDownAction = MediaPlayerCommandAction<MediaPlayerCommand::MEDIA_PLAYER_COMMAND_VOLUME_DOWN, Ts...>;
|
||||||
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)
|
||||||
|
@ -49,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:
|
||||||
|
|
Loading…
Reference in a new issue