ESPHome  2022.9.1
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 {
14 };
16 
17 enum MediaPlayerCommand : uint8_t {
26 };
28 
29 class MediaPlayer;
30 
32  public:
33  MediaPlayerTraits() = default;
34 
35  void set_supports_pause(bool supports_pause) { this->supports_pause_ = supports_pause; }
36 
37  bool get_supports_pause() const { return this->supports_pause_; }
38 
39  protected:
40  bool supports_pause_{false};
41 };
42 
44  public:
45  MediaPlayerCall(MediaPlayer *parent) : parent_(parent) {}
46 
47  MediaPlayerCall &set_command(MediaPlayerCommand command);
48  MediaPlayerCall &set_command(optional<MediaPlayerCommand> command);
49  MediaPlayerCall &set_command(const std::string &command);
50 
51  MediaPlayerCall &set_media_url(const std::string &url);
52 
53  MediaPlayerCall &set_volume(float volume);
54 
55  void perform();
56 
57  const optional<MediaPlayerCommand> &get_command() const { return command_; }
58  const optional<std::string> &get_media_url() const { return media_url_; }
59  const optional<float> &get_volume() const { return volume_; }
60 
61  protected:
62  void validate_();
67 };
68 
69 class MediaPlayer : public EntityBase {
70  public:
72  float volume{1.0f};
73 
75 
76  void publish_state();
77 
78  void add_on_state_callback(std::function<void()> &&callback);
79 
80  virtual bool is_muted() const { return false; }
81 
82  virtual MediaPlayerTraits get_traits() = 0;
83 
84  protected:
86 
87  virtual void control(const MediaPlayerCall &call) = 0;
88 
89  CallbackManager<void()> state_callback_{};
90 };
91 
92 } // namespace media_player
93 } // namespace esphome
virtual bool is_muted() const
Definition: media_player.h:80
const optional< float > & get_volume() const
Definition: media_player.h:59
const char * media_player_state_to_string(MediaPlayerState state)
const char * media_player_command_to_string(MediaPlayerCommand command)
const optional< MediaPlayerCommand > & get_command() const
Definition: media_player.h:57
const optional< std::string > & get_media_url() const
Definition: media_player.h:58
MediaPlayerCall(MediaPlayer *parent)
Definition: media_player.h:45
Definition: a4988.cpp:4
void set_supports_pause(bool supports_pause)
Definition: media_player.h:35
bool state
Definition: fan.h:34
optional< std::string > media_url_
Definition: media_player.h:65
optional< MediaPlayerCommand > command_
Definition: media_player.h:64