Remember Media Player volume

This commit is contained in:
Edward Firmo
2025-07-31 10:26:37 +02:00
parent e53334ac18
commit 796eb7620c
5 changed files with 96 additions and 113 deletions

View File

@@ -17,7 +17,13 @@ packages:
esphome:
platformio_options:
build_flags:
- -D TX_ULTIMATE_EASY_ADDON_MEDIA_PLAYER
- -D TX_ULTIMATE_EASY_STANDARD_MEDIA_PLAYER
globals:
- id: last_media_player_volume
type: uint8_t
restore_value: false
initial_value: '100'
media_player:
- id: mp_media_player
@@ -37,8 +43,66 @@ media_player:
format: FLAC
num_channels: 1
sample_rate: 48000
mute_pin:
number: GPIO26
inverted: true
allow_other_uses: true
on_state:
then:
- lambda: |-
// Check if volume has changed and update number entity
uint8_t current_volume_percentage = static_cast<uint8_t>(mp_media_player->volume * 100.0f);
if (abs(static_cast<int>(current_volume_percentage) - static_cast<int>(id(last_media_player_volume))) > 1) { // 1% threshold
id(last_media_player_volume) = current_volume_percentage;
ESP_LOGD("media_player_volume", "Media player volume changed to %" PRIu8 "%%, updating number entity", current_volume_percentage);
nr_media_player_volume->publish_state(static_cast<float>(current_volume_percentage));
}
number:
- id: nr_media_player_volume
name: Volume - Media Player
icon: mdi:volume-high
unit_of_measurement: "%"
internal: false
entity_category: config
platform: template
min_value: 0
max_value: 100
step: 1
initial_value: 100
optimistic: false
restore_value: true
lambda: |-
// Return current media player volume as percentage
return mp_media_player->volume * 100.0f;
set_action:
then:
- media_player.volume_set:
id: mp_media_player
volume: !lambda return x / 100.0f;
- lambda: |-
id(last_media_player_volume) = static_cast<uint8_t>(x);
ESP_LOGD("media_player_volume", "Setting media player volume to %.1f%% (%.3f)", x, x / 100.0f);
on_value:
then:
- lambda: |-
// Update our tracking variable when number entity changes
id(last_media_player_volume) = static_cast<uint8_t>(x);
ESP_LOGVV("media_player_volume", "Number entity changed to %.1f%%", x);
script:
- id: !extend boot_sequence
then:
- script.execute: sync_initial_volume
- id: !extend dump_config
then:
- lambda: |-
// Volume sync configuration
ESP_LOGCONFIG("standard_media_player", "Media Player Volume Sync:");
ESP_LOGCONFIG("standard_media_player", " Current volume: %.1f%%", mp_media_player->volume * 100.0f);
ESP_LOGCONFIG("standard_media_player", " Number entity: %.1f%%", nr_media_player_volume->state);
- id: !extend dump_config_list_packages
then:
- script.wait: dump_config
@@ -46,13 +110,24 @@ script:
// Check for requirements
#if !defined(TX_ULTIMATE_EASY_CORE)
#error "The package TX-Ultimate-Easy-ESPHome_core.yaml is required."
#endif
#endif // TX_ULTIMATE_EASY_CORE
#if !defined(TX_ULTIMATE_EASY_STANDARD_HW_SPEAKER)
#error "The package TX-Ultimate-Easy-ESPHome_standard_hw_speaker.yaml is required."
#endif
#endif // TX_ULTIMATE_EASY_STANDARD_HW_SPEAKER
// Identify itself
ESP_LOGCONFIG(ESPHOME_PROJECT_NAME, " - Add-on - Media Player");
ESP_LOGCONFIG(ESPHOME_PROJECT_NAME, " - Standard - Media Player");
- id: sync_initial_volume
mode: restart
then:
- delay: 2s # Wait for media player to initialize
- media_player.volume_set: # Sync initial volume from persisted number entity to media player
id: mp_media_player
volume: !lambda return nr_media_player_volume->state / 100.0f;
- lambda: |-
id(last_media_player_volume) = static_cast<uint8_t>(nr_media_player_volume->state);
ESP_LOGI("media_player_volume", "Initial volume sync: %.1f%% (restored from persistence)", nr_media_player_volume->state);
speaker:
- platform: mixer
@@ -67,4 +142,9 @@ speaker:
- platform: resampler
id: media_spk_resampling_input
output_speaker: media_speaker_embedded
switch:
- id: !extend sw_enable_audio
pin:
allow_other_uses: true
...