From d63e14a4b68495f9eb6dccc2f0450553037cc845 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 8 Jun 2022 22:33:21 +1200 Subject: [PATCH] Implement the media player actions (#3534) --- .../i2s_audio/i2s_audio_media_player.cpp | 8 +++++ esphome/components/media_player/__init__.py | 8 ++++- esphome/components/media_player/automation.h | 35 +++++++++++++++++++ .../components/media_player/media_player.cpp | 4 +++ .../components/media_player/media_player.h | 3 +- 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 esphome/components/media_player/automation.h diff --git a/esphome/components/i2s_audio/i2s_audio_media_player.cpp b/esphome/components/i2s_audio/i2s_audio_media_player.cpp index 0ab3237aeb..9ddc8419bf 100644 --- a/esphome/components/i2s_audio/i2s_audio_media_player.cpp +++ b/esphome/components/i2s_audio/i2s_audio_media_player.cpp @@ -43,6 +43,14 @@ void I2SAudioMediaPlayer::control(const media_player::MediaPlayerCall &call) { case media_player::MEDIA_PLAYER_COMMAND_UNMUTE: this->unmute_(); break; + case media_player::MEDIA_PLAYER_COMMAND_TOGGLE: + this->audio_->pauseResume(); + if (this->audio_->isRunning()) { + this->state = media_player::MEDIA_PLAYER_STATE_PLAYING; + } else { + this->state = media_player::MEDIA_PLAYER_STATE_PAUSED; + } + break; } } this->publish_state(); diff --git a/esphome/components/media_player/__init__.py b/esphome/components/media_player/__init__.py index 91cf613ed9..98b93a7ee7 100644 --- a/esphome/components/media_player/__init__.py +++ b/esphome/components/media_player/__init__.py @@ -20,6 +20,9 @@ MediaPlayer = media_player_ns.class_("MediaPlayer") PlayAction = media_player_ns.class_( "PlayAction", automation.Action, cg.Parented.template(MediaPlayer) ) +ToggleAction = media_player_ns.class_( + "ToggleAction", automation.Action, cg.Parented.template(MediaPlayer) +) PauseAction = media_player_ns.class_( "PauseAction", automation.Action, cg.Parented.template(MediaPlayer) ) @@ -42,12 +45,15 @@ async def register_media_player(var, config): MEDIA_PLAYER_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.Schema({})) -MEDIA_PLAYER_ACTION_SCHEMA = maybe_simple_id()( +MEDIA_PLAYER_ACTION_SCHEMA = maybe_simple_id( {cv.Required(CONF_ID): cv.use_id(MediaPlayer)} ) @automation.register_action("media_player.play", PlayAction, MEDIA_PLAYER_ACTION_SCHEMA) +@automation.register_action( + "media_player.toggle", ToggleAction, MEDIA_PLAYER_ACTION_SCHEMA +) @automation.register_action( "media_player.pause", PauseAction, MEDIA_PLAYER_ACTION_SCHEMA ) diff --git a/esphome/components/media_player/automation.h b/esphome/components/media_player/automation.h new file mode 100644 index 0000000000..e2b40d6a00 --- /dev/null +++ b/esphome/components/media_player/automation.h @@ -0,0 +1,35 @@ +#pragma once + +#include "esphome/core/automation.h" +#include "media_player.h" + +namespace esphome { + +namespace media_player { + +template class PlayAction : public Action, public Parented { + void play(Ts... x) override { + this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PLAY).perform(); + } +}; + +template class ToggleAction : public Action, public Parented { + void play(Ts... x) override { + this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_TOGGLE).perform(); + } +}; + +template class PauseAction : public Action, public Parented { + void play(Ts... x) override { + this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PAUSE).perform(); + } +}; + +template class StopAction : public Action, public Parented { + void play(Ts... x) override { + this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_STOP).perform(); + } +}; + +} // namespace media_player +} // namespace esphome diff --git a/esphome/components/media_player/media_player.cpp b/esphome/components/media_player/media_player.cpp index b05423c2bd..81cb6ca751 100644 --- a/esphome/components/media_player/media_player.cpp +++ b/esphome/components/media_player/media_player.cpp @@ -33,6 +33,8 @@ const char *media_player_command_to_string(MediaPlayerCommand command) { return "MUTE"; case MEDIA_PLAYER_COMMAND_UNMUTE: return "UNMUTE"; + case MEDIA_PLAYER_COMMAND_TOGGLE: + return "TOGGLE"; default: return "UNKNOWN"; } @@ -88,6 +90,8 @@ MediaPlayerCall &MediaPlayerCall::set_command(const std::string &command) { this->set_command(MEDIA_PLAYER_COMMAND_MUTE); } else if (str_equals_case_insensitive(command, "UNMUTE")) { this->set_command(MEDIA_PLAYER_COMMAND_UNMUTE); + } else if (str_equals_case_insensitive(command, "TOGGLE")) { + this->set_command(MEDIA_PLAYER_COMMAND_TOGGLE); } else { ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command.c_str()); } diff --git a/esphome/components/media_player/media_player.h b/esphome/components/media_player/media_player.h index 12e0b5c17d..6a2643b713 100644 --- a/esphome/components/media_player/media_player.h +++ b/esphome/components/media_player/media_player.h @@ -19,7 +19,8 @@ enum MediaPlayerCommand : uint8_t { MEDIA_PLAYER_COMMAND_PAUSE = 1, MEDIA_PLAYER_COMMAND_STOP = 2, MEDIA_PLAYER_COMMAND_MUTE = 3, - MEDIA_PLAYER_COMMAND_UNMUTE = 4 + MEDIA_PLAYER_COMMAND_UNMUTE = 4, + MEDIA_PLAYER_COMMAND_TOGGLE = 5 }; const char *media_player_command_to_string(MediaPlayerCommand command);