Use globals for persistence

This commit is contained in:
Edward Firmo
2025-07-31 10:58:22 +02:00
parent a67f3dfd4a
commit 9edd43079b
2 changed files with 13 additions and 50 deletions

View File

@@ -42,7 +42,7 @@ esphome:
number: number:
- id: speaker_volume - id: speaker_volume
name: Volume - Speaker name: Speaker Volume
platform: template platform: template
max_value: 100 max_value: 100
min_value: 0 min_value: 0

View File

@@ -24,6 +24,10 @@ globals:
type: uint8_t type: uint8_t
restore_value: false restore_value: false
initial_value: '100' initial_value: '100'
- id: persistent_media_player_volume
type: uint8_t
restore_value: true
initial_value: '100'
media_player: media_player:
- id: mp_media_player - id: mp_media_player
@@ -43,52 +47,16 @@ media_player:
format: FLAC format: FLAC
num_channels: 1 num_channels: 1
sample_rate: 48000 sample_rate: 48000
mute_pin:
number: GPIO26
inverted: true
allow_other_uses: true
on_state: on_state:
then: then:
- lambda: |- - lambda: |-
// Check if volume has changed and update number entity // Check if volume has changed and update number entity
uint8_t current_volume_percentage = static_cast<uint8_t>(mp_media_player->volume * 100.0f); id(persistent_media_player_volume) = 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 if (id(persistent_media_player_volume) != id(last_media_player_volume)) {
id(last_media_player_volume) = current_volume_percentage; id(last_media_player_volume) = id(persistent_media_player_volume);
ESP_LOGD("media_player_volume", "Media player volume changed to %" PRIu8 "%%, updating number entity", current_volume_percentage); ESP_LOGD("media_player_volume", "Media player volume changed to %" PRIu8 "%%, updating number entity", id(persistent_media_player_volume));
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
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: script:
- id: !extend boot_sequence - id: !extend boot_sequence
then: then:
@@ -99,8 +67,8 @@ script:
- lambda: |- - lambda: |-
// Volume sync configuration // Volume sync configuration
ESP_LOGCONFIG("standard_media_player", "Media Player Volume Sync:"); ESP_LOGCONFIG("standard_media_player", "Media Player Volume Sync:");
ESP_LOGCONFIG("standard_media_player", " Persistent volume: %" PRIu8 "%", id(persistent_media_player_volume));
ESP_LOGCONFIG("standard_media_player", " Current volume: %.1f%%", mp_media_player->volume * 100.0f); 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 - id: !extend dump_config_list_packages
then: then:
@@ -123,10 +91,10 @@ script:
- delay: 2s # Wait for media player to initialize - delay: 2s # Wait for media player to initialize
- media_player.volume_set: # Sync initial volume from persisted number entity to media player - media_player.volume_set: # Sync initial volume from persisted number entity to media player
id: mp_media_player id: mp_media_player
volume: !lambda return nr_media_player_volume->state / 100.0f; volume: !lambda return id(persistent_media_player_volume) / 100.0f;
- lambda: |- - lambda: |-
id(last_media_player_volume) = static_cast<uint8_t>(nr_media_player_volume->state); id(last_media_player_volume) = id(persistent_media_player_volume);
ESP_LOGI("media_player_volume", "Initial volume sync: %.1f%% (restored from persistence)", nr_media_player_volume->state); ESP_LOGI("media_player_volume", "Initial volume sync: %.1f%% (restored from persistence)", id(persistent_media_player_volume));
speaker: speaker:
- platform: mixer - platform: mixer
@@ -141,9 +109,4 @@ speaker:
- platform: resampler - platform: resampler
id: media_spk_resampling_input id: media_spk_resampling_input
output_speaker: media_speaker_embedded output_speaker: media_speaker_embedded
switch:
- id: !extend sw_enable_audio
pin:
allow_other_uses: true
... ...