ESPHome 2025.12.0-dev
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 {
9namespace speaker {
10
11template<typename... Ts> class PlayAction : public Action<Ts...>, public Parented<Speaker> {
12 public:
13 void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
14 this->data_.func = func;
15 this->len_ = -1; // Sentinel value indicates template mode
16 }
17
18 void set_data_static(const uint8_t *data, size_t len) {
19 this->data_.data = data;
20 this->len_ = len; // Length >= 0 indicates static mode
21 }
22
23 void play(const Ts &...x) override {
24 if (this->len_ >= 0) {
25 // Static mode: pass pointer directly to play(const uint8_t *, size_t)
26 this->parent_->play(this->data_.data, static_cast<size_t>(this->len_));
27 } else {
28 // Template mode: call function and pass vector to play(const std::vector<uint8_t> &)
29 auto val = this->data_.func(x...);
30 this->parent_->play(val);
31 }
32 }
33
34 protected:
35 ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length
36 union Data {
37 std::vector<uint8_t> (*func)(Ts...); // Function pointer (stateless lambdas)
38 const uint8_t *data; // Pointer to static data in flash
40};
41
42template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<Speaker> {
43 TEMPLATABLE_VALUE(float, volume)
44 void play(const Ts &...x) override { this->parent_->set_volume(this->volume_.value(x...)); }
45};
46
47template<typename... Ts> class MuteOnAction : public Action<Ts...> {
48 public:
49 explicit MuteOnAction(Speaker *speaker) : speaker_(speaker) {}
50
51 void play(const Ts &...x) override { this->speaker_->set_mute_state(true); }
52
53 protected:
55};
56
57template<typename... Ts> class MuteOffAction : public Action<Ts...> {
58 public:
59 explicit MuteOffAction(Speaker *speaker) : speaker_(speaker) {}
60
61 void play(const Ts &...x) override { this->speaker_->set_mute_state(false); }
62
63 protected:
65};
66
67template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Speaker> {
68 public:
69 void play(const Ts &...x) override { this->parent_->stop(); }
70};
71
72template<typename... Ts> class FinishAction : public Action<Ts...>, public Parented<Speaker> {
73 public:
74 void play(const Ts &...x) override { this->parent_->finish(); }
75};
76
77template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<Speaker> {
78 public:
79 bool check(const Ts &...x) override { return this->parent_->is_running(); }
80};
81
82template<typename... Ts> class IsStoppedCondition : public Condition<Ts...>, public Parented<Speaker> {
83 public:
84 bool check(const Ts &...x) override { return this->parent_->is_stopped(); }
85};
86
87} // namespace speaker
88} // namespace esphome
virtual void play(const Ts &...x)=0
Base class for all automation conditions.
Definition automation.h:148
Helper class to easily give an object a parent of type T.
Definition helpers.h:918
void play(const Ts &...x) override
Definition automation.h:74
bool check(const Ts &...x) override
Definition automation.h:79
bool check(const Ts &...x) override
Definition automation.h:84
MuteOffAction(Speaker *speaker)
Definition automation.h:59
void play(const Ts &...x) override
Definition automation.h:61
MuteOnAction(Speaker *speaker)
Definition automation.h:49
void play(const Ts &...x) override
Definition automation.h:51
void play(const Ts &...x) override
Definition automation.h:23
union esphome::speaker::PlayAction::Data data_
void set_data_template(std::vector< uint8_t >(*func)(Ts...))
Definition automation.h:13
void set_data_static(const uint8_t *data, size_t len)
Definition automation.h:18
virtual void set_mute_state(bool mute_state)
Definition speaker.h:81
void play(const Ts &...x) override
Definition automation.h:69
__int64 ssize_t
Definition httplib.h:178
mopeka_std_values val[4]
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:500
uint16_t x
Definition tt21100.cpp:5
std::vector< uint8_t >(* func)(Ts...)
Definition automation.h:37