Use globals for persistence
This commit is contained in:
@@ -42,7 +42,7 @@ esphome:
|
||||
|
||||
number:
|
||||
- id: speaker_volume
|
||||
name: Volume - Speaker
|
||||
name: Speaker Volume
|
||||
platform: template
|
||||
max_value: 100
|
||||
min_value: 0
|
||||
|
||||
@@ -24,6 +24,10 @@ globals:
|
||||
type: uint8_t
|
||||
restore_value: false
|
||||
initial_value: '100'
|
||||
- id: persistent_media_player_volume
|
||||
type: uint8_t
|
||||
restore_value: true
|
||||
initial_value: '100'
|
||||
|
||||
media_player:
|
||||
- id: mp_media_player
|
||||
@@ -43,52 +47,16 @@ 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));
|
||||
id(persistent_media_player_volume) = static_cast<uint8_t>(mp_media_player->volume * 100.0f);
|
||||
if (id(persistent_media_player_volume) != id(last_media_player_volume)) {
|
||||
id(last_media_player_volume) = id(persistent_media_player_volume);
|
||||
ESP_LOGD("media_player_volume", "Media player volume changed to %" PRIu8 "%%, updating number entity", id(persistent_media_player_volume));
|
||||
}
|
||||
|
||||
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:
|
||||
- id: !extend boot_sequence
|
||||
then:
|
||||
@@ -99,8 +67,8 @@ script:
|
||||
- lambda: |-
|
||||
// Volume sync configuration
|
||||
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", " Number entity: %.1f%%", nr_media_player_volume->state);
|
||||
|
||||
- id: !extend dump_config_list_packages
|
||||
then:
|
||||
@@ -123,10 +91,10 @@ script:
|
||||
- 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;
|
||||
volume: !lambda return id(persistent_media_player_volume) / 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);
|
||||
id(last_media_player_volume) = id(persistent_media_player_volume);
|
||||
ESP_LOGI("media_player_volume", "Initial volume sync: %.1f%% (restored from persistence)", id(persistent_media_player_volume));
|
||||
|
||||
speaker:
|
||||
- platform: mixer
|
||||
@@ -141,9 +109,4 @@ speaker:
|
||||
- platform: resampler
|
||||
id: media_spk_resampling_input
|
||||
output_speaker: media_speaker_embedded
|
||||
|
||||
switch:
|
||||
- id: !extend sw_enable_audio
|
||||
pin:
|
||||
allow_other_uses: true
|
||||
...
|
||||
|
||||
Reference in New Issue
Block a user