ESPHome 2025.6.3
Loading...
Searching...
No Matches
media_player.cpp
Go to the documentation of this file.
1#include "media_player.h"
2
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace media_player {
7
8static const char *const TAG = "media_player";
9
11 switch (state) {
13 return "IDLE";
15 return "PLAYING";
17 return "PAUSED";
19 return "ANNOUNCING";
21 default:
22 return "UNKNOWN";
23 }
24}
25
27 switch (command) {
29 return "PLAY";
31 return "PAUSE";
33 return "STOP";
35 return "MUTE";
37 return "UNMUTE";
39 return "TOGGLE";
41 return "VOLUME_UP";
43 return "VOLUME_DOWN";
45 return "ENQUEUE";
47 return "REPEAT_ONE";
49 return "REPEAT_OFF";
51 return "CLEAR_PLAYLIST";
52 default:
53 return "UNKNOWN";
54 }
55}
56
58 if (this->media_url_.has_value()) {
59 if (this->command_.has_value() && this->command_.value() != MEDIA_PLAYER_COMMAND_ENQUEUE) {
60 // Don't remove an enqueue command
61 ESP_LOGW(TAG, "MediaPlayerCall: Setting both command and media_url is not needed.");
62 this->command_.reset();
63 }
64 }
65 if (this->volume_.has_value()) {
66 if (this->volume_.value() < 0.0f || this->volume_.value() > 1.0f) {
67 ESP_LOGW(TAG, "MediaPlayerCall: Volume must be between 0.0 and 1.0.");
68 this->volume_.reset();
69 }
70 }
71}
72
74 ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
75 this->validate_();
76 if (this->command_.has_value()) {
77 const char *command_s = media_player_command_to_string(this->command_.value());
78 ESP_LOGD(TAG, " Command: %s", command_s);
79 }
80 if (this->media_url_.has_value()) {
81 ESP_LOGD(TAG, " Media URL: %s", this->media_url_.value().c_str());
82 }
83 if (this->volume_.has_value()) {
84 ESP_LOGD(TAG, " Volume: %.2f", this->volume_.value());
85 }
86 if (this->announcement_.has_value()) {
87 ESP_LOGD(TAG, " Announcement: %s", this->announcement_.value() ? "yes" : "no");
88 }
89 this->parent_->control(*this);
90}
91
93 this->command_ = command;
94 return *this;
95}
97 this->command_ = command;
98 return *this;
99}
100MediaPlayerCall &MediaPlayerCall::set_command(const std::string &command) {
101 if (str_equals_case_insensitive(command, "PLAY")) {
103 } else if (str_equals_case_insensitive(command, "PAUSE")) {
105 } else if (str_equals_case_insensitive(command, "STOP")) {
107 } else if (str_equals_case_insensitive(command, "MUTE")) {
109 } else if (str_equals_case_insensitive(command, "UNMUTE")) {
111 } else if (str_equals_case_insensitive(command, "TOGGLE")) {
113 } else {
114 ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command.c_str());
115 }
116 return *this;
117}
118
119MediaPlayerCall &MediaPlayerCall::set_media_url(const std::string &media_url) {
120 this->media_url_ = media_url;
121 return *this;
122}
123
125 this->volume_ = volume;
126 return *this;
127}
128
130 this->announcement_ = announce;
131 return *this;
132}
133
134void MediaPlayer::add_on_state_callback(std::function<void()> &&callback) {
135 this->state_callback_.add(std::move(callback));
136}
137
139
140} // namespace media_player
141} // namespace esphome
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:69
MediaPlayerCall & set_media_url(const std::string &url)
MediaPlayerCall & set_volume(float volume)
optional< std::string > media_url_
MediaPlayerCall & set_announcement(bool announce)
MediaPlayerCall & set_command(MediaPlayerCommand command)
optional< MediaPlayerCommand > command_
virtual void control(const MediaPlayerCall &call)=0
CallbackManager< void()> state_callback_
void add_on_state_callback(std::function< void()> &&callback)
bool has_value() const
Definition optional.h:87
value_type const & value() const
Definition optional.h:89
bool state
Definition fan.h:0
const char * media_player_command_to_string(MediaPlayerCommand command)
const char * media_player_state_to_string(MediaPlayerState state)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition helpers.cpp:262