ESPHome  2024.11.0
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 enum class MediaPlayerFormatPurpose : uint8_t {
31  PURPOSE_DEFAULT = 0,
33 };
34 
36  std::string format;
37  uint32_t sample_rate;
38  uint32_t num_channels;
40  uint32_t sample_bytes;
41 };
42 
43 class MediaPlayer;
44 
46  public:
47  MediaPlayerTraits() = default;
48 
49  void set_supports_pause(bool supports_pause) { this->supports_pause_ = supports_pause; }
50 
51  bool get_supports_pause() const { return this->supports_pause_; }
52 
53  std::vector<MediaPlayerSupportedFormat> &get_supported_formats() { return this->supported_formats_; }
54 
55  protected:
56  bool supports_pause_{false};
57  std::vector<MediaPlayerSupportedFormat> supported_formats_{};
58 };
59 
61  public:
62  MediaPlayerCall(MediaPlayer *parent) : parent_(parent) {}
63 
64  MediaPlayerCall &set_command(MediaPlayerCommand command);
65  MediaPlayerCall &set_command(optional<MediaPlayerCommand> command);
66  MediaPlayerCall &set_command(const std::string &command);
67 
68  MediaPlayerCall &set_media_url(const std::string &url);
69 
70  MediaPlayerCall &set_volume(float volume);
71  MediaPlayerCall &set_announcement(bool announce);
72 
73  void perform();
74 
75  const optional<MediaPlayerCommand> &get_command() const { return command_; }
76  const optional<std::string> &get_media_url() const { return media_url_; }
77  const optional<float> &get_volume() const { return volume_; }
78  const optional<bool> &get_announcement() const { return announcement_; }
79 
80  protected:
81  void validate_();
87 };
88 
89 class MediaPlayer : public EntityBase {
90  public:
92  float volume{1.0f};
93 
95 
96  void publish_state();
97 
98  void add_on_state_callback(std::function<void()> &&callback);
99 
100  virtual bool is_muted() const { return false; }
101 
102  virtual MediaPlayerTraits get_traits() = 0;
103 
104  protected:
106 
107  virtual void control(const MediaPlayerCall &call) = 0;
108 
109  CallbackManager<void()> state_callback_{};
110 };
111 
112 } // namespace media_player
113 } // namespace esphome
const optional< float > & get_volume() const
Definition: media_player.h:77
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:78
std::vector< MediaPlayerSupportedFormat > & get_supported_formats()
Definition: media_player.h:53
const optional< MediaPlayerCommand > & get_command() const
Definition: media_player.h:75
const optional< std::string > & get_media_url() const
Definition: media_player.h:76
MediaPlayerCall(MediaPlayer *parent)
Definition: media_player.h:62
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_supports_pause(bool supports_pause)
Definition: media_player.h:49
bool state
Definition: fan.h:34
optional< std::string > media_url_
Definition: media_player.h:84
optional< MediaPlayerCommand > command_
Definition: media_player.h:83