ESPHome 2026.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
7
8template<MediaPlayerCommand Command, typename... Ts>
9class MediaPlayerCommandAction : public Action<Ts...>, public Parented<MediaPlayer> {
10 public:
11 TEMPLATABLE_VALUE(bool, announcement);
12 void play(const Ts &...x) override {
13 this->parent_->make_call().set_command(Command).set_announcement(this->announcement_.value(x...)).perform();
14 }
15};
16
17template<typename... Ts>
19template<typename... Ts>
21template<typename... Ts>
23template<typename... Ts>
25template<typename... Ts>
27template<typename... Ts>
29template<typename... Ts>
31template<typename... Ts>
33template<typename... Ts>
35template<typename... Ts>
37template<typename... Ts>
39template<typename... Ts>
41template<typename... Ts>
43template<typename... Ts>
45template<typename... Ts>
47template<typename... Ts>
49template<typename... Ts>
51template<typename... Ts>
53template<typename... Ts>
55
56template<MediaPlayerCommand Command, typename... Ts>
57class MediaPlayerMediaAction : public Action<Ts...>, public Parented<MediaPlayer> {
58 TEMPLATABLE_VALUE(std::string, media_url)
59 TEMPLATABLE_VALUE(bool, announcement)
60 void play(const Ts &...x) override {
61 auto call = this->parent_->make_call();
62 if constexpr (Command != MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PLAY)
63 call.set_command(Command);
64 call.set_media_url(this->media_url_.value(x...)).set_announcement(this->announcement_.value(x...)).perform();
65 }
66};
67
68template<typename... Ts>
70template<typename... Ts>
72
73template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<MediaPlayer> {
74 TEMPLATABLE_VALUE(float, volume)
75 void play(const Ts &...x) override { this->parent_->make_call().set_volume(this->volume_.value(x...)).perform(); }
76};
77
82 void operator()(MediaPlayerState /*state*/) const { this->automation->trigger(); }
83};
84
87template<MediaPlayerState State> struct StateEnterForwarder {
90 if (state == State)
91 this->automation->trigger();
92 }
93};
94
95static_assert(sizeof(StateAnyForwarder) <= sizeof(void *));
96static_assert(std::is_trivially_copyable_v<StateAnyForwarder>);
97static_assert(sizeof(StateEnterForwarder<MediaPlayerState::MEDIA_PLAYER_STATE_IDLE>) <= sizeof(void *));
98static_assert(std::is_trivially_copyable_v<StateEnterForwarder<MediaPlayerState::MEDIA_PLAYER_STATE_IDLE>>);
99
100template<typename... Ts> class IsIdleCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
101 public:
102 bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_IDLE; }
103};
104
105template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
106 public:
107 bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; }
108};
109
110template<typename... Ts> class IsPausedCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
111 public:
112 bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PAUSED; }
113};
114
115template<typename... Ts> class IsAnnouncingCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
116 public:
117 bool check(const Ts &...x) override {
119 }
120};
121
122template<typename... Ts> class IsOnCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
123 public:
124 bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_ON; }
125};
126
127template<typename... Ts> class IsOffCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
128 public:
129 bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_OFF; }
130};
131
132template<typename... Ts> class IsMutedCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
133 public:
134 bool check(const Ts &...x) override { return this->parent_->is_muted(); }
135};
136
137} // namespace esphome::media_player
virtual void play(const Ts &...x)=0
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:642
Base class for all automation conditions.
Definition automation.h:459
Helper class to easily give an object a parent of type T.
Definition helpers.h:1861
bool check(const Ts &...x) override
Definition automation.h:117
bool check(const Ts &...x) override
Definition automation.h:102
bool check(const Ts &...x) override
Definition automation.h:134
bool check(const Ts &...x) override
Definition automation.h:129
bool check(const Ts &...x) override
Definition automation.h:124
bool check(const Ts &...x) override
Definition automation.h:112
bool check(const Ts &...x) override
Definition automation.h:107
bool state
Definition fan.h:2
Callback forwarder that triggers an Automation<> on any state change.
Definition automation.h:80
void operator()(MediaPlayerState) const
Definition automation.h:82
Callback forwarder that triggers an Automation<> only when a specific media player state is entered.
Definition automation.h:87
void operator()(MediaPlayerState state) const
Definition automation.h:89
uint16_t x
Definition tt21100.cpp:5