mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Implement media player volume actions (#3551)
This commit is contained in:
		| @@ -51,6 +51,22 @@ void I2SAudioMediaPlayer::control(const media_player::MediaPlayerCall &call) { | |||||||
|           this->state = media_player::MEDIA_PLAYER_STATE_PAUSED; |           this->state = media_player::MEDIA_PLAYER_STATE_PAUSED; | ||||||
|         } |         } | ||||||
|         break; |         break; | ||||||
|  |       case media_player::MEDIA_PLAYER_COMMAND_VOLUME_UP: { | ||||||
|  |         float new_volume = this->volume + 0.1f; | ||||||
|  |         if (new_volume > 1.0f) | ||||||
|  |           new_volume = 1.0f; | ||||||
|  |         this->set_volume_(new_volume); | ||||||
|  |         this->unmute_(); | ||||||
|  |         break; | ||||||
|  |       } | ||||||
|  |       case media_player::MEDIA_PLAYER_COMMAND_VOLUME_DOWN: { | ||||||
|  |         float new_volume = this->volume - 0.1f; | ||||||
|  |         if (new_volume < 0.0f) | ||||||
|  |           new_volume = 0.0f; | ||||||
|  |         this->set_volume_(new_volume); | ||||||
|  |         this->unmute_(); | ||||||
|  |         break; | ||||||
|  |       } | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|   this->publish_state(); |   this->publish_state(); | ||||||
|   | |||||||
| @@ -29,6 +29,17 @@ PauseAction = media_player_ns.class_( | |||||||
| StopAction = media_player_ns.class_( | StopAction = media_player_ns.class_( | ||||||
|     "StopAction", automation.Action, cg.Parented.template(MediaPlayer) |     "StopAction", automation.Action, cg.Parented.template(MediaPlayer) | ||||||
| ) | ) | ||||||
|  | VolumeUpAction = media_player_ns.class_( | ||||||
|  |     "VolumeUpAction", automation.Action, cg.Parented.template(MediaPlayer) | ||||||
|  | ) | ||||||
|  | VolumeDownAction = media_player_ns.class_( | ||||||
|  |     "VolumeDownAction", automation.Action, cg.Parented.template(MediaPlayer) | ||||||
|  | ) | ||||||
|  | VolumeSetAction = media_player_ns.class_( | ||||||
|  |     "VolumeSetAction", automation.Action, cg.Parented.template(MediaPlayer) | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | CONF_VOLUME = "volume" | ||||||
|  |  | ||||||
|  |  | ||||||
| async def setup_media_player_core_(var, config): | async def setup_media_player_core_(var, config): | ||||||
| @@ -45,9 +56,7 @@ async def register_media_player(var, config): | |||||||
| MEDIA_PLAYER_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.Schema({})) | 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.GenerateID(): cv.use_id(MediaPlayer)}) | ||||||
|     {cv.Required(CONF_ID): cv.use_id(MediaPlayer)} |  | ||||||
| ) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| @automation.register_action("media_player.play", PlayAction, MEDIA_PLAYER_ACTION_SCHEMA) | @automation.register_action("media_player.play", PlayAction, MEDIA_PLAYER_ACTION_SCHEMA) | ||||||
| @@ -58,12 +67,37 @@ MEDIA_PLAYER_ACTION_SCHEMA = maybe_simple_id( | |||||||
|     "media_player.pause", PauseAction, MEDIA_PLAYER_ACTION_SCHEMA |     "media_player.pause", PauseAction, MEDIA_PLAYER_ACTION_SCHEMA | ||||||
| ) | ) | ||||||
| @automation.register_action("media_player.stop", StopAction, MEDIA_PLAYER_ACTION_SCHEMA) | @automation.register_action("media_player.stop", StopAction, MEDIA_PLAYER_ACTION_SCHEMA) | ||||||
|  | @automation.register_action( | ||||||
|  |     "media_player.volume_up", VolumeUpAction, MEDIA_PLAYER_ACTION_SCHEMA | ||||||
|  | ) | ||||||
|  | @automation.register_action( | ||||||
|  |     "media_player.volume_down", VolumeDownAction, MEDIA_PLAYER_ACTION_SCHEMA | ||||||
|  | ) | ||||||
| async def media_player_action(config, action_id, template_arg, args): | async def media_player_action(config, action_id, template_arg, args): | ||||||
|     var = cg.new_Pvariable(action_id, template_arg) |     var = cg.new_Pvariable(action_id, template_arg) | ||||||
|     await cg.register_parented(var, config[CONF_ID]) |     await cg.register_parented(var, config[CONF_ID]) | ||||||
|     return var |     return var | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @automation.register_action( | ||||||
|  |     "media_player.volume_set", | ||||||
|  |     VolumeSetAction, | ||||||
|  |     cv.maybe_simple_value( | ||||||
|  |         { | ||||||
|  |             cv.GenerateID(): cv.use_id(MediaPlayer), | ||||||
|  |             cv.Required(CONF_VOLUME): cv.templatable(cv.percentage), | ||||||
|  |         }, | ||||||
|  |         key=CONF_VOLUME, | ||||||
|  |     ), | ||||||
|  | ) | ||||||
|  | async def media_player_volume_set_action(config, action_id, template_arg, args): | ||||||
|  |     var = cg.new_Pvariable(action_id, template_arg) | ||||||
|  |     await cg.register_parented(var, config[CONF_ID]) | ||||||
|  |     volume = await cg.templatable(config[CONF_VOLUME], args, float) | ||||||
|  |     cg.add(var.set_volume(volume)) | ||||||
|  |     return var | ||||||
|  |  | ||||||
|  |  | ||||||
| @coroutine_with_priority(100.0) | @coroutine_with_priority(100.0) | ||||||
| async def to_code(config): | async def to_code(config): | ||||||
|     cg.add_global(media_player_ns.using) |     cg.add_global(media_player_ns.using) | ||||||
|   | |||||||
| @@ -7,28 +7,23 @@ namespace esphome { | |||||||
|  |  | ||||||
| namespace media_player { | namespace media_player { | ||||||
|  |  | ||||||
| template<typename... Ts> class PlayAction : public Action<Ts...>, public Parented<MediaPlayer> { | #define MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(ACTION_CLASS, ACTION_COMMAND) \ | ||||||
|   void play(Ts... x) override { |   template<typename... Ts> class ACTION_CLASS : public Action<Ts...>, public Parented<MediaPlayer> { \ | ||||||
|     this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PLAY).perform(); |     void play(Ts... x) override { \ | ||||||
|   } |       this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_##ACTION_COMMAND).perform(); \ | ||||||
| }; |     } \ | ||||||
|  |   }; | ||||||
|  |  | ||||||
| template<typename... Ts> class ToggleAction : public Action<Ts...>, public Parented<MediaPlayer> { | MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(PlayAction, PLAY) | ||||||
|   void play(Ts... x) override { | MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(PauseAction, PAUSE) | ||||||
|     this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_TOGGLE).perform(); | MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(StopAction, STOP) | ||||||
|   } | MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(ToggleAction, TOGGLE) | ||||||
| }; | MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(VolumeUpAction, VOLUME_UP) | ||||||
|  | MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(VolumeDownAction, VOLUME_DOWN) | ||||||
|  |  | ||||||
| template<typename... Ts> class PauseAction : public Action<Ts...>, public Parented<MediaPlayer> { | template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<MediaPlayer> { | ||||||
|   void play(Ts... x) override { |   TEMPLATABLE_VALUE(float, volume) | ||||||
|     this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PAUSE).perform(); |   void play(Ts... x) override { this->parent_->make_call().set_volume(this->volume_.value(x...)).perform(); } | ||||||
|   } |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<MediaPlayer> { |  | ||||||
|   void play(Ts... x) override { |  | ||||||
|     this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_STOP).perform(); |  | ||||||
|   } |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
| }  // namespace media_player | }  // namespace media_player | ||||||
|   | |||||||
| @@ -20,7 +20,9 @@ enum MediaPlayerCommand : uint8_t { | |||||||
|   MEDIA_PLAYER_COMMAND_STOP = 2, |   MEDIA_PLAYER_COMMAND_STOP = 2, | ||||||
|   MEDIA_PLAYER_COMMAND_MUTE = 3, |   MEDIA_PLAYER_COMMAND_MUTE = 3, | ||||||
|   MEDIA_PLAYER_COMMAND_UNMUTE = 4, |   MEDIA_PLAYER_COMMAND_UNMUTE = 4, | ||||||
|   MEDIA_PLAYER_COMMAND_TOGGLE = 5 |   MEDIA_PLAYER_COMMAND_TOGGLE = 5, | ||||||
|  |   MEDIA_PLAYER_COMMAND_VOLUME_UP = 6, | ||||||
|  |   MEDIA_PLAYER_COMMAND_VOLUME_DOWN = 7, | ||||||
| }; | }; | ||||||
| const char *media_player_command_to_string(MediaPlayerCommand command); | const char *media_player_command_to_string(MediaPlayerCommand command); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -604,6 +604,14 @@ touchscreen: | |||||||
|       - logger.log: |       - logger.log: | ||||||
|           format: Touch at (%d, %d) |           format: Touch at (%d, %d) | ||||||
|           args: ["touch.x", "touch.y"] |           args: ["touch.x", "touch.y"] | ||||||
|  |       - media_player.play: | ||||||
|  |       - media_player.pause: | ||||||
|  |       - media_player.stop: | ||||||
|  |       - media_player.toggle: | ||||||
|  |       - media_player.volume_up: | ||||||
|  |       - media_player.volume_down: | ||||||
|  |       - media_player.volume_set: 50% | ||||||
|  |  | ||||||
|  |  | ||||||
| media_player: | media_player: | ||||||
|   - platform: i2s_audio |   - platform: i2s_audio | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user