ESPHome 2025.8.0-dev
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 "ON";
15 return "OFF";
17 return "IDLE";
19 return "PLAYING";
21 return "PAUSED";
23 return "ANNOUNCING";
25 return "NONE";
26 default:
27 return "UNKNOWN";
28 }
29}
30
32 switch (command) {
34 return "PLAY";
36 return "PAUSE";
38 return "STOP";
40 return "MUTE";
42 return "UNMUTE";
44 return "TOGGLE";
46 return "VOLUME_UP";
48 return "VOLUME_DOWN";
50 return "ENQUEUE";
52 return "REPEAT_ONE";
54 return "REPEAT_OFF";
56 return "CLEAR_PLAYLIST";
58 return "TURN_ON";
60 return "TURN_OFF";
61 default:
62 return "UNKNOWN";
63 }
64}
65
67 if (this->media_url_.has_value()) {
68 if (this->command_.has_value() && this->command_.value() != MEDIA_PLAYER_COMMAND_ENQUEUE) {
69 // Don't remove an enqueue command
70 ESP_LOGW(TAG, "MediaPlayerCall: Setting both command and media_url is not needed.");
71 this->command_.reset();
72 }
73 }
74 if (this->volume_.has_value()) {
75 if (this->volume_.value() < 0.0f || this->volume_.value() > 1.0f) {
76 ESP_LOGW(TAG, "MediaPlayerCall: Volume must be between 0.0 and 1.0.");
77 this->volume_.reset();
78 }
79 }
80}
81
83 ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
84 this->validate_();
85 if (this->command_.has_value()) {
86 const char *command_s = media_player_command_to_string(this->command_.value());
87 ESP_LOGD(TAG, " Command: %s", command_s);
88 }
89 if (this->media_url_.has_value()) {
90 ESP_LOGD(TAG, " Media URL: %s", this->media_url_.value().c_str());
91 }
92 if (this->volume_.has_value()) {
93 ESP_LOGD(TAG, " Volume: %.2f", this->volume_.value());
94 }
95 if (this->announcement_.has_value()) {
96 ESP_LOGD(TAG, " Announcement: %s", this->announcement_.value() ? "yes" : "no");
97 }
98 this->parent_->control(*this);
99}
100
102 this->command_ = command;
103 return *this;
104}
106 this->command_ = command;
107 return *this;
108}
109MediaPlayerCall &MediaPlayerCall::set_command(const std::string &command) {
110 if (str_equals_case_insensitive(command, "PLAY")) {
112 } else if (str_equals_case_insensitive(command, "PAUSE")) {
114 } else if (str_equals_case_insensitive(command, "STOP")) {
116 } else if (str_equals_case_insensitive(command, "MUTE")) {
118 } else if (str_equals_case_insensitive(command, "UNMUTE")) {
120 } else if (str_equals_case_insensitive(command, "TOGGLE")) {
122 } else if (str_equals_case_insensitive(command, "TURN_ON")) {
124 } else if (str_equals_case_insensitive(command, "TURN_OFF")) {
126 } else {
127 ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command.c_str());
128 }
129 return *this;
130}
131
132MediaPlayerCall &MediaPlayerCall::set_media_url(const std::string &media_url) {
133 this->media_url_ = media_url;
134 return *this;
135}
136
138 this->volume_ = volume;
139 return *this;
140}
141
143 this->announcement_ = announce;
144 return *this;
145}
146
147void MediaPlayer::add_on_state_callback(std::function<void()> &&callback) {
148 this->state_callback_.add(std::move(callback));
149}
150
152
153} // namespace media_player
154} // 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)
MediaPlayerCall & set_command(const std::string &command)
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:92
value_type const & value() const
Definition optional.h:94
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:147