ESPHome  2024.5.4
media_player.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/helpers.h"
5 
6 namespace esphome {
7 namespace media_player {
8 
9 enum MediaPlayerState : uint8_t {
15 };
17 
18 enum MediaPlayerCommand : uint8_t {
27 };
29 
30 class MediaPlayer;
31 
33  public:
34  MediaPlayerTraits() = default;
35 
36  void set_supports_pause(bool supports_pause) { this->supports_pause_ = supports_pause; }
37 
38  bool get_supports_pause() const { return this->supports_pause_; }
39 
40  protected:
41  bool supports_pause_{false};
42 };
43 
45  public:
46  MediaPlayerCall(MediaPlayer *parent) : parent_(parent) {}
47 
48  MediaPlayerCall &set_command(MediaPlayerCommand command);
49  MediaPlayerCall &set_command(optional<MediaPlayerCommand> command);
50  MediaPlayerCall &set_command(const std::string &command);
51 
52  MediaPlayerCall &set_media_url(const std::string &url);
53 
54  MediaPlayerCall &set_volume(float volume);
55  MediaPlayerCall &set_announcement(bool announce);
56 
57  void perform();
58 
59  const optional<MediaPlayerCommand> &get_command() const { return command_; }
60  const optional<std::string> &get_media_url() const { return media_url_; }
61  const optional<float> &get_volume() const { return volume_; }
62  const optional<bool> &get_announcement() const { return announcement_; }
63 
64  protected:
65  void validate_();
71 };
72 
73 class MediaPlayer : public EntityBase {
74  public:
76  float volume{1.0f};
77 
79 
80  void publish_state();
81 
82  void add_on_state_callback(std::function<void()> &&callback);
83 
84  virtual bool is_muted() const { return false; }
85 
86  virtual MediaPlayerTraits get_traits() = 0;
87 
88  protected:
90 
91  virtual void control(const MediaPlayerCall &call) = 0;
92 
93  CallbackManager<void()> state_callback_{};
94 };
95 
96 } // namespace media_player
97 } // namespace esphome
virtual bool is_muted() const
Definition: media_player.h:84
const optional< float > & get_volume() const
Definition: media_player.h:61
const char * media_player_state_to_string(MediaPlayerState state)
const char * media_player_command_to_string(MediaPlayerCommand command)
const optional< bool > & get_announcement() const
Definition: media_player.h:62
const optional< MediaPlayerCommand > & get_command() const
Definition: media_player.h:59
const optional< std::string > & get_media_url() const
Definition: media_player.h:60
MediaPlayerCall(MediaPlayer *parent)
Definition: media_player.h:46
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_supports_pause(bool supports_pause)
Definition: media_player.h:36
bool state
Definition: fan.h:34
optional< std::string > media_url_
Definition: media_player.h:68
optional< MediaPlayerCommand > command_
Definition: media_player.h:67