ESPHome  2024.11.0
automation.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "speaker.h"
5 
6 #include <vector>
7 
8 namespace esphome {
9 namespace speaker {
10 
11 template<typename... Ts> class PlayAction : public Action<Ts...>, public Parented<Speaker> {
12  public:
13  void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) {
14  this->data_func_ = func;
15  this->static_ = false;
16  }
17  void set_data_static(const std::vector<uint8_t> &data) {
18  this->data_static_ = data;
19  this->static_ = true;
20  }
21 
22  void play(Ts... x) override {
23  if (this->static_) {
24  this->parent_->play(this->data_static_);
25  } else {
26  auto val = this->data_func_(x...);
27  this->parent_->play(val);
28  }
29  }
30 
31  protected:
32  bool static_{false};
33  std::function<std::vector<uint8_t>(Ts...)> data_func_{};
34  std::vector<uint8_t> data_static_{};
35 };
36 
37 template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<Speaker> {
38  TEMPLATABLE_VALUE(float, volume)
39  void play(Ts... x) override { this->parent_->set_volume(this->volume_.value(x...)); }
40 };
41 
42 template<typename... Ts> class MuteOnAction : public Action<Ts...> {
43  public:
44  explicit MuteOnAction(Speaker *speaker) : speaker_(speaker) {}
45 
46  void play(Ts... x) override { this->speaker_->set_mute_state(true); }
47 
48  protected:
50 };
51 
52 template<typename... Ts> class MuteOffAction : public Action<Ts...> {
53  public:
54  explicit MuteOffAction(Speaker *speaker) : speaker_(speaker) {}
55 
56  void play(Ts... x) override { this->speaker_->set_mute_state(false); }
57 
58  protected:
60 };
61 
62 template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Speaker> {
63  public:
64  void play(Ts... x) override { this->parent_->stop(); }
65 };
66 
67 template<typename... Ts> class FinishAction : public Action<Ts...>, public Parented<Speaker> {
68  public:
69  void play(Ts... x) override { this->parent_->finish(); }
70 };
71 
72 template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<Speaker> {
73  public:
74  bool check(Ts... x) override { return this->parent_->is_running(); }
75 };
76 
77 template<typename... Ts> class IsStoppedCondition : public Condition<Ts...>, public Parented<Speaker> {
78  public:
79  bool check(Ts... x) override { return this->parent_->is_stopped(); }
80 };
81 
82 } // namespace speaker
83 } // namespace esphome
void set_data_template(std::function< std::vector< uint8_t >(Ts...)> func)
Definition: automation.h:13
void set_data_static(const std::vector< uint8_t > &data)
Definition: automation.h:17
std::function< std::vector< uint8_t >Ts...)> data_func_
Definition: automation.h:33
uint16_t x
Definition: tt21100.cpp:17
mopeka_std_values val[4]
std::vector< uint8_t > data_static_
Definition: automation.h:34
MuteOffAction(Speaker *speaker)
Definition: automation.h:54
void play(Ts... x) override
Definition: automation.h:22
Base class for all automation conditions.
Definition: automation.h:74
MuteOnAction(Speaker *speaker)
Definition: automation.h:44
void play(Ts... x) override
Definition: automation.h:69
void play(Ts... x) override
Definition: automation.h:56
void play(Ts... x) override
Definition: automation.h:64
void play(Ts... x) override
Definition: automation.h:46
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool check(Ts... x) override
Definition: automation.h:74
bool check(Ts... x) override
Definition: automation.h:79
Helper class to easily give an object a parent of type T.
Definition: helpers.h:522