New implementation of show_relay_status

Solves #28
I believe this also solves #21
This commit is contained in:
Edward Firmo
2024-12-20 18:07:17 +01:00
parent 0cc17b715f
commit b1b1a40d5e
3 changed files with 149 additions and 64 deletions

View File

@@ -144,60 +144,70 @@ script:
- script.wait: boot_initialize_relays
- wait_until:
condition:
- lambda: return sl_tx_model_format->active_index().has_value();
- lambda: return id(boot_initialization_relays);
- lambda: |-
auto model_format = sl_tx_model_format->active_index();
if (!model_format.has_value()) {
ESP_LOGE("core_hw_relays", "Model (Format) not defined");
const bool is_model_us = sl_tx_model_format->active_index().has_value() and
sl_tx_model_format->active_index().value() == 1;
const uint8_t num_gangs = (sl_tx_model_gang->active_index().has_value()) ?
(sl_tx_model_gang->active_index().value() + 1) : 0;
if (num_gangs < 1 || num_gangs > 4) {
ESP_LOGE("core_hw_relays", "Invalid number of gangs: %" PRIu8, num_gangs);
return;
}
std::vector<esphome::select::Select*> relay_modes;
const bool is_eu_model = model_format.value() == 0;
for (uint8_t i = 0; i < num_gangs; i++) {
bool relay_state = false;
std::string light_mode;
const std::string light_mode_group_1 = is_model_us
? "${LIGHT_MODE_LEFT_TEXT}"
: "${LIGHT_MODE_BOTTOM_TEXT}";
const std::string light_mode_group_2 = is_model_us
? "${LIGHT_MODE_RIGHT_TEXT}"
: "${LIGHT_MODE_TOP_TEXT}";
// 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};
}
// Get switch states
const std::vector<switch_::Switch*> switches = {sw_relay_1, sw_relay_2, sw_relay_3, sw_relay_4};
// Check if light vectors are initialized
if (id(gb_lights_1).empty() || id(gb_lights_2).empty()) {
ESP_LOGE("core_hw_relays", "Light vector not initialized");
return;
}
// Update lights based on relay states
for (size_t i = 0; i < relay_modes.size() && i < id(gb_lights_1).size(); ++i) {
if (!relay_modes[i] || !switches[i]) continue;
auto light_index = relay_modes[i]->active_index();
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) && i < id(gb_lights_1).size() && id(gb_lights_1)[i]) {
if (switch_state)
id(gb_lights_1)[i]->turn_on();
else
id(gb_lights_1)[i]->turn_off();
// Determine relay state and light mode
switch (i) {
case 0:
relay_state = id(sw_relay_1).state;
light_mode = is_model_us
? id(sl_relay_1_light_mode_us)->state
: id(sl_relay_1_light_mode_eu)->state;
break;
case 1:
relay_state = id(sw_relay_2).state;
light_mode = is_model_us
? id(sl_relay_2_light_mode_us)->state
: id(sl_relay_2_light_mode_eu)->state;
break;
case 2:
relay_state = id(sw_relay_3).state;
light_mode = is_model_us
? id(sl_relay_3_light_mode_us)->state
: id(sl_relay_3_light_mode_eu)->state;
break;
case 3:
relay_state = id(sw_relay_4).state;
light_mode = is_model_us
? id(sl_relay_4_light_mode_us)->state
: id(sl_relay_4_light_mode_eu)->state;
break;
}
// Update second light group (top/right)
if ((light_idx == 2 || light_idx == 3) && i < id(gb_lights_2).size() && id(gb_lights_2)[i]) {
if (switch_state)
id(gb_lights_2)[i]->turn_on();
else
id(gb_lights_2)[i]->turn_off();
ESP_LOGV("core_hw_relays", "Relay %" PRIu8 " is %s", i + 1, relay_state ? "ON" : "OFF");
ESP_LOGV("core_hw_relays", "Light Mode for Relay %" PRIu8 ": %s", i + 1, light_mode.c_str());
// Use light_set_state for light updates
if (relay_state) {
if (light_mode == light_mode_group_1 || light_mode == "${LIGHT_MODE_BOTH_TEXT}") {
light_set_state->execute(1, i, true); // Left/bottom light
}
if (light_mode == light_mode_group_2 || light_mode == "${LIGHT_MODE_BOTH_TEXT}") {
light_set_state->execute(2, i, true); // Right/top light
}
} else {
light_set_state->execute(1, i, false); // Turn off left/bottom light
light_set_state->execute(2, i, false); // Turn off right/top light
}
}