150 lines
5.8 KiB
YAML
150 lines
5.8 KiB
YAML
####################################################################################################
|
|
##### 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_speaker: !include TX-Ultimate-Easy-ESPHome_standard_hw_speaker.yaml
|
|
|
|
esphome:
|
|
platformio_options:
|
|
build_flags:
|
|
- -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
|
|
name: Media Player
|
|
internal: false
|
|
platform: speaker
|
|
codec_support_enabled: true
|
|
buffer_size: 250000
|
|
task_stack_in_psram: false
|
|
announcement_pipeline:
|
|
speaker: announcement_spk_resampling_input
|
|
format: WAV
|
|
num_channels: 1
|
|
sample_rate: 48000
|
|
media_pipeline:
|
|
speaker: media_spk_resampling_input
|
|
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
|
|
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
|
|
- lambda: |-
|
|
// Check for requirements
|
|
#if !defined(TX_ULTIMATE_EASY_CORE)
|
|
#error "The package TX-Ultimate-Easy-ESPHome_core.yaml is required."
|
|
#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 // TX_ULTIMATE_EASY_STANDARD_HW_SPEAKER
|
|
|
|
// Identify itself
|
|
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
|
|
id: mixer_speaker_embedded
|
|
output_speaker: speaker_embedded
|
|
source_speakers:
|
|
- id: announcement_speaker_embedded
|
|
- id: media_speaker_embedded
|
|
- platform: resampler
|
|
id: announcement_spk_resampling_input
|
|
output_speaker: announcement_speaker_embedded
|
|
- platform: resampler
|
|
id: media_spk_resampling_input
|
|
output_speaker: media_speaker_embedded
|
|
|
|
switch:
|
|
- id: !extend sw_enable_audio
|
|
pin:
|
|
allow_other_uses: true
|
|
...
|