From 2f92e874b3171eb9913cc699439bf5fe43eee3d4 Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Thu, 19 Dec 2024 20:45:31 +0100 Subject: [PATCH] Clean-up relay mode select --- ...Ultimate-Easy-ESPHome_core_hw_buttons.yaml | 1 - ...-Ultimate-Easy-ESPHome_core_hw_relays.yaml | 96 ++++++++----------- 2 files changed, 39 insertions(+), 58 deletions(-) diff --git a/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml b/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml index ffd32d8..2f5b1ef 100644 --- a/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml +++ b/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml @@ -244,5 +244,4 @@ select: entity_category: config disabled_by_default: false icon: mdi:dip-switch - ... diff --git a/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml b/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml index eeaf855..4f67524 100644 --- a/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml +++ b/ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml @@ -82,40 +82,52 @@ script: mode: restart then: - lambda: |- - return; - show_relay_status->stop(); auto model_format = sl_tx_model_format->active_index(); - std::vector relay_modes; + if (!model_format.has_value()) return; // Early return if no model format - if (model_format.has_value() and model_format.value() == 0) { - relay_modes = {sl_relay_1_light_mode_eu, sl_relay_2_light_mode_eu, sl_relay_3_light_mode_eu, sl_relay_4_light_mode_eu}; + std::vector relay_modes; + const bool is_eu_model = model_format.value() == 0; + + // Select appropriate light mode selectors based on model + if (is_eu_model) { + relay_modes = {sl_relay_1_light_mode_eu, sl_relay_2_light_mode_eu, + sl_relay_3_light_mode_eu, sl_relay_4_light_mode_eu}; } else { - relay_modes = {sl_relay_1_light_mode_us, sl_relay_2_light_mode_us, sl_relay_3_light_mode_us, sl_relay_4_light_mode_us}; + relay_modes = {sl_relay_1_light_mode_us, sl_relay_2_light_mode_us, + sl_relay_3_light_mode_us, sl_relay_4_light_mode_us}; } - std::vector switches = {sw_relay_1, sw_relay_2, sw_relay_3, sw_relay_4}; - for (int i = 0; i < relay_modes.size(); ++i) { + // Get switch states + const std::vector switches = {sw_relay_1, sw_relay_2, sw_relay_3, sw_relay_4}; + + // Update lights based on relay states + for (size_t i = 0; i < relay_modes.size(); ++i) { auto light_index = relay_modes[i]->active_index(); - if (light_index.has_value()) { - uint8_t light_idx = light_index.value(); - bool switch_state = switches[i]->state; - if (light_idx == 1 || light_idx == 3) { - if (switch_state) - id(gb_lights_1)[i]->turn_on().perform(); - else - id(gb_lights_1)[i]->turn_off().perform(); - } - if (light_idx == 2 || light_idx == 3) { - if (switch_state) - id(gb_lights_2)[i]->turn_on().perform(); - else - id(gb_lights_2)[i]->turn_off().perform(); - } + if (!light_index.has_value()) continue; + + const uint8_t light_idx = light_index.value(); + const bool switch_state = switches[i]->state; + + // Update first light group (bottom/left) + if (light_idx == 1 || light_idx == 3) { + if (switch_state) + id(gb_lights_1)[i]->turn_on(); + else + id(gb_lights_1)[i]->turn_off(); + } + + // Update second light group (top/right) + if (light_idx == 2 || light_idx == 3) { + if (switch_state) + id(gb_lights_2)[i]->turn_on(); + else + id(gb_lights_2)[i]->turn_off(); } } select: - - id: sl_relay_1_mode + - &relay_mode_base + id: sl_relay_1_mode name: Relay 1 mode platform: template options: @@ -131,45 +143,15 @@ select: - id: sl_relay_2_mode name: Relay 2 mode - platform: template - options: - - "Relay" - - "Light" - - "Not in use" - optimistic: true - restore_value: true - internal: true - entity_category: config - disabled_by_default: false - icon: mdi:dip-switch + <<: *relay_mode_base - id: sl_relay_3_mode name: Relay 3 mode - platform: template - options: - - "Relay" - - "Light" - - "Not in use" - optimistic: true - restore_value: true - internal: true - entity_category: config - disabled_by_default: false - icon: mdi:dip-switch + <<: *relay_mode_base - id: sl_relay_4_mode name: Relay 4 mode - platform: template - options: - - "Relay" - - "Light" - - "Not in use" - optimistic: true - restore_value: true - internal: true - entity_category: config - disabled_by_default: false - icon: mdi:dip-switch + <<: *relay_mode_base switch: - &relay_switch_base