ESPHome 2025.8.0b1
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>
31template<typename... Ts>
33template<typename... Ts>
35
36template<typename... Ts> class PlayMediaAction : public Action<Ts...>, public Parented<MediaPlayer> {
37 TEMPLATABLE_VALUE(std::string, media_url)
38 TEMPLATABLE_VALUE(bool, announcement)
39 void play(Ts... x) override {
40 this->parent_->make_call()
41 .set_media_url(this->media_url_.value(x...))
42 .set_announcement(this->announcement_.value(x...))
43 .perform();
44 }
45};
46
47template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<MediaPlayer> {
48 TEMPLATABLE_VALUE(float, volume)
49 void play(Ts... x) override { this->parent_->make_call().set_volume(this->volume_.value(x...)).perform(); }
50};
51
52class StateTrigger : public Trigger<> {
53 public:
54 explicit StateTrigger(MediaPlayer *player) {
55 player->add_on_state_callback([this]() { this->trigger(); });
56 }
57};
58
59template<MediaPlayerState State> class MediaPlayerStateTrigger : public Trigger<> {
60 public:
62 player->add_on_state_callback([this, player]() {
63 if (player->state == State)
64 this->trigger();
65 });
66 }
67};
68
75
76template<typename... Ts> class IsIdleCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
77 public:
78 bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_IDLE; }
79};
80
81template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
82 public:
83 bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; }
84};
85
86template<typename... Ts> class IsPausedCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
87 public:
88 bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PAUSED; }
89};
90
91template<typename... Ts> class IsAnnouncingCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
92 public:
93 bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_ANNOUNCING; }
94};
95
96template<typename... Ts> class IsOnCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
97 public:
98 bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_ON; }
99};
100
101template<typename... Ts> class IsOffCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
102 public:
103 bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_OFF; }
104};
105
106} // namespace media_player
107} // namespace esphome
virtual void play(Ts... x)=0
Base class for all automation conditions.
Definition automation.h:124
Helper class to easily give an object a parent of type T.
Definition helpers.h:656
void trigger(Ts... x)
Definition automation.h:145
bool check(Ts... x) override
Definition automation.h:98
void add_on_state_callback(std::function< void()> &&callback)
StateTrigger(MediaPlayer *player)
Definition automation.h:54
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5