Refactor touchscreen button handling: Simplify button logic and implement service calls for light and fan control
This commit is contained in:
@@ -119,81 +119,48 @@ touchscreen:
|
||||
y_min: 340
|
||||
y_max: 3860
|
||||
on_touch:
|
||||
# Wake backlight if off
|
||||
- lambda: |-
|
||||
id(last_interaction_ms) = millis();
|
||||
if (!id(backlight_is_on)) {
|
||||
auto call = id(backlight).turn_on();
|
||||
call.perform();
|
||||
id(backlight_is_on) = true;
|
||||
return;
|
||||
}
|
||||
ESP_LOGD("touch", "Touch at x=%d, y=%d", touch.x, touch.y);
|
||||
# Button 1: Toggle All (top-left)
|
||||
- if:
|
||||
condition:
|
||||
lambda: 'return touch.x >= 20 && touch.x <= 150 && touch.y >= 50 && touch.y <= 130;'
|
||||
then:
|
||||
- lambda: |-
|
||||
ESP_LOGD("touch", "Toggle All pressed");
|
||||
id(light1_state) = !id(light1_state);
|
||||
id(light2_state) = !id(light2_state);
|
||||
id(fan_state) = !id(fan_state);
|
||||
id(all_state) = id(light1_state) && id(light2_state) && id(fan_state);
|
||||
- homeassistant.service:
|
||||
service: switch.toggle
|
||||
data:
|
||||
entity_id: ${light_1_entity}
|
||||
- homeassistant.service:
|
||||
service: switch.toggle
|
||||
data:
|
||||
entity_id: ${light_2_entity}
|
||||
- homeassistant.service:
|
||||
service: switch.toggle
|
||||
data:
|
||||
entity_id: ${fan_entity}
|
||||
- component.update: my_display
|
||||
# Button 2: Polly's Light (top-right)
|
||||
- if:
|
||||
condition:
|
||||
lambda: 'return touch.x >= 170 && touch.x <= 300 && touch.y >= 50 && touch.y <= 130;'
|
||||
then:
|
||||
- lambda: |-
|
||||
ESP_LOGD("touch", "Polly's Light pressed");
|
||||
id(light1_state) = !id(light1_state);
|
||||
id(all_state) = id(light1_state) && id(light2_state) && id(fan_state);
|
||||
- homeassistant.service:
|
||||
service: switch.toggle
|
||||
data:
|
||||
entity_id: ${light_1_entity}
|
||||
- component.update: my_display
|
||||
# Button 3: Joshua's Light (bottom-left)
|
||||
- if:
|
||||
condition:
|
||||
lambda: 'return touch.x >= 20 && touch.x <= 150 && touch.y >= 150 && touch.y <= 230;'
|
||||
then:
|
||||
- lambda: |-
|
||||
ESP_LOGD("touch", "Joshua's Light pressed");
|
||||
id(light2_state) = !id(light2_state);
|
||||
id(all_state) = id(light1_state) && id(light2_state) && id(fan_state);
|
||||
- homeassistant.service:
|
||||
service: switch.toggle
|
||||
data:
|
||||
entity_id: ${light_2_entity}
|
||||
- component.update: my_display
|
||||
# Button 4: Ceiling Fan (bottom-right)
|
||||
- if:
|
||||
condition:
|
||||
lambda: 'return touch.x >= 170 && touch.x <= 300 && touch.y >= 150 && touch.y <= 230;'
|
||||
then:
|
||||
- lambda: |-
|
||||
ESP_LOGD("touch", "Ceiling Fan pressed");
|
||||
id(fan_state) = !id(fan_state);
|
||||
id(all_state) = id(light1_state) && id(light2_state) && id(fan_state);
|
||||
- homeassistant.service:
|
||||
service: switch.toggle
|
||||
data:
|
||||
entity_id: ${fan_entity}
|
||||
- component.update: my_display
|
||||
|
||||
// Button 1: Toggle All (top-left)
|
||||
if (touch.x >= 20 && touch.x <= 150 && touch.y >= 50 && touch.y <= 130) {
|
||||
ESP_LOGD("touch", "Toggle All pressed");
|
||||
id(light1_state) = !id(light1_state);
|
||||
id(light2_state) = !id(light2_state);
|
||||
id(fan_state) = !id(fan_state);
|
||||
id(all_state) = id(light1_state) && id(light2_state) && id(fan_state);
|
||||
id(svc_toggle_all).execute();
|
||||
|
||||
// Button 2: Polly's Light (top-right)
|
||||
} else if (touch.x >= 170 && touch.x <= 300 && touch.y >= 50 && touch.y <= 130) {
|
||||
ESP_LOGD("touch", "Polly's Light pressed");
|
||||
id(light1_state) = !id(light1_state);
|
||||
id(all_state) = id(light1_state) && id(light2_state) && id(fan_state);
|
||||
id(svc_toggle_polly).execute();
|
||||
|
||||
// Button 3: Joshua's Light (bottom-left)
|
||||
} else if (touch.x >= 20 && touch.x <= 150 && touch.y >= 150 && touch.y <= 230) {
|
||||
ESP_LOGD("touch", "Joshua's Light pressed");
|
||||
id(light2_state) = !id(light2_state);
|
||||
id(all_state) = id(light1_state) && id(light2_state) && id(fan_state);
|
||||
id(svc_toggle_joshua).execute();
|
||||
|
||||
// Button 4: Ceiling Fan (bottom-right)
|
||||
} else if (touch.x >= 170 && touch.x <= 300 && touch.y >= 150 && touch.y <= 230) {
|
||||
ESP_LOGD("touch", "Ceiling Fan pressed");
|
||||
id(fan_state) = !id(fan_state);
|
||||
id(all_state) = id(light1_state) && id(light2_state) && id(fan_state);
|
||||
id(svc_toggle_fan).execute();
|
||||
}
|
||||
|
||||
id(my_display).update();
|
||||
|
||||
# Backlight control
|
||||
output:
|
||||
@@ -217,6 +184,41 @@ font:
|
||||
id: button_font
|
||||
size: 20
|
||||
|
||||
# Scripts to call HA services (lambdas can't call homeassistant.service directly)
|
||||
script:
|
||||
- id: svc_toggle_all
|
||||
then:
|
||||
- homeassistant.service:
|
||||
service: switch.toggle
|
||||
data:
|
||||
entity_id: ${light_1_entity}
|
||||
- homeassistant.service:
|
||||
service: switch.toggle
|
||||
data:
|
||||
entity_id: ${light_2_entity}
|
||||
- homeassistant.service:
|
||||
service: switch.toggle
|
||||
data:
|
||||
entity_id: ${fan_entity}
|
||||
- id: svc_toggle_polly
|
||||
then:
|
||||
- homeassistant.service:
|
||||
service: switch.toggle
|
||||
data:
|
||||
entity_id: ${light_1_entity}
|
||||
- id: svc_toggle_joshua
|
||||
then:
|
||||
- homeassistant.service:
|
||||
service: switch.toggle
|
||||
data:
|
||||
entity_id: ${light_2_entity}
|
||||
- id: svc_toggle_fan
|
||||
then:
|
||||
- homeassistant.service:
|
||||
service: switch.toggle
|
||||
data:
|
||||
entity_id: ${fan_entity}
|
||||
|
||||
# Global state tracking
|
||||
globals:
|
||||
- id: all_state
|
||||
|
||||
Reference in New Issue
Block a user