ESPHome  2025.2.0
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 template<MediaPlayerCommand Command, typename... Ts>
11 class MediaPlayerCommandAction : public Action<Ts...>, public Parented<MediaPlayer> {
12  public:
13  TEMPLATABLE_VALUE(bool, announcement);
14  void play(Ts... x) override {
15  this->parent_->make_call().set_command(Command).set_announcement(this->announcement_.value(x...)).perform();
16  }
17 };
18 
19 template<typename... Ts>
21 template<typename... Ts>
23 template<typename... Ts>
25 template<typename... Ts>
27 template<typename... Ts>
29 template<typename... Ts>
31 
32 template<typename... Ts> class PlayMediaAction : public Action<Ts...>, public Parented<MediaPlayer> {
33  TEMPLATABLE_VALUE(std::string, media_url)
34  TEMPLATABLE_VALUE(bool, announcement)
35  void play(Ts... x) override {
36  this->parent_->make_call()
37  .set_media_url(this->media_url_.value(x...))
38  .set_announcement(this->announcement_.value(x...))
39  .perform();
40  }
41 };
42 
43 template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<MediaPlayer> {
44  TEMPLATABLE_VALUE(float, volume)
45  void play(Ts... x) override { this->parent_->make_call().set_volume(this->volume_.value(x...)).perform(); }
46 };
47 
48 class StateTrigger : public Trigger<> {
49  public:
50  explicit StateTrigger(MediaPlayer *player) {
51  player->add_on_state_callback([this]() { this->trigger(); });
52  }
53 };
54 
55 template<MediaPlayerState State> class MediaPlayerStateTrigger : public Trigger<> {
56  public:
58  player->add_on_state_callback([this, player]() {
59  if (player->state == State)
60  this->trigger();
61  });
62  }
63 };
64 
69 
70 template<typename... Ts> class IsIdleCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
71  public:
72  bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_IDLE; }
73 };
74 
75 template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
76  public:
77  bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; }
78 };
79 
80 template<typename... Ts> class IsPausedCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
81  public:
82  bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PAUSED; }
83 };
84 
85 template<typename... Ts> class IsAnnouncingCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
86  public:
87  bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_ANNOUNCING; }
88 };
89 
90 } // namespace media_player
91 } // namespace esphome
StateTrigger(MediaPlayer *player)
Definition: automation.h:50
uint16_t x
Definition: tt21100.cpp:17
bool check(Ts... x) override
Definition: automation.h:72
Base class for all automation conditions.
Definition: automation.h:74
void add_on_state_callback(std::function< void()> &&callback)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Helper class to easily give an object a parent of type T.
Definition: helpers.h:538