ESPHome  2024.3.1
automation.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "media_player.h"
5 
6 namespace esphome {
7 
8 namespace media_player {
9 
10 #define MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(ACTION_CLASS, ACTION_COMMAND) \
11  template<typename... Ts> class ACTION_CLASS : public Action<Ts...>, public Parented<MediaPlayer> { \
12  void play(Ts... x) override { \
13  this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_##ACTION_COMMAND).perform(); \
14  } \
15  };
16 
17 #define MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(TRIGGER_CLASS, TRIGGER_STATE) \
18  class TRIGGER_CLASS : public Trigger<> { \
19  public: \
20  explicit TRIGGER_CLASS(MediaPlayer *player) { \
21  player->add_on_state_callback([this, player]() { \
22  if (player->state == MediaPlayerState::MEDIA_PLAYER_STATE_##TRIGGER_STATE) \
23  this->trigger(); \
24  }); \
25  } \
26  };
27 
28 MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(PlayAction, PLAY)
29 MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(PauseAction, PAUSE)
31 MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(ToggleAction, TOGGLE)
32 MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(VolumeUpAction, VOLUME_UP)
33 MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(VolumeDownAction, VOLUME_DOWN)
34 
35 template<typename... Ts> class PlayMediaAction : public Action<Ts...>, public Parented<MediaPlayer> {
36  TEMPLATABLE_VALUE(std::string, media_url)
37  void play(Ts... x) override { this->parent_->make_call().set_media_url(this->media_url_.value(x...)).perform(); }
38 };
39 
40 template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<MediaPlayer> {
41  TEMPLATABLE_VALUE(float, volume)
42  void play(Ts... x) override { this->parent_->make_call().set_volume(this->volume_.value(x...)).perform(); }
43 };
44 
45 class StateTrigger : public Trigger<> {
46  public:
47  explicit StateTrigger(MediaPlayer *player) {
48  player->add_on_state_callback([this]() { this->trigger(); });
49  }
50 };
51 
52 MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(IdleTrigger, IDLE)
53 MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(PlayTrigger, PLAYING)
54 MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(PauseTrigger, PAUSED)
55 
56 template<typename... Ts> class IsIdleCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
57  public:
58  bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_IDLE; }
59 };
60 
61 template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
62  public:
63  bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; }
64 };
65 
66 } // namespace media_player
67 } // namespace esphome
StateTrigger(MediaPlayer *player)
Definition: automation.h:47
uint16_t x
Definition: tt21100.cpp:17
Base class for all automation conditions.
Definition: automation.h:74
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(PlayAction, PLAY) MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(PauseAction
void add_on_state_callback(std::function< void()> &&callback)
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(IdleTrigger, IDLE) MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(PlayTrigger
MediaPlayerCall & set_media_url(const std::string &url)
virtual void play(Ts... x)=0
Helper class to easily give an object a parent of type T.
Definition: helpers.h:515