[speaker mediaplayer] Yaml config initial volume (on first boot) (#8898)

This commit is contained in:
mrtoy-me 2025-05-29 05:37:25 +10:00 committed by GitHub
parent 8eac859bab
commit 34169491ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 3 deletions

View File

@ -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]))

View File

@ -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);
}

View File

@ -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_;