Fix entity info when calling detailed pages

Solves #1022
This commit is contained in:
Edward Firmo
2023-09-09 02:58:23 +02:00
parent 5ed58d8f24
commit b5bb42c68d
41 changed files with 943 additions and 325 deletions

View File

@@ -276,7 +276,9 @@ api:
ESP_LOGV("api.service.open_entity_settings_page", "page_icon_color: %i, %i, %i", page_icon_color[0], page_icon_color[1], page_icon_color[2] );
ESP_LOGV("api.service.open_entity_settings_page", "entity: %s", entity.c_str());
ESP_LOGV("api.service.open_entity_settings_page", "back_page: %s", back_page.c_str());
id(disp1).set_component_text_printf("home.entity", "%s", entity.c_str());
std::string cmp_entity = page.c_str() + std::string(".entity");
ESP_LOGV("api.service.open_entity_settings_page", "cmp_entity: %s", cmp_entity.c_str());
id(disp1).set_component_text_printf(cmp_entity.c_str(), "%s", entity.c_str());
std::string cmd_page = std::string("page ") + page.c_str();
id(disp1).send_command_printf(cmd_page.c_str());
id(disp1).set_component_text_printf("page_label", "%s", page_label.c_str());
@@ -356,12 +358,14 @@ api:
btn_icon: string
btn_label: string
btn_bri_txt: string
#entity: string
then:
- lambda: |-
// ESP_LOGV("nextion", "set button %s", btn_id.c_str());
std::string btnicon = btn_id.c_str() + std::string("icon");
std::string btntext = btn_id.c_str() + std::string("text");
std::string btnbri = btn_id.c_str() + std::string("bri");
//std::string btnentity = btn_id.c_str() + std::string("entity");
id(disp1).send_command_printf("%spic.pic=%i", btn_id.c_str(), btn_pic);
id(set_component_color).execute(btnicon.c_str(), btn_icon_font, btn_bg);
id(set_component_color).execute(btntext.c_str(), btn_txt_font, btn_bg);
@@ -372,7 +376,7 @@ api:
id(disp1).set_component_text_printf(btnbri.c_str(), "%s", btn_bri_txt.c_str());
else
id(disp1).set_component_text_printf(btnbri.c_str(), " ");
//id(disp1).set_component_text_printf(btnentity.c_str(), "%s", entity.c_str());
##### SERVICE TO WAKE UP THE DISPLAY #####
- service: wake_up
@@ -924,6 +928,17 @@ binary_sensor:
##### START - SENSOR CONFIGURATION #####
sensor:
##### touchevent sensor, Reset the page timeout #####
- id: touchevent
platform: nextion
nextion_id: disp1
component_name: touchevent
internal: true
on_value:
then:
- lambda: |-
id(page_timer)->execute(int(id(page_timeout).state));
##### Uptime Sensors #####
- name: ${device_name} Uptime seconds
id: uptime_sec
@@ -1162,14 +1177,125 @@ text_sensor:
id(display_wrapped_text).execute("notification.notifi_text01", id(notification_text).state.c_str(), id(display_mode) == 2 ? 23 : 32);
}
}
else if (page=="media_player")
{
ESP_LOGV("text_sensor.nspanelevent", "Construct media_player page");
id(disp1).set_component_text_printf("bt_on_off", "%s", "\uE424"); //mdi:power
id(disp1).set_component_text_printf("bt_prev", "%s", "\uE4AD"); //mdi:skip-previous
id(disp1).set_component_text_printf("bt_next", "%s", "\uE4AC"); //mdi:skip-next
id(disp1).set_component_text_printf("bt_play_pause", "%s", "\uE3E3"); //mdi:pause
id(disp1).set_component_text_printf("bt_stop", "%s", "\uE4DA"); //mdi:stop
id(disp1).set_component_text_printf("bt_mute", "%s", "\uE75E"); //mdi:volume-mute
id(disp1).set_component_text_printf("bt_vol_down", "%s", "\uE75D"); //mdi:volume-minus
id(disp1).set_component_text_printf("bt_vol_up", "%s", "\uE75C"); //mdi:volume-plus
}
}
if (page=="boot")
##### NSPanel event - Execute actions from ESPHome - NO push to HA #####
- name: ${device_name} NSPanel local event
platform: nextion
nextion_id: disp1
id: disp1_local_event
component_name: localevent
internal: true
filters:
- lambda: |-
x = x.c_str();
x.shrink_to_fit();
return x;
on_value:
then:
- lambda: |-
ESP_LOGV("text_sensor.localevent", "Starting");
id(page_timer)->execute(int(id(page_timeout).state));
DynamicJsonDocument doc(256);
deserializeJson(doc, x);
std::string page = doc["page"];
ESP_LOGV("text_sensor.localevent", "page=%s", page.c_str());
std::string event = doc["event"];
ESP_LOGV("text_sensor.localevent", "event=%s", event.c_str());
std::string key = doc["key"];
ESP_LOGV("text_sensor.localevent", "key=%s", key.c_str());
std::string value = doc["value"];
ESP_LOGV("text_sensor.localevent", "value=%s", value.c_str());
std::string entity = doc["entity"];
ESP_LOGV("text_sensor.localevent", "entity=%s", entity.c_str());
int embedded = doc["embedded"];
ESP_LOGV("text_sensor.localevent", "embedded=%i", embedded);
std::string service = "";
auto ha_event = new esphome::api::CustomAPIDevice();
ha_event->fire_homeassistant_event("esphome.nspanel_ha_blueprint",
{
{"type", "generic"},
{"page", page},
{"event", event},
{"value", value},
{"entity", entity}
});
if (event=="pagechanged")
{
ESP_LOGD("text_sensor.localevent", "New page: %s", page.c_str());
ESP_LOGV("text_sensor.localevent", "Trigger HA event");
ha_event->fire_homeassistant_event("esphome.nspanel_ha_blueprint",
{
{"type", "page_changed"},
{"page", page},
{"entity", entity}
});
ESP_LOGV("text_sensor.localevent", "Call add-ons scripts for new page");
id(addon_climate_set_climate).execute(page=="climate" and entity=="embedded_climate");
ESP_LOGV("text_sensor.localevent", "Publish current_page sensor");
id(current_page).publish_state(page);
ESP_LOGV("text_sensor.localevent", "Construct new page");
}
if (page == "climate") id(service_call_climate)->execute(entity.c_str(), key.c_str(), value.c_str(), (embedded==1));
else if (page == "alarm")
{
std::string code_format = doc["code_format"];
std::string code_arm_req = doc["code_arm_req"];
std::string title = doc["mui"];
if (code_format=="number" and (key=="disarm" or code_arm_req=="1"))
{
id(disp1).send_command_printf("page keyb_num");
id(disp1).set_component_value("keyb_num.page_id", 23); //Calling from Alarm page
id(disp1).set_component_text_printf("keyb_num.domain", "%s", page.c_str());
id(disp1).set_component_text_printf("keyb_num.key", "%s", key.c_str());
id(disp1).set_component_text_printf("keyb_num.value", "%s", value.c_str());
id(disp1).set_component_text_printf("keyb_num.entity", "%s", entity.c_str());
id(disp1).set_component_text_printf("keyb_num.title", "%s", title.c_str());
}
else id(service_call_alarm_control_panel)->execute(entity.c_str(), key.c_str(), code_format.c_str(), "");
}
else if (page == "keyb_num")
{
std::string base_domain = doc["base_domain"];
if (base_domain == "alarm")
{
std::string code_format = doc["code_format"];
std::string pin = doc["pin"];
id(service_call_alarm_control_panel)->execute(entity.c_str(), key.c_str(), code_format.c_str(), pin.c_str());
}
else if (base_domain == "" or base_domain.empty()) base_domain = "home";
id(disp1).send_command_printf("page %s", base_domain.c_str());
}
else if (page == "light") id(ha_call_service)->execute("light.turn_on", key.c_str(), value.c_str(), entity.c_str());
else if (page == "cover")
{
if (key == "position") id(ha_call_service)->execute("cover.set_cover_position", key.c_str(), value.c_str(), entity.c_str());
else id(ha_call_service)->execute((std::string("cover.") + key.c_str()), "", "", entity.c_str());
}
else if (page == "fan")
{
if (key == "stop" or value == "0") id(ha_call_service)->execute("fan.turn_off", "", "", entity.c_str());
else id(ha_call_service)->execute("fan.turn_on", key.c_str(), value.c_str(), entity.c_str());
}
else if (page=="boot")
{
// Detect display mode - Need review
if (doc.containsKey("display_mode"))
{
std::string display_mode_str = doc["display_mode"];
ESP_LOGV("text_sensor.nspanelevent", "display_mode: %s", display_mode_str.c_str());
ESP_LOGV("text_sensor.localevent", "display_mode: %s", display_mode_str.c_str());
float display_mode_float = stof(display_mode_str);
if (display_mode_float > 0) id(display_mode) = int(display_mode_float);
}
@@ -1183,9 +1309,9 @@ text_sensor:
id(check_versions).execute();
// Detect timeout
if (component=="timeout")
if (event=="timeout")
{
ESP_LOGV("text_sensor.nspanelevent", "Trigger boot timeout event");
ESP_LOGV("text_sensor.localevent", "Trigger boot timeout event");
ha_event->fire_homeassistant_event("esphome.nspanel_ha_blueprint",
{
{"type", "boot"},
@@ -1194,82 +1320,13 @@ text_sensor:
});
if (stof(value) >= 5)
{
ESP_LOGV("text_sensor.nspanelevent", "Jump to wakeup page: %i", id(wakeup_page_id));
ESP_LOGV("text_sensor.localevent", "Jump to wakeup page: %i", id(wakeup_page_id));
id(disp1).send_command_printf("page %i", id(wakeup_page_id));
}
}
}
##### NSPanel event - Execute actions from ESPHome - NO push to HA #####
- name: ${device_name} NSPanel local event
platform: nextion
nextion_id: disp1
id: disp1_local_event
component_name: localevent
internal: True
filters:
- lambda: |-
x = x.c_str();
x.shrink_to_fit();
return x;
on_value:
then:
- lambda: |-
id(page_timer)->execute(int(id(page_timeout).state));
DynamicJsonDocument doc(1024);
deserializeJson(doc, x);
std::string domain = doc["domain"];
std::string key = doc["key"];
std::string value = doc["value"];
std::string entity = doc["entity"];
int embedded = doc["embedded"];
std::string service = "";
ESP_LOGV("text_sensor.localevent", "domain=%s", domain.c_str());
ESP_LOGV("text_sensor.localevent", "key=%s", key.c_str());
ESP_LOGV("text_sensor.localevent", "value=%s", value.c_str());
ESP_LOGV("text_sensor.localevent", "entity=%s", entity.c_str());
ESP_LOGV("text_sensor.localevent", "embedded=%i", embedded);
if (domain == "climate") id(service_call_climate)->execute(entity.c_str(), key.c_str(), value.c_str(), (embedded==1));
else if (domain == "alarm")
{
std::string code_format = doc["code_format"];
std::string code_arm_req = doc["code_arm_req"];
std::string title = doc["mui"];
if (code_format=="number" and (key=="disarm" or code_arm_req=="1"))
{
id(disp1).send_command_printf("page keyb_num");
id(disp1).set_component_value("keyb_num.page_id", 23); //Calling from Alarm page
id(disp1).set_component_text_printf("keyb_num.domain", "%s", domain.c_str());
id(disp1).set_component_text_printf("keyb_num.key", "%s", key.c_str());
id(disp1).set_component_text_printf("keyb_num.value", "%s", value.c_str());
id(disp1).set_component_text_printf("keyb_num.entity", "%s", entity.c_str());
id(disp1).set_component_text_printf("keyb_num.title", "%s", title.c_str());
}
else id(service_call_alarm_control_panel)->execute(entity.c_str(), key.c_str(), code_format.c_str(), "");
}
else if (domain == "keyb_num")
{
std::string base_domain = doc["base_domain"];
if (base_domain == "alarm")
{
std::string code_format = doc["code_format"];
std::string pin = doc["pin"];
id(service_call_alarm_control_panel)->execute(entity.c_str(), key.c_str(), code_format.c_str(), pin.c_str());
}
else if (base_domain == "" or base_domain.empty()) base_domain = "home";
id(disp1).send_command_printf("page %s", base_domain.c_str());
}
else if (domain == "light") id(ha_call_service)->execute("light.turn_on", key.c_str(), value.c_str(), entity.c_str());
else if (domain == "cover")
{
if (key == "position") id(ha_call_service)->execute("cover.set_cover_position", key.c_str(), value.c_str(), entity.c_str());
else id(ha_call_service)->execute((std::string("cover.") + key.c_str()), "", "", entity.c_str());
}
else if (domain == "fan")
{
if (key == "stop" or value == "0") id(ha_call_service)->execute("fan.turn_off", "", "", entity.c_str());
else id(ha_call_service)->execute("fan.turn_on", key.c_str(), value.c_str(), entity.c_str());
}
ESP_LOGV("text_sensor.localevent", "Finished");
##### touchevent sensor, Reset the page timeout #####
- id: disp1_touchevent
@@ -1278,11 +1335,11 @@ text_sensor:
#name: ${device_name} touchevent
component_name: touchevent
internal: true
#filters:
# - lambda: |-
# x = x.c_str();
# x.shrink_to_fit();
# return x;
filters:
- lambda: |-
x = x.c_str();
x.shrink_to_fit();
return x;
on_value:
then:
- lambda: |-
@@ -1498,6 +1555,7 @@ display:
on_setup:
then:
- lambda: |-
ESP_LOGV("display.disp1", "Starting");
id(disp1).send_command_printf("page 8");
id(disp1).set_component_text_printf("boot.esph_version", "%s", "${version}"); // ### esphome-version ###
id(disp1).show_component("bt_reboot");