ESPHome 2026.5.0-dev
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(const 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>
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>
55template<typename... Ts>
57
58template<MediaPlayerCommand Command, typename... Ts>
59class MediaPlayerMediaAction : public Action<Ts...>, public Parented<MediaPlayer> {
60 TEMPLATABLE_VALUE(std::string, media_url)
61 TEMPLATABLE_VALUE(bool, announcement)
62 void play(const Ts &...x) override {
63 auto call = this->parent_->make_call();
64 if constexpr (Command != MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PLAY)
65 call.set_command(Command);
66 call.set_media_url(this->media_url_.value(x...)).set_announcement(this->announcement_.value(x...)).perform();
67 }
68};
69
70template<typename... Ts>
72template<typename... Ts>
74
75template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<MediaPlayer> {
76 TEMPLATABLE_VALUE(float, volume)
77 void play(const Ts &...x) override { this->parent_->make_call().set_volume(this->volume_.value(x...)).perform(); }
78};
79
84 void operator()(MediaPlayerState /*state*/) const { this->automation->trigger(); }
85};
86
89template<MediaPlayerState State> struct StateEnterForwarder {
92 if (state == State)
93 this->automation->trigger();
94 }
95};
96
97static_assert(sizeof(StateAnyForwarder) <= sizeof(void *));
98static_assert(std::is_trivially_copyable_v<StateAnyForwarder>);
99static_assert(sizeof(StateEnterForwarder<MediaPlayerState::MEDIA_PLAYER_STATE_IDLE>) <= sizeof(void *));
100static_assert(std::is_trivially_copyable_v<StateEnterForwarder<MediaPlayerState::MEDIA_PLAYER_STATE_IDLE>>);
101
102template<typename... Ts> class IsIdleCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
103 public:
104 bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_IDLE; }
105};
106
107template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
108 public:
109 bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; }
110};
111
112template<typename... Ts> class IsPausedCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
113 public:
114 bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PAUSED; }
115};
116
117template<typename... Ts> class IsAnnouncingCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
118 public:
119 bool check(const Ts &...x) override {
121 }
122};
123
124template<typename... Ts> class IsOnCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
125 public:
126 bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_ON; }
127};
128
129template<typename... Ts> class IsOffCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
130 public:
131 bool check(const Ts &...x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_OFF; }
132};
133
134template<typename... Ts> class IsMutedCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
135 public:
136 bool check(const Ts &...x) override { return this->parent_->is_muted(); }
137};
138
139} // namespace media_player
140} // namespace esphome
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:2013
bool check(const Ts &...x) override
Definition automation.h:119
bool check(const Ts &...x) override
Definition automation.h:104
bool check(const Ts &...x) override
Definition automation.h:136
bool check(const Ts &...x) override
Definition automation.h:131
bool check(const Ts &...x) override
Definition automation.h:126
bool check(const Ts &...x) override
Definition automation.h:114
bool check(const Ts &...x) override
Definition automation.h:109
bool state
Definition fan.h:2
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Callback forwarder that triggers an Automation<> on any state change.
Definition automation.h:82
void operator()(MediaPlayerState) const
Definition automation.h:84
Callback forwarder that triggers an Automation<> only when a specific media player state is entered.
Definition automation.h:89
void operator()(MediaPlayerState state) const
Definition automation.h:91
uint16_t x
Definition tt21100.cpp:5