From 34169491ac86e84bbf05fa3fff8cac8fa18a3fd2 Mon Sep 17 00:00:00 2001 From: mrtoy-me <118446898+mrtoy-me@users.noreply.github.com> Date: Thu, 29 May 2025 05:37:25 +1000 Subject: [PATCH] [speaker mediaplayer] Yaml config initial volume (on first boot) (#8898) --- esphome/components/speaker/media_player/__init__.py | 3 +++ .../speaker/media_player/speaker_media_player.cpp | 4 +--- .../components/speaker/media_player/speaker_media_player.h | 6 ++++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/esphome/components/speaker/media_player/__init__.py b/esphome/components/speaker/media_player/__init__.py index cedafe214d..dc2dae2ef1 100644 --- a/esphome/components/speaker/media_player/__init__.py +++ b/esphome/components/speaker/media_player/__init__.py @@ -48,6 +48,7 @@ CONF_ON_UNMUTE = "on_unmute" CONF_ON_VOLUME = "on_volume" CONF_STREAM = "stream" CONF_VOLUME_INCREMENT = "volume_increment" +CONF_VOLUME_INITIAL = "volume_initial" CONF_VOLUME_MIN = "volume_min" CONF_VOLUME_MAX = "volume_max" @@ -282,6 +283,7 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_FILES): cv.ensure_list(MEDIA_FILE_TYPE_SCHEMA), cv.Optional(CONF_TASK_STACK_IN_PSRAM, default=False): cv.boolean, cv.Optional(CONF_VOLUME_INCREMENT, default=0.05): cv.percentage, + cv.Optional(CONF_VOLUME_INITIAL, default=0.5): cv.percentage, cv.Optional(CONF_VOLUME_MAX, default=1.0): cv.percentage, cv.Optional(CONF_VOLUME_MIN, default=0.0): cv.percentage, cv.Optional(CONF_ON_MUTE): automation.validate_automation(single=True), @@ -356,6 +358,7 @@ async def to_code(config): ) cg.add(var.set_volume_increment(config[CONF_VOLUME_INCREMENT])) + cg.add(var.set_volume_initial(config[CONF_VOLUME_INITIAL])) cg.add(var.set_volume_max(config[CONF_VOLUME_MAX])) cg.add(var.set_volume_min(config[CONF_VOLUME_MIN])) diff --git a/esphome/components/speaker/media_player/speaker_media_player.cpp b/esphome/components/speaker/media_player/speaker_media_player.cpp index fed0207c93..2c30f17c78 100644 --- a/esphome/components/speaker/media_player/speaker_media_player.cpp +++ b/esphome/components/speaker/media_player/speaker_media_player.cpp @@ -48,8 +48,6 @@ static const uint32_t MEDIA_CONTROLS_QUEUE_LENGTH = 20; static const UBaseType_t MEDIA_PIPELINE_TASK_PRIORITY = 1; static const UBaseType_t ANNOUNCEMENT_PIPELINE_TASK_PRIORITY = 1; -static const float FIRST_BOOT_DEFAULT_VOLUME = 0.5f; - static const char *const TAG = "speaker_media_player"; void SpeakerMediaPlayer::setup() { @@ -64,7 +62,7 @@ void SpeakerMediaPlayer::setup() { this->set_volume_(volume_restore_state.volume); this->set_mute_state_(volume_restore_state.is_muted); } else { - this->set_volume_(FIRST_BOOT_DEFAULT_VOLUME); + this->set_volume_(this->volume_initial_); this->set_mute_state_(false); } diff --git a/esphome/components/speaker/media_player/speaker_media_player.h b/esphome/components/speaker/media_player/speaker_media_player.h index 67e9859a13..967772d1a5 100644 --- a/esphome/components/speaker/media_player/speaker_media_player.h +++ b/esphome/components/speaker/media_player/speaker_media_player.h @@ -55,6 +55,9 @@ class SpeakerMediaPlayer : public Component, public media_player::MediaPlayer { // Percentage to increase or decrease the volume for volume up or volume down commands void set_volume_increment(float volume_increment) { this->volume_increment_ = volume_increment; } + // Volume used initially on first boot when no volume had been previously saved + void set_volume_initial(float volume_initial) { this->volume_initial_ = volume_initial; } + void set_volume_max(float volume_max) { this->volume_max_ = volume_max; } void set_volume_min(float volume_min) { this->volume_min_ = volume_min; } @@ -128,6 +131,9 @@ class SpeakerMediaPlayer : public Component, public media_player::MediaPlayer { // The amount to change the volume on volume up/down commands float volume_increment_; + // The initial volume used by Setup when no previous volume was saved + float volume_initial_; + float volume_max_; float volume_min_;