ESPHome  2024.10.2
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  void play(Ts... x) override { this->parent_->make_call().set_command(Command).perform(); }
14 };
15 
16 template<typename... Ts>
18 template<typename... Ts>
20 template<typename... Ts>
22 template<typename... Ts>
24 template<typename... Ts>
26 template<typename... Ts>
28 
29 template<typename... Ts> class PlayMediaAction : public Action<Ts...>, public Parented<MediaPlayer> {
30  TEMPLATABLE_VALUE(std::string, media_url)
31  void play(Ts... x) override { this->parent_->make_call().set_media_url(this->media_url_.value(x...)).perform(); }
32 };
33 
34 template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<MediaPlayer> {
35  TEMPLATABLE_VALUE(float, volume)
36  void play(Ts... x) override { this->parent_->make_call().set_volume(this->volume_.value(x...)).perform(); }
37 };
38 
39 class StateTrigger : public Trigger<> {
40  public:
41  explicit StateTrigger(MediaPlayer *player) {
42  player->add_on_state_callback([this]() { this->trigger(); });
43  }
44 };
45 
46 template<MediaPlayerState State> class MediaPlayerStateTrigger : public Trigger<> {
47  public:
49  player->add_on_state_callback([this, player]() {
50  if (player->state == State)
51  this->trigger();
52  });
53  }
54 };
55 
60 
61 template<typename... Ts> class IsIdleCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
62  public:
63  bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_IDLE; }
64 };
65 
66 template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
67  public:
68  bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; }
69 };
70 
71 } // namespace media_player
72 } // namespace esphome
StateTrigger(MediaPlayer *player)
Definition: automation.h:41
uint16_t x
Definition: tt21100.cpp:17
bool check(Ts... x) override
Definition: automation.h:63
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:521