ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
announcement_volume.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <utility>
5
6namespace esphome::speaker {
7
11 public:
13 template<typename F> void set_volume(float volume, F &&apply) {
14 this->volume_ = volume;
15 if (this->active_) {
16 std::forward<F>(apply)(volume);
17 }
18 }
19 float get_volume() const { return this->volume_; }
20 bool is_configured() const { return !std::isnan(this->volume_); }
21 bool is_active() const { return this->active_; }
22
25 template<typename F> void begin(bool other_audio_playing, F &&apply) {
26 if (this->active_ || other_audio_playing || !this->is_configured()) {
27 return;
28 }
29 this->active_ = true;
30 this->log_begin_();
31 std::forward<F>(apply)(this->volume_);
32 }
33
35 template<typename F> void end(float media_volume, F &&apply) {
36 if (!this->active_) {
37 return;
38 }
39 this->active_ = false;
40 this->log_end_(media_volume);
41 std::forward<F>(apply)(media_volume);
42 }
43
45 void cancel() { this->active_ = false; }
46
51 template<typename F>
52 void item_started(bool is_announcement, bool other_audio_playing, float media_volume, F &&apply) {
53 if (!is_announcement) {
54 this->end(media_volume, apply);
55 } else if (!this->last_item_announcement_) {
56 this->begin(other_audio_playing, apply);
57 }
58 this->last_item_announcement_ = is_announcement;
59 }
60
62 template<typename F> void playlist_finished(float media_volume, F &&apply) {
63 this->last_item_announcement_ = false;
64 this->end(media_volume, std::forward<F>(apply));
65 }
66
67 protected:
68 void log_begin_() const;
69 void log_end_(float media_volume) const;
70
71 float volume_{NAN};
72 bool active_{false};
74};
75
76} // namespace esphome::speaker
Tracks a separate volume for announcements that start while nothing else is playing.
void cancel()
The media player volume was set and already sent to the speakers; it applies until the next announcem...
void item_started(bool is_announcement, bool other_audio_playing, float media_volume, F &&apply)
Called when an item starts on a playlist that holds announcements, and may hold media too.
void begin(bool other_audio_playing, F &&apply)
Called when an announcement starts.
void set_volume(float volume, F &&apply)
Stores a new announcement volume and applies it right away if it is in use.
void playlist_finished(float media_volume, F &&apply)
Called when the playlist that holds announcements has finished. Switches back to the media volume.
void log_end_(float media_volume) const
void end(float media_volume, F &&apply)
Switches back to the media player volume if the announcement volume is in use.
void apply(Climate *climate)
Apply these settings to the climate device.
uint8_t end[39]
Definition sun_gtil2.cpp:17