#################################################################################################### ##### 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. ##### #################################################################################################### --- substitutions: TAG_STD_MEDIA_PLAYER: "std.media-player" 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' - id: persistent_media_player_volume type: uint8_t restore_value: true 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 on_state: then: - lambda: |- // Check if volume has changed and update number entity id(persistent_media_player_volume) = static_cast(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("${TAG_STD_MEDIA_PLAYER}", "Media player volume changed to %" PRIu8 "%%, updating number entity", id(persistent_media_player_volume)); } script: - id: !extend boot_sequence then: - script.execute: sync_initial_volume - id: !extend dump_config then: - lambda: |- // Volume sync configuration ESP_LOGCONFIG("${TAG_STD_MEDIA_PLAYER}", "Media Player Volume Sync:"); ESP_LOGCONFIG("${TAG_STD_MEDIA_PLAYER}", " Persistent volume: %" PRIu8 "%", id(persistent_media_player_volume)); ESP_LOGCONFIG("${TAG_STD_MEDIA_PLAYER}", " Current volume: %.1f%%", mp_media_player->volume * 100.0f); - 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 id(persistent_media_player_volume) / 100.0f; - lambda: |- id(last_media_player_volume) = id(persistent_media_player_volume); ESP_LOGI("${TAG_STD_MEDIA_PLAYER}", "Initial volume sync: %.1f%% (restored from persistence)", id(persistent_media_player_volume)); 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 ...