ESPHome  2024.4.0
media_player.cpp
Go to the documentation of this file.
1 #include "media_player.h"
2 
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace media_player {
7 
8 static const char *const TAG = "media_player";
9 
11  switch (state) {
13  return "IDLE";
15  return "PLAYING";
17  return "PAUSED";
19  default:
20  return "UNKNOWN";
21  }
22 }
23 
25  switch (command) {
27  return "PLAY";
29  return "PAUSE";
31  return "STOP";
33  return "MUTE";
35  return "UNMUTE";
37  return "TOGGLE";
38  default:
39  return "UNKNOWN";
40  }
41 }
42 
44  if (this->media_url_.has_value()) {
45  if (this->command_.has_value()) {
46  ESP_LOGW(TAG, "MediaPlayerCall: Setting both command and media_url is not needed.");
47  this->command_.reset();
48  }
49  }
50  if (this->volume_.has_value()) {
51  if (this->volume_.value() < 0.0f || this->volume_.value() > 1.0f) {
52  ESP_LOGW(TAG, "MediaPlayerCall: Volume must be between 0.0 and 1.0.");
53  this->volume_.reset();
54  }
55  }
56 }
57 
59  ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
60  this->validate_();
61  if (this->command_.has_value()) {
62  const char *command_s = media_player_command_to_string(this->command_.value());
63  ESP_LOGD(TAG, " Command: %s", command_s);
64  }
65  if (this->media_url_.has_value()) {
66  ESP_LOGD(TAG, " Media URL: %s", this->media_url_.value().c_str());
67  }
68  if (this->volume_.has_value()) {
69  ESP_LOGD(TAG, " Volume: %.2f", this->volume_.value());
70  }
71  this->parent_->control(*this);
72 }
73 
75  this->command_ = command;
76  return *this;
77 }
79  this->command_ = command;
80  return *this;
81 }
82 MediaPlayerCall &MediaPlayerCall::set_command(const std::string &command) {
83  if (str_equals_case_insensitive(command, "PLAY")) {
85  } else if (str_equals_case_insensitive(command, "PAUSE")) {
87  } else if (str_equals_case_insensitive(command, "STOP")) {
89  } else if (str_equals_case_insensitive(command, "MUTE")) {
91  } else if (str_equals_case_insensitive(command, "UNMUTE")) {
93  } else if (str_equals_case_insensitive(command, "TOGGLE")) {
95  } else {
96  ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command.c_str());
97  }
98  return *this;
99 }
100 
101 MediaPlayerCall &MediaPlayerCall::set_media_url(const std::string &media_url) {
102  this->media_url_ = media_url;
103  return *this;
104 }
105 
107  this->volume_ = volume;
108  return *this;
109 }
110 
111 void MediaPlayer::add_on_state_callback(std::function<void()> &&callback) {
112  this->state_callback_.add(std::move(callback));
113 }
114 
115 void MediaPlayer::publish_state() { this->state_callback_.call(); }
116 
117 } // namespace media_player
118 } // namespace esphome
value_type const & value() const
Definition: optional.h:89
MediaPlayerCall & set_command(MediaPlayerCommand command)
bool has_value() const
Definition: optional.h:87
const char * media_player_state_to_string(MediaPlayerState state)
const char * media_player_command_to_string(MediaPlayerCommand command)
void add_on_state_callback(std::function< void()> &&callback)
constexpr const char * c_str() const
Definition: string_ref.h:68
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
virtual void control(const MediaPlayerCall &call)=0
MediaPlayerCall & set_media_url(const std::string &url)
const StringRef & get_name() const
Definition: entity_base.cpp:10
MediaPlayerCall & set_volume(float volume)
bool state
Definition: fan.h:34
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition: helpers.cpp:256
optional< std::string > media_url_
Definition: media_player.h:65
optional< MediaPlayerCommand > command_
Definition: media_player.h:64