ESPHome 2025.5.0
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
4#include "media_player.h"
5
6namespace esphome {
7
8namespace media_player {
9
10template<MediaPlayerCommand Command, typename... Ts>
11class 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
19template<typename... Ts>
21template<typename... Ts>
23template<typename... Ts>
25template<typename... Ts>
27template<typename... Ts>
29template<typename... Ts>
31
32template<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
43template<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
48class StateTrigger : public Trigger<> {
49 public:
50 explicit StateTrigger(MediaPlayer *player) {
51 player->add_on_state_callback([this]() { this->trigger(); });
52 }
53};
54
55template<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
70template<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
75template<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
80template<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
85template<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
virtual void play(Ts... x)=0
Base class for all automation conditions.
Definition automation.h:75
Helper class to easily give an object a parent of type T.
Definition helpers.h:538
void trigger(Ts... x)
Definition automation.h:96
void add_on_state_callback(std::function< void()> &&callback)
StateTrigger(MediaPlayer *player)
Definition automation.h:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5