ESPHome 2026.5.1
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
4#include "speaker.h"
5
6#include <vector>
7
8namespace esphome::speaker {
9
10template<typename... Ts> class PlayAction : public Action<Ts...>, public Parented<Speaker> {
11 public:
12 void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
13 this->data_.func = func;
14 this->len_ = -1; // Sentinel value indicates template mode
15 }
16
17 void set_data_static(const uint8_t *data, size_t len) {
18 this->data_.data = data;
19 this->len_ = len; // Length >= 0 indicates static mode
20 }
21
22 void play(const Ts &...x) override {
23 if (this->len_ >= 0) {
24 // Static mode: pass pointer directly to play(const uint8_t *, size_t)
25 this->parent_->play(this->data_.data, static_cast<size_t>(this->len_));
26 } else {
27 // Template mode: call function and pass vector to play(const std::vector<uint8_t> &)
28 auto val = this->data_.func(x...);
29 this->parent_->play(val);
30 }
31 }
32
33 protected:
34 ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length
35 union Data {
36 std::vector<uint8_t> (*func)(Ts...); // Function pointer (stateless lambdas)
37 const uint8_t *data; // Pointer to static data in flash
39};
40
41template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<Speaker> {
42 TEMPLATABLE_VALUE(float, volume)
43 void play(const Ts &...x) override { this->parent_->set_volume(this->volume_.value(x...)); }
44};
45
46template<typename... Ts> class MuteOnAction : public Action<Ts...> {
47 public:
48 explicit MuteOnAction(Speaker *speaker) : speaker_(speaker) {}
49
50 void play(const Ts &...x) override { this->speaker_->set_mute_state(true); }
51
52 protected:
54};
55
56template<typename... Ts> class MuteOffAction : public Action<Ts...> {
57 public:
58 explicit MuteOffAction(Speaker *speaker) : speaker_(speaker) {}
59
60 void play(const Ts &...x) override { this->speaker_->set_mute_state(false); }
61
62 protected:
64};
65
66template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Speaker> {
67 public:
68 void play(const Ts &...x) override { this->parent_->stop(); }
69};
70
71template<typename... Ts> class FinishAction : public Action<Ts...>, public Parented<Speaker> {
72 public:
73 void play(const Ts &...x) override { this->parent_->finish(); }
74};
75
76template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<Speaker> {
77 public:
78 bool check(const Ts &...x) override { return this->parent_->is_running(); }
79};
80
81template<typename... Ts> class IsStoppedCondition : public Condition<Ts...>, public Parented<Speaker> {
82 public:
83 bool check(const Ts &...x) override { return this->parent_->is_stopped(); }
84};
85
86} // namespace esphome::speaker
virtual void play(const Ts &...x)=0
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
void play(const Ts &...x) override
Definition automation.h:73
bool check(const Ts &...x) override
Definition automation.h:78
bool check(const Ts &...x) override
Definition automation.h:83
MuteOffAction(Speaker *speaker)
Definition automation.h:58
void play(const Ts &...x) override
Definition automation.h:60
MuteOnAction(Speaker *speaker)
Definition automation.h:48
void play(const Ts &...x) override
Definition automation.h:50
void play(const Ts &...x) override
Definition automation.h:22
union esphome::speaker::PlayAction::Data data_
void set_data_template(std::vector< uint8_t >(*func)(Ts...))
Definition automation.h:12
void set_data_static(const uint8_t *data, size_t len)
Definition automation.h:17
virtual void set_mute_state(bool mute_state)
Definition speaker.h:80
void play(const Ts &...x) override
Definition automation.h:68
__int64 ssize_t
Definition httplib.h:178
mopeka_std_values val[3]
std::string size_t len
uint16_t x
Definition tt21100.cpp:5
std::vector< uint8_t >(* func)(Ts...)
Definition automation.h:36