Reduce number of calls to Nextion - Relay and buttons bars

Helps with #1949
This commit is contained in:
Edward Firmo
2024-03-20 19:46:47 +01:00
parent 231c9aa426
commit 5e5476f6e2

View File

@@ -119,9 +119,6 @@ ota:
##### Adds custom library for NSPanel HA Blueprint project ##### Adds custom library for NSPanel HA Blueprint project
nspanel_ha_blueprint: nspanel_ha_blueprint:
##### JSON - Used to parse json and for Upload TFT #####
json:
##### LOGGER ##### ##### LOGGER #####
logger: logger:
id: logger_std id: logger_std
@@ -154,9 +151,10 @@ time:
on_time: on_time:
- seconds: 0 - seconds: 0
then: then:
- script.execute: refresh_datetime - lambda:
- script.execute: refresh_relays refresh_datetime->execute();
- script.execute: refresh_hardware_buttons_bars refresh_relays->execute(3);
refresh_hardware_buttons_bars->execute(3);
on_time_sync: on_time_sync:
then: then:
@@ -289,7 +287,7 @@ api:
if (button_mask & 2) // Checks if the second least significant bit is set (right button) if (button_mask & 2) // Checks if the second least significant bit is set (right button)
update_bitwise_setting(id(buttons_settings), state, ButtonSettings::ButtonRight_State); update_bitwise_setting(id(buttons_settings), state, ButtonSettings::ButtonRight_State);
// Refreshes the indication bars on the display // Refreshes the indication bars on the display
refresh_hardware_buttons_bars->execute(); refresh_hardware_buttons_bars->execute(button_mask);
# Icon Service # Icon Service
- service: icon - service: icon
@@ -400,7 +398,8 @@ api:
button_bar_color_off[2])); button_bar_color_off[2]));
// Refresh relays display // Refresh relays display
refresh_relays->execute(); refresh_relays->execute(3);
refresh_hardware_buttons_bars->execute(3);
} }
blueprint_status->publish_state(int(blueprint_status->raw_state) | (1 << 4)); blueprint_status->publish_state(int(blueprint_status->raw_state) | (1 << 4));
# Sets up the "Home" page in ESPHome with customized settings and UI elements as defined in the project blueprint. # Sets up the "Home" page in ESPHome with customized settings and UI elements as defined in the project blueprint.
@@ -1583,10 +1582,14 @@ switch:
restore_mode: RESTORE_DEFAULT_OFF restore_mode: RESTORE_DEFAULT_OFF
on_turn_on: on_turn_on:
then: then:
- script.execute: refresh_relays - lambda:
refresh_relays->execute(1);
refresh_hardware_buttons_bars->execute(1);
on_turn_off: on_turn_off:
then: then:
- script.execute: refresh_relays - lambda:
refresh_relays->execute(1);
refresh_hardware_buttons_bars->execute(1);
##### PHYSICAL SWITCH 2 ###### ##### PHYSICAL SWITCH 2 ######
- name: Relay 2 - name: Relay 2
platform: gpio platform: gpio
@@ -1596,10 +1599,14 @@ switch:
restore_mode: RESTORE_DEFAULT_OFF restore_mode: RESTORE_DEFAULT_OFF
on_turn_on: on_turn_on:
then: then:
- script.execute: refresh_relays - lambda:
refresh_relays->execute(2);
refresh_hardware_buttons_bars->execute(2);
on_turn_off: on_turn_off:
then: then:
- script.execute: refresh_relays - lambda:
refresh_relays->execute(2);
refresh_hardware_buttons_bars->execute(2);
##### DISPLAY ALWAYS ON ##### ##### DISPLAY ALWAYS ON #####
- name: Nextion display - Power - name: Nextion display - Power
@@ -1733,7 +1740,7 @@ text_sensor:
const cJSON* title = cJSON_GetObjectItemCaseSensitive(json, "mui"); const cJSON* title = cJSON_GetObjectItemCaseSensitive(json, "mui");
if (json_cmp_string(code_format, "number") and (json_cmp_string(key, "disarm") or json_cmp_string(code_arm_req, "1"))) { if (json_cmp_string(code_format, "number") and (json_cmp_string(key, "disarm") or json_cmp_string(code_arm_req, "1"))) {
goto_page->execute("keyb_num"); goto_page->execute("keyb_num");
disp1->set_component_value("keyb_num.page_id", 23); //Calling from Alarm page disp1->set_component_value("keyb_num.page_id", get_page_id("alarm")); //Calling from Alarm page
disp1->set_component_text("keyb_num.domain", page->valuestring); disp1->set_component_text("keyb_num.domain", page->valuestring);
disp1->set_component_text("keyb_num.key", key->valuestring); disp1->set_component_text("keyb_num.key", key->valuestring);
disp1->set_component_text("keyb_num.value", value->valuestring); disp1->set_component_text("keyb_num.value", value->valuestring);
@@ -1762,7 +1769,7 @@ text_sensor:
if (json_cmp_string(key, "volume_mute")) if (json_cmp_string(key, "volume_mute"))
ha_call_service->execute("media_player.volume_mute", "is_volume_muted", value->valuestring, detailed_entity->state.c_str()); ha_call_service->execute("media_player.volume_mute", "is_volume_muted", value->valuestring, detailed_entity->state.c_str());
else if (json_cmp_string(key, "volume_set")) else if (json_cmp_string(key, "volume_set"))
ha_call_service->execute("media_player.volume_set", "volume_level", json_percentage_to_float_string(value).c_str()), detailed_entity->state.c_str()); ha_call_service->execute("media_player.volume_set", "volume_level", json_percentage_to_float_string(value).c_str(), detailed_entity->state.c_str());
else if ((key != NULL and key->valuestring != NULL and key->valuestring[0] != '\0')) else if ((key != NULL and key->valuestring != NULL and key->valuestring[0] != '\0'))
ha_call_service->execute((std::string("media_player.") + key->valuestring), "", "", detailed_entity->state.c_str()); ha_call_service->execute((std::string("media_player.") + key->valuestring), "", "", detailed_entity->state.c_str());
} }
@@ -2199,7 +2206,7 @@ script:
ESP_LOGD("script.page_changed", "Entity shown: %s", detailed_entity->state.c_str()); ESP_LOGD("script.page_changed", "Entity shown: %s", detailed_entity->state.c_str());
// Update buttons bars on screen // Update buttons bars on screen
refresh_hardware_buttons_bars->execute(); refresh_hardware_buttons_bars->execute(3);
// Reset timers // Reset timers
if (current_page->state != "screensaver") timer_reset_all->execute(); if (current_page->state != "screensaver") timer_reset_all->execute();
@@ -2245,7 +2252,7 @@ script:
else if (current_page->state == "weather04") page_weather04->execute(); else if (current_page->state == "weather04") page_weather04->execute();
else if (current_page->state == "weather05") page_weather05->execute(); else if (current_page->state == "weather05") page_weather05->execute();
- delay: 1s - delay: 1s
- script.execute: refresh_hardware_buttons_bars - lambda: refresh_hardware_buttons_bars->execute(3);
- id: page_climate - id: page_climate
mode: restart mode: restart
@@ -2308,8 +2315,9 @@ script:
- id: page_home - id: page_home
mode: restart mode: restart
then: then:
- script.execute: refresh_relays - lambda:
- script.execute: refresh_wifi_icon refresh_relays->execute(3);
refresh_wifi_icon->execute();
- id: page_keyb_num - id: page_keyb_num
mode: restart mode: restart
@@ -2428,6 +2436,8 @@ script:
- id: refresh_hardware_buttons_bars - id: refresh_hardware_buttons_bars
mode: restart mode: restart
parameters:
button_mask: uint8_t
then: then:
- lambda: |- - lambda: |-
#if ESPHOME_LOG_LEVEL > ESPHOME_LOG_LEVEL_DEBUG #if ESPHOME_LOG_LEVEL > ESPHOME_LOG_LEVEL_DEBUG
@@ -2435,11 +2445,12 @@ script:
ESP_LOGV("script.refresh_hardware_buttons_bars", "Page id: %i", get_page_id(current_page->state.c_str())); ESP_LOGV("script.refresh_hardware_buttons_bars", "Page id: %i", get_page_id(current_page->state.c_str()));
ESP_LOGV("script.refresh_hardware_buttons_bars", "buttons_bars_pages: %i", id(buttons_bars_pages)); ESP_LOGV("script.refresh_hardware_buttons_bars", "buttons_bars_pages: %i", id(buttons_bars_pages));
ESP_LOGV("script.refresh_hardware_buttons_bars", "relay_settings: %i", id(relay_settings)); ESP_LOGV("script.refresh_hardware_buttons_bars", "relay_settings: %i", id(relay_settings));
ESP_LOGV("script.refresh_hardware_buttons_bars", "button_mask: %i", button_mask);
#endif #endif
if (!id(is_uploading_tft) and ((id(buttons_bars_pages) & (1 << get_page_id(current_page->state.c_str()))) != 0)) { if (!id(is_uploading_tft) and ((id(buttons_bars_pages) & (1 << get_page_id(current_page->state.c_str()))) != 0)) {
switch (int(display_mode->state)) { switch (int(display_mode->state)) {
case 1: // EU model case 1: // EU model
if (id(buttons_settings) & ButtonSettings::ButtonLeft_Enabled) { // Left button if (button_mask & 1 and id(buttons_settings) & ButtonSettings::ButtonLeft_Enabled) { // Left button
disp1->fill_area(48, 307, 118, 3, disp1->fill_area(48, 307, 118, 3,
(((id(relay_settings) & RelaySettings::Relay1_Local) and relay_1->state) or (id(buttons_settings) & ButtonSettings::ButtonLeft_State)) ? (((id(relay_settings) & RelaySettings::Relay1_Local) and relay_1->state) or (id(buttons_settings) & ButtonSettings::ButtonLeft_State)) ?
id(buttons_color_on) : id(buttons_color_off)); id(buttons_color_on) : id(buttons_color_off));
@@ -2447,7 +2458,7 @@ script:
(((id(relay_settings) & RelaySettings::Relay1_Local) and relay_1->state) or (id(buttons_settings) & ButtonSettings::ButtonLeft_State)) ? (((id(relay_settings) & RelaySettings::Relay1_Local) and relay_1->state) or (id(buttons_settings) & ButtonSettings::ButtonLeft_State)) ?
id(buttons_color_on) : id(buttons_color_off)); id(buttons_color_on) : id(buttons_color_off));
} }
if (id(buttons_settings) & ButtonSettings::ButtonRight_Enabled) { // Right button if (button_mask & 2 and id(buttons_settings) & ButtonSettings::ButtonRight_Enabled) { // Right button
disp1->fill_area(289, 307, 118, 3, disp1->fill_area(289, 307, 118, 3,
(((id(relay_settings) & RelaySettings::Relay2_Local) and relay_2->state) or (id(buttons_settings) & ButtonSettings::ButtonRight_State)) ? (((id(relay_settings) & RelaySettings::Relay2_Local) and relay_2->state) or (id(buttons_settings) & ButtonSettings::ButtonRight_State)) ?
id(buttons_color_on) : id(buttons_color_off)); id(buttons_color_on) : id(buttons_color_off));
@@ -2457,7 +2468,7 @@ script:
} }
break; break;
case 2: // US Portrait case 2: // US Portrait
if (id(buttons_settings) & ButtonSettings::ButtonLeft_Enabled) { // Left button if (button_mask & 1 and id(buttons_settings) & ButtonSettings::ButtonLeft_Enabled) { // Left button
disp1->fill_area(17, 466, 118, 3, disp1->fill_area(17, 466, 118, 3,
(((id(relay_settings) & RelaySettings::Relay1_Local) and relay_1->state) or (id(buttons_settings) & ButtonSettings::ButtonLeft_State)) ? (((id(relay_settings) & RelaySettings::Relay1_Local) and relay_1->state) or (id(buttons_settings) & ButtonSettings::ButtonLeft_State)) ?
id(buttons_color_on) : id(buttons_color_off)); id(buttons_color_on) : id(buttons_color_off));
@@ -2465,7 +2476,7 @@ script:
(((id(relay_settings) & RelaySettings::Relay1_Local) and relay_1->state) or (id(buttons_settings) & ButtonSettings::ButtonLeft_State)) ? (((id(relay_settings) & RelaySettings::Relay1_Local) and relay_1->state) or (id(buttons_settings) & ButtonSettings::ButtonLeft_State)) ?
id(buttons_color_on) : id(buttons_color_off)); id(buttons_color_on) : id(buttons_color_off));
} }
if (id(buttons_settings) & ButtonSettings::ButtonRight_Enabled) { // Right button if (button_mask & 2 and id(buttons_settings) & ButtonSettings::ButtonRight_Enabled) { // Right button
disp1->fill_area(184, 466, 118, 3, disp1->fill_area(184, 466, 118, 3,
(((id(relay_settings) & RelaySettings::Relay2_Local) and relay_2->state) or (id(buttons_settings) & ButtonSettings::ButtonRight_State)) ? (((id(relay_settings) & RelaySettings::Relay2_Local) and relay_2->state) or (id(buttons_settings) & ButtonSettings::ButtonRight_State)) ?
id(buttons_color_on) : id(buttons_color_off)); id(buttons_color_on) : id(buttons_color_off));
@@ -2475,7 +2486,7 @@ script:
} }
break; break;
case 3: // US Landscape case 3: // US Landscape
if (id(buttons_settings) & ButtonSettings::ButtonLeft_Enabled) { // Left button if (button_mask & 1 and id(buttons_settings) & ButtonSettings::ButtonLeft_Enabled) { // Left button
disp1->fill_area(467, 174, 3, 118, disp1->fill_area(467, 174, 3, 118,
(((id(relay_settings) & RelaySettings::Relay1_Local) and relay_1->state) or (id(buttons_settings) & ButtonSettings::ButtonLeft_State)) ? (((id(relay_settings) & RelaySettings::Relay1_Local) and relay_1->state) or (id(buttons_settings) & ButtonSettings::ButtonLeft_State)) ?
id(buttons_color_on) : id(buttons_color_off)); id(buttons_color_on) : id(buttons_color_off));
@@ -2483,7 +2494,7 @@ script:
(((id(relay_settings) & RelaySettings::Relay1_Local) and relay_1->state) or (id(buttons_settings) & ButtonSettings::ButtonLeft_State)) ? (((id(relay_settings) & RelaySettings::Relay1_Local) and relay_1->state) or (id(buttons_settings) & ButtonSettings::ButtonLeft_State)) ?
id(buttons_color_on) : id(buttons_color_off)); id(buttons_color_on) : id(buttons_color_off));
} }
if (id(buttons_settings) & ButtonSettings::ButtonRight_Enabled) { // Right button if (button_mask & 2 and id(buttons_settings) & ButtonSettings::ButtonRight_Enabled) { // Right button
disp1->fill_area(467, 28, 3, 118, disp1->fill_area(467, 28, 3, 118,
(((id(relay_settings) & RelaySettings::Relay2_Local) and relay_2->state) or (id(buttons_settings) & ButtonSettings::ButtonRight_State)) ? (((id(relay_settings) & RelaySettings::Relay2_Local) and relay_2->state) or (id(buttons_settings) & ButtonSettings::ButtonRight_State)) ?
id(buttons_color_on) : id(buttons_color_off)); id(buttons_color_on) : id(buttons_color_off));
@@ -2497,18 +2508,19 @@ script:
- id: refresh_relays - id: refresh_relays
mode: restart mode: restart
parameters:
relay_mask: uint8_t
then: then:
- lambda: |- - lambda: |-
// Chips - Relays // Chips - Relays
disp1->set_component_text("home.chip_relay1", (relay_1->state) ? id(home_relay1_icon) : "\uFFFF"); if (!id(is_uploading_tft) and relay_mask & 1) disp1->set_component_text("home.chip_relay1", (relay_1->state) ? id(home_relay1_icon) : "\uFFFF");
disp1->set_component_text("home.chip_relay2", (relay_2->state) ? id(home_relay2_icon) : "\uFFFF"); if (!id(is_uploading_tft) and relay_mask & 2) disp1->set_component_text("home.chip_relay2", (relay_2->state) ? id(home_relay2_icon) : "\uFFFF");
refresh_hardware_buttons_bars->execute();
- id: refresh_wifi_icon - id: refresh_wifi_icon
mode: restart mode: restart
then: then:
- lambda: |- - lambda: |-
if (nextion_init->state) { if (!id(is_uploading_tft) and nextion_init->state) {
disp1->send_command_printf("api=%i", (wifi_component->is_connected() and api_server->is_connected() and blueprint_status->state > 99) ? 1 : 0); disp1->send_command_printf("api=%i", (wifi_component->is_connected() and api_server->is_connected() and blueprint_status->state > 99) ? 1 : 0);
// Update Wi-Fi icon color // Update Wi-Fi icon color
disp1->set_component_font_color("home.wifi_icon", (blueprint_status->state > 99) ? (wifi_rssi->state > -70 ? 33808 : 64992) : 63488); disp1->set_component_font_color("home.wifi_icon", (blueprint_status->state > 99) ? (wifi_rssi->state > -70 ? 33808 : 64992) : 63488);