Skip to content

Commit

Permalink
Merge pull request #56 from edwardtfn/new-events
Browse files Browse the repository at this point in the history
New event handler
  • Loading branch information
edwardtfn authored Dec 23, 2024
2 parents bd4f683 + 5d5a3d3 commit eb1a395
Show file tree
Hide file tree
Showing 7 changed files with 617 additions and 199 deletions.
33 changes: 26 additions & 7 deletions ESPHome/TX-Ultimate-Easy-ESPHome_core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ substitutions:

packages:
# yamllint disable rule:colons
core_api: !include TX-Ultimate-Easy-ESPHome_core_api.yaml
core_hw_buttons: !include TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml
core_hw_leds: !include TX-Ultimate-Easy-ESPHome_core_hw_leds.yaml
core_hw_relays: !include TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml
Expand All @@ -36,6 +35,12 @@ packages:
core_hw_vibration: !include TX-Ultimate-Easy-ESPHome_core_hw_vibration.yaml
# yamllint enable rule:colons

api:
id: api_server
on_client_connected:
then:
- script.execute: publish_device_info

button:
- id: bt_restart
name: Restart
Expand Down Expand Up @@ -82,13 +87,25 @@ ota:
psram:

script:
- id: api_send_ha_event_boot
mode: queued
parameters:
type: string
then:
- homeassistant.event:
event: esphome.tx_ultimate_easy
data:
device_name: !lambda return tx_device_name->state.c_str();
firmware: ${version}
domain: boot
type: !lambda return type.c_str();

- id: boot_done
mode: restart
then:
- script.execute:
id: send_event_to_ha
component: boot
event: done
id: api_send_ha_event_boot
type: done

- id: boot_initialize
mode: restart
Expand All @@ -98,9 +115,8 @@ script:
# - HW Relays
# - HW Touch
- script.execute:
id: send_event_to_ha
component: boot
event: start
id: api_send_ha_event_boot
type: start

- id: boot_sequence
mode: restart
Expand Down Expand Up @@ -144,6 +160,9 @@ select:
entity_category: config
disabled_by_default: false
icon: mdi:dip-switch
on_value:
then:
- lambda: tx_ultimate->set_gang_count(static_cast<uint8_t>(i)+1);

text_sensor:
- id: tx_fw_version
Expand Down
41 changes: 0 additions & 41 deletions ESPHome/TX-Ultimate-Easy-ESPHome_core_api.yaml

This file was deleted.

113 changes: 78 additions & 35 deletions ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_buttons.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,33 +108,89 @@ script:
- id: button_action
mode: parallel
parameters:
component: string
event: string
button: uint8_t
action: string
count: uint8_t
then:
# Extended by:
# - core_api
- homeassistant.event:
event: esphome.tx_ultimate_easy
data:
device_name: !lambda return tx_device_name->state.c_str();
firmware: ${version}
domain: touch
type: button
action: !lambda return action.c_str();
button_id: !lambda return button;
count: !lambda return count;
position: !lambda return id(button_press_position);
- lambda: |-
ESP_LOGI("core_hw_buttons", "Button '%s' action: '%s'", component.c_str(), event.c_str());
ESP_LOGI("core_hw_buttons", "Button %" PRIu8 " action: '%s'", button, action.c_str());
if (action == "click") {
switch (button) {
case ${BUTTON_1_ID}:
bs_button_1_click_event->publish_state(true);
break;
case ${BUTTON_2_ID}:
bs_button_2_click_event->publish_state(true);
break;
case ${BUTTON_3_ID}:
bs_button_3_click_event->publish_state(true);
break;
case ${BUTTON_4_ID}:
bs_button_4_click_event->publish_state(true);
break;
}
} else if (action == "double_click") {
switch (button) {
case ${BUTTON_1_ID}:
bs_button_1_double_click_event->publish_state(true);
break;
case ${BUTTON_2_ID}:
bs_button_2_double_click_event->publish_state(true);
break;
case ${BUTTON_3_ID}:
bs_button_3_double_click_event->publish_state(true);
break;
case ${BUTTON_4_ID}:
bs_button_4_double_click_event->publish_state(true);
break;
}
} else if (action == "long_press") {
switch (button) {
case ${BUTTON_1_ID}:
bs_button_1_long_press_event->publish_state(true);
break;
case ${BUTTON_2_ID}:
bs_button_2_long_press_event->publish_state(true);
break;
case ${BUTTON_3_ID}:
bs_button_3_long_press_event->publish_state(true);
break;
case ${BUTTON_4_ID}:
bs_button_4_long_press_event->publish_state(true);
break;
}
}
id(button_press_button) = 0;
id(click_counter) = 0;
id(button_press_position) = 0;
id(button_press_start_time) = 0;
id(click_counter) = 0;
buttons_release->execute();
- id: button_click_event
mode: restart
parameters:
button_id: uint8_t
button: uint8_t
click_count: uint8_t
then:
- delay:
milliseconds: ${BUTTON_MULTI_CLICK_DELAY}
- lambda: |-
const std::string button_name = "bs_button_" + std::to_string(button_id);
std::string event_name;
if (click_count == 1) event_name = "click";
else if (click_count == 2) event_name = "double_click";
else event_name = std::to_string(click_count) + "_click";
button_action->execute(button_name.c_str(), event_name.c_str());
else event_name = "multiple_click";
button_action->execute(button, event_name.c_str(), click_count);
- id: buttons_release
mode: restart
Expand All @@ -157,30 +213,24 @@ script:
then:
- script.execute:
id: touch_on_press_buttons
touch_position: !lambda return touch_position;
button: !lambda return button;
position: !lambda return position;

- id: touch_on_press_buttons
mode: restart
parameters:
touch_position: uint8_t
button: uint8_t
position: uint8_t
then:
- lambda: |-
id(button_press_start_time) = millis();
id(button_press_position) = touch_position;
uint8_t button = 0;
auto model_index = sl_tx_model_gang->active_index();
if (model_index.has_value()) {
const uint8_t model_idx = model_index.value() + 1; // Increment for 1-based indexing
if (model_idx == 1) {
button = 1; // Single button, always 1
} else {
const uint8_t width = ${TOUCH_POSITION_MAX_VALUE} / model_idx; // Width of each button region
ESP_LOGV("core_hw_buttons", "Button regions: width=%" PRIu8 ", touch_position=%" PRIu8,
width, touch_position);
button = (touch_position / width) + 1; // Determine button region
if (button > model_idx)
button = model_idx; // Clamp to max button count
}
id(button_press_button) = button;
// Update counters
if (id(button_press_position) == position) {
id(click_counter)++;
} else {
id(click_counter) = 1;
id(button_press_position) = position;
}
// Update binary sensor
switch (button) {
Expand All @@ -197,13 +247,6 @@ script:
bs_button_4->publish_state(true);
break;
}
// Update counters
if (id(button_press_button) == button) {
id(click_counter)++;
} else {
id(click_counter) = 1;
id(button_press_button) = button;
}
- id: !extend touch_on_release
then:
Expand All @@ -221,7 +264,7 @@ script:
press_duration <= ${BUTTON_CLICK_MAX_LENGTH}) { // Short/normal click
button_click_event->execute(id(button_press_button), id(click_counter));
} else if (press_duration >= ${BUTTON_LONG_PRESS_DELAY} and press_duration <= ${BUTTON_PRESS_TIMEOUT}) {
button_action->execute(("bs_button_" + std::to_string(id(button_press_button))).c_str(), "long_click");
button_action->execute(id(button_press_button), "long_press", 1);
} else if (press_duration > ${BUTTON_PRESS_TIMEOUT}) { // Timeout or invalid
ESP_LOGW("core_hw_buttons", "Button press cancelled or timed out after ${BUTTON_PRESS_TIMEOUT} ms");
}
Expand Down
2 changes: 1 addition & 1 deletion ESPHome/TX-Ultimate-Easy-ESPHome_core_hw_relays.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ script:
}
// Toggle relay if corresponding button action is enabled
switch (button_id) {
switch (button) {
case ${BUTTON_1_ID}:
if (sl_button_1_action->state != "${RELAY_MODE_TEXT_NOT_USED}")
sw_relay_1->toggle();
Expand Down
Loading

0 comments on commit eb1a395

Please sign in to comment.