Remember Media Player volume
This commit is contained in:
@@ -1,68 +0,0 @@
|
|||||||
####################################################################################################
|
|
||||||
##### TX Ultimate Easy for ESPHome #####
|
|
||||||
##### Repository: https://github.com/edwardtfn/TX-Ultimate-Easy #####
|
|
||||||
####################################################################################################
|
|
||||||
##### Purpose: ESPHome - Add-on - Media player #####
|
|
||||||
####################################################################################################
|
|
||||||
##### Author: edwardtfn - https://github.com/edwardtfn - https://buymeacoffee.com/edwardfirmo #####
|
|
||||||
####################################################################################################
|
|
||||||
##### NOTE: #####
|
|
||||||
##### - Make changes ONLY if absolutely necessary and you have the required knowledge. #####
|
|
||||||
##### - For normal system use, modifications to this file are NOT required. #####
|
|
||||||
####################################################################################################
|
|
||||||
---
|
|
||||||
packages:
|
|
||||||
standard_hw_audio: !include TX-Ultimate-Easy-ESPHome_standard_hw_audio.yaml
|
|
||||||
|
|
||||||
esp32:
|
|
||||||
framework:
|
|
||||||
type: arduino
|
|
||||||
|
|
||||||
esphome:
|
|
||||||
platformio_options:
|
|
||||||
build_flags:
|
|
||||||
- -D TX_ULTIMATE_EASY_ADDON_MEDIA_PLAYER
|
|
||||||
|
|
||||||
media_player:
|
|
||||||
- id: mp_media_player
|
|
||||||
name: Media Player
|
|
||||||
internal: false
|
|
||||||
platform: i2s_audio
|
|
||||||
i2s_dout_pin: GPIO15
|
|
||||||
i2s_audio_id: if_i2s_audio
|
|
||||||
i2s_comm_fmt: msb
|
|
||||||
dac_type: external
|
|
||||||
mode: mono
|
|
||||||
mute_pin:
|
|
||||||
number: GPIO26
|
|
||||||
inverted: true
|
|
||||||
allow_other_uses: true
|
|
||||||
|
|
||||||
script:
|
|
||||||
- id: !extend dump_config_list_packages
|
|
||||||
then:
|
|
||||||
- script.wait: dump_config
|
|
||||||
- lambda: |-
|
|
||||||
// Check for requirements
|
|
||||||
#if !defined(TX_ULTIMATE_EASY_CORE)
|
|
||||||
#error "The package TX-Ultimate-Easy-ESPHome_core.yaml is required."
|
|
||||||
#endif
|
|
||||||
#if !defined(TX_ULTIMATE_EASY_STANDARD_HW_AUDIO)
|
|
||||||
#error "The package TX-Ultimate-Easy-ESPHome_standard_hw_audio.yaml is required."
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Identify itself
|
|
||||||
ESP_LOGCONFIG(ESPHOME_PROJECT_NAME, " - Add-on - Media Player");
|
|
||||||
|
|
||||||
switch:
|
|
||||||
- id: sw_speaker_amplifier
|
|
||||||
name: Speaker - Amplifier
|
|
||||||
platform: gpio
|
|
||||||
pin:
|
|
||||||
number: GPIO26
|
|
||||||
inverted: true
|
|
||||||
allow_other_uses: true
|
|
||||||
restore_mode: RESTORE_DEFAULT_ON
|
|
||||||
internal: true
|
|
||||||
entity_category: config
|
|
||||||
...
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
####################################################################################################
|
|
||||||
##### TX Ultimate Easy for ESPHome #####
|
|
||||||
##### Repository: https://github.com/edwardtfn/TX-Ultimate-Easy #####
|
|
||||||
####################################################################################################
|
|
||||||
##### Purpose: ESPHome Core - Hardware - Media player #####
|
|
||||||
####################################################################################################
|
|
||||||
##### Author: edwardtfn - https://github.com/edwardtfn - https://buymeacoffee.com/edwardfirmo #####
|
|
||||||
####################################################################################################
|
|
||||||
##### NOTE: #####
|
|
||||||
##### - Make changes ONLY if absolutely necessary and you have the required knowledge. #####
|
|
||||||
##### - For normal system use, modifications to this file are NOT required. #####
|
|
||||||
####################################################################################################
|
|
||||||
---
|
|
||||||
i2s_audio:
|
|
||||||
id: if_i2s_audio
|
|
||||||
i2s_bclk_pin: GPIO2
|
|
||||||
i2s_lrclk_pin: GPIO4
|
|
||||||
|
|
||||||
media_player:
|
|
||||||
- id: mp_media_player
|
|
||||||
name: Media Player
|
|
||||||
internal: false
|
|
||||||
platform: i2s_audio
|
|
||||||
i2s_dout_pin: GPIO15
|
|
||||||
i2s_audio_id: if_i2s_audio
|
|
||||||
i2s_comm_fmt: msb # Experimental - It was 'lsb' until v2024.12.12
|
|
||||||
dac_type: external
|
|
||||||
mode: mono
|
|
||||||
...
|
|
||||||
@@ -33,4 +33,15 @@ script:
|
|||||||
|
|
||||||
// Identify itself
|
// Identify itself
|
||||||
ESP_LOGCONFIG(ESPHOME_PROJECT_NAME, " - Standard - Hardware - Audio");
|
ESP_LOGCONFIG(ESPHOME_PROJECT_NAME, " - Standard - Hardware - Audio");
|
||||||
|
|
||||||
|
switch:
|
||||||
|
- id: sw_enable_audio
|
||||||
|
name: Enable Audio
|
||||||
|
platform: gpio
|
||||||
|
pin:
|
||||||
|
number: GPIO26
|
||||||
|
inverted: true
|
||||||
|
restore_mode: RESTORE_DEFAULT_ON
|
||||||
|
internal: true
|
||||||
|
entity_category: config
|
||||||
...
|
...
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ esphome:
|
|||||||
|
|
||||||
number:
|
number:
|
||||||
- id: speaker_volume
|
- id: speaker_volume
|
||||||
name: Sound - Volume
|
name: Volume - Speaker
|
||||||
platform: template
|
platform: template
|
||||||
max_value: 100
|
max_value: 100
|
||||||
min_value: 0
|
min_value: 0
|
||||||
@@ -183,15 +183,4 @@ speaker:
|
|||||||
bits_per_sample: 16bit
|
bits_per_sample: 16bit
|
||||||
sample_rate: 48000
|
sample_rate: 48000
|
||||||
buffer_duration: 500ms
|
buffer_duration: 500ms
|
||||||
|
|
||||||
switch:
|
|
||||||
- id: sw_speaker_amplifier
|
|
||||||
name: Speaker - Amplifier
|
|
||||||
platform: gpio
|
|
||||||
pin:
|
|
||||||
number: GPIO26
|
|
||||||
inverted: false
|
|
||||||
restore_mode: ALWAYS_ON
|
|
||||||
internal: true
|
|
||||||
entity_category: config
|
|
||||||
...
|
...
|
||||||
|
|||||||
@@ -17,7 +17,13 @@ packages:
|
|||||||
esphome:
|
esphome:
|
||||||
platformio_options:
|
platformio_options:
|
||||||
build_flags:
|
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:
|
media_player:
|
||||||
- id: mp_media_player
|
- id: mp_media_player
|
||||||
@@ -37,8 +43,66 @@ 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:
|
||||||
|
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:
|
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
|
- id: !extend dump_config_list_packages
|
||||||
then:
|
then:
|
||||||
- script.wait: dump_config
|
- script.wait: dump_config
|
||||||
@@ -46,13 +110,24 @@ script:
|
|||||||
// Check for requirements
|
// Check for requirements
|
||||||
#if !defined(TX_ULTIMATE_EASY_CORE)
|
#if !defined(TX_ULTIMATE_EASY_CORE)
|
||||||
#error "The package TX-Ultimate-Easy-ESPHome_core.yaml is required."
|
#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)
|
#if !defined(TX_ULTIMATE_EASY_STANDARD_HW_SPEAKER)
|
||||||
#error "The package TX-Ultimate-Easy-ESPHome_standard_hw_speaker.yaml is required."
|
#error "The package TX-Ultimate-Easy-ESPHome_standard_hw_speaker.yaml is required."
|
||||||
#endif
|
#endif // TX_ULTIMATE_EASY_STANDARD_HW_SPEAKER
|
||||||
|
|
||||||
// Identify itself
|
// 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:
|
speaker:
|
||||||
- platform: mixer
|
- platform: mixer
|
||||||
@@ -67,4 +142,9 @@ 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
|
||||||
...
|
...
|
||||||
|
|||||||
Reference in New Issue
Block a user