Refactor display logic for better readability and maintainability; update TV power control logic with override functionality

This commit is contained in:
ZareMate 2025-02-04 06:45:09 +01:00
parent a07c1ad06e
commit 1623f3afdf
Signed by: zaremate
GPG Key ID: 369A0E45E03A81C3
3 changed files with 88 additions and 69 deletions

View File

@ -38,12 +38,10 @@ output:
id: output_cold id: output_cold
pin: P26 pin: P26
max_power: 70% # Limit the cold white channel to 80% max_power: 70% # Limit the cold white channel to 80%
min_power: 14%
- platform: libretiny_pwm - platform: libretiny_pwm
id: output_warm id: output_warm
pin: P24 pin: P24
max_power: 70% # Limit the warm white channel to 80% max_power: 70% # Limit the warm white channel to 80%
min_power: 14%
light: light:
- platform: cwww - platform: cwww

View File

@ -57,14 +57,16 @@ display:
int row_y = margin + 25; int row_y = margin + 25;
int col_offset = margin + 25; int col_offset = margin + 25;
int icon_size = y_step * 0.75; int icon_size = y_step * 0.75;
int col;
// Calculate column and row positions // Calculate column and row positions
int col_array[] = {margin, col_offset + y_step, col_offset + y_step * 2, col_offset + y_step * 3, col_offset + y_step * 5, col_offset + y_step * 7}; int col_array[] = {margin, col_offset + y_step, col_offset + y_step * 2, col_offset + y_step * 3, col_offset + y_step * 5, col_offset + y_step * 7};
it.fill(Color(0, 0, 0)); // Clear screen // Clear screen
it.fill(Color(0, 0, 0));
// Function to calculate row position // Function to calculate row position
auto row = [&](int row_in) { auto row_calc = [&](int row_in) {
if (row_in == 0) { if (row_in == 0) {
return margin; return margin;
} else { } else {
@ -74,12 +76,12 @@ display:
// Function to handle all state conditions // Function to handle all state conditions
auto handle_state = [&](int col_index, int row_index, std::string text, std::string type) { auto handle_state = [&](int col_index, int row_index, std::string text, std::string type) {
int x, y, center;
x = col_array[col_index];
y = row(row_index);
center = ((col_array[col_index] + col_array[col_index + 1]) / 2) - 10;
if (text != "on" && text != "off" && text != "unavailable" && text != "unknown" && text != "") { // Calculate center of column
int center = ((col_array[col_index] + col_array[col_index + 1]) / 2) - 10;
// Check if text is a number and add unit
if (text != "unavailable" && text != "unknown" && text != "") {
if (type == "temperature") { if (type == "temperature") {
text = text + "°C"; text = text + "°C";
} else if (type == "humidity") { } else if (type == "humidity") {
@ -89,61 +91,66 @@ display:
if (text == "unavailable" || text == "unknown") { if (text == "unavailable" || text == "unknown") {
if (id(triangle_visible)) { if (id(triangle_visible)) {
it.filled_triangle(center, y + icon_size, center + icon_size, y + icon_size, center + (icon_size / 2), y, Color(255, 0, 0)); it.filled_triangle(center, row_calc(row_index) + icon_size, center + icon_size, row_calc(row_index) + icon_size, center + (icon_size / 2), row_calc(row_index), Color(255, 0, 0));
it.print((center + icon_size / 3) + 1, y, id(my_font), Color(255, 255, 255), "!"); it.print((center + icon_size / 3) + 1, row_calc(row_index), id(my_font), Color(255, 255, 255), "!");
} }
} else if (text == "" && type == "door" || text == "" && type == "motion") { } else if (text == "" && type == "door" || text == "" && type == "motion") {
it.filled_rectangle(x, y, icon_size, icon_size, Color(128, 128, 128)); it.filled_rectangle(col_array[col_index], row_calc(row_index), icon_size, icon_size, Color(128, 128, 128));
} else if (text == "on") { } else if (text == "on") {
it.filled_rectangle(x, y, icon_size, icon_size, Color(0, 255, 0)); it.filled_rectangle(col_array[col_index], row_calc(row_index), icon_size, icon_size, Color(0, 255, 0));
} else if (text == "off") { } else if (text == "off") {
it.filled_rectangle(x, y, icon_size, icon_size, Color(255, 0, 0)); it.filled_rectangle(col_array[col_index], row_calc(row_index), icon_size, icon_size, Color(255, 0, 0));
} else if (type == "icon") { } else if (type == "icon") {
it.print(center, y, id(icon_font), Color(255, 255, 255), text.c_str()); it.print(center, row_calc(row_index), id(icon_font), Color(255, 255, 255), text.c_str());
} else { } else {
it.print(x, y, id(my_font), Color(255, 255, 255), text.c_str()); it.print(col_array[col_index], row_calc(row_index), id(my_font), Color(255, 255, 255), text.c_str());
} }
}; };
// Room names (first column)
handle_state(0,0, "󰚡", "icon");
handle_state(0,1, "Bath", "name");
handle_state(0,2, "Hall", "name");
handle_state(0,3, "Kitch", "name");;
handle_state(0,4, "Tom", "name");
handle_state(0,5, "Mate", "name");
// Door status (second column) // Room names (column 0)
handle_state(1, 0, "󰠚", "icon"); col = 0;
handle_state(1, 1, id(bath_door_status).state, "door"); handle_state(col,0, "󰚡", "icon");
handle_state(1, 2, id(hall_door_status).state, "door"); handle_state(col,1, "Bath", "name");
handle_state(1, 3, "", "door"); handle_state(col,2, "Hall", "name");
handle_state(1, 4, id(tomek_door_status).state, "door"); handle_state(col,3, "Kitch", "name");;
handle_state(1, 5, id(mateusz_door_status).state, "door"); handle_state(col,4, "Tom", "name");
handle_state(col,5, "Mate", "name");
// Motion status (third column) // Door status (column 1)
handle_state(2, 0, "󰶑", "icon"); col = 1;
handle_state(2, 1, id(bath_motion_status).state, "motion"); handle_state(col, 0, "󰠚", "icon");
handle_state(2, 2, id(hall_motion_status).state, "motion"); handle_state(col, 1, id(bath_door_status).state, "door");
handle_state(2, 3, id(kitchen_motion_status).state, "motion"); handle_state(col, 2, id(hall_door_status).state, "door");
handle_state(2, 4, "", "motion"); handle_state(col, 3, "", "door");
handle_state(2, 5, "", "motion"); handle_state(col, 4, id(tomek_door_status).state, "door");
handle_state(col, 5, id(mateusz_door_status).state, "door");
// Temperature (fourth column) // Motion status (column 2)
handle_state(3, 0, "󰔏", "icon"); col = 2;
handle_state(3, 1, id(bath_temperature).state, "temperature"); handle_state(col, 0, "󰶑", "icon");
handle_state(3, 2, id(hall_temperature).state, "temperature"); handle_state(col, 1, id(bath_motion_status).state, "motion");
handle_state(3, 3, id(kitchen_temperature).state, "temperature"); handle_state(col, 2, id(hall_motion_status).state, "motion");
handle_state(3, 4, id(tomek_temperature).state, "temperature"); handle_state(col, 3, id(kitchen_motion_status).state, "motion");
handle_state(3, 5, id(mateusz_temperature).state, "temperature"); handle_state(col, 4, "", "motion");
handle_state(col, 5, "", "motion");
// Humidity (fifth column) // Temperature (column 3)
handle_state(4, 0, "󰖌", "icon"); col = 3;
handle_state(4, 1, id(bath_humidity).state, "humidity"); handle_state(col, 0, "󰔏", "icon");
handle_state(4, 2, id(hall_humidity).state, "humidity"); handle_state(col, 1, id(bath_temperature).state, "temperature");
handle_state(4, 3, id(kitchen_humidity).state, "humidity"); handle_state(col, 2, id(hall_temperature).state, "temperature");
handle_state(4, 4, id(tomek_humidity).state, "humidity"); handle_state(col, 3, id(kitchen_temperature).state, "temperature");
handle_state(4, 5, id(mateusz_humidity).state, "humidity"); handle_state(col, 4, id(tomek_temperature).state, "temperature");
handle_state(col, 5, id(mateusz_temperature).state, "temperature");
// Humidity (column 4)
col = 4;
handle_state(col, 0, "󰖌", "icon");
handle_state(col, 1, id(bath_humidity).state, "humidity");
handle_state(col, 2, id(hall_humidity).state, "humidity");
handle_state(col, 3, id(kitchen_humidity).state, "humidity");
handle_state(col, 4, id(tomek_humidity).state, "humidity");
handle_state(col, 5, id(mateusz_humidity).state, "humidity");
// 3D Printer variables // 3D Printer variables
int progress_width = screen_width - (2 * margin) - 120; int progress_width = screen_width - (2 * margin) - 120;
@ -151,16 +158,16 @@ display:
// 3D Printer progress bar // 3D Printer progress bar
if (id(octo_state).state == "Printing") { if (id(octo_state).state == "Printing") {
it.print(margin, row(6), id(my_font), Color(255, 255, 255), "3D Printer:"); it.print(margin, row_calc(6), id(my_font), Color(255, 255, 255), "3D Printer:");
it.filled_rectangle(margin + 90, row(6), progress, 20, Color(0, 255, 0)); it.filled_rectangle(margin + 90, row_calc(6), progress, 20, Color(0, 255, 0));
it.rectangle(margin + 90, row(6), progress_width, 20, Color(255, 255, 255)); it.rectangle(margin + 90, row_calc(6), progress_width, 20, Color(255, 255, 255));
} }
// 3D Printer light status // 3D Printer light status
if (id(printer_light).state == "on" && id(octo_state).state == "Printing" ) { if (id(printer_light).state == "on" && id(octo_state).state == "Printing" ) {
it.print(margin + 90 + progress_width + 10, row(6), id(icon_font), Color(255, 255, 255), "󰖨"); it.print(margin + 90 + progress_width + 10, row_calc(6), id(icon_font), Color(255, 255, 255), "󰖨");
} else if (id(printer_light).state == "on") { } else if (id(printer_light).state == "on") {
it.print(margin, row(6), id(my_font), Color(255, 255, 255), "3D Printer Light On"); it.print(margin, row_calc(6), id(my_font), Color(255, 255, 255), "3D Printer Light On");
} }
# Home Assistant integration # Home Assistant integration
@ -243,6 +250,7 @@ font:
- id: my_font - id: my_font
file: "fonts/Roboto.ttf" file: "fonts/Roboto.ttf"
size: 20 size: 20
- id: icon_font - id: icon_font
file: "fonts/JetBrains.ttf" file: "fonts/JetBrains.ttf"
size: 20 size: 20

View File

@ -43,8 +43,10 @@ binary_sensor:
device_class: power device_class: power
on_state: on_state:
- lambda: |- - lambda: |-
if (!id(power_switch).state == x) { if (id(override).state == false) {
id(power_switch).publish_state(x); if (id(power_switch).state != x) {
id(power_switch).publish_state(x);
}
} }
text_sensor: text_sensor:
@ -53,27 +55,38 @@ text_sensor:
id: tv_status id: tv_status
on_value: on_value:
- lambda: |- - lambda: |-
if (x == "on") { if (id(override).state == false) {
id(power_switch).turn_on(); if (x == "on" && !id(power_switch).state) {
} else { id(power_switch).turn_on();
id(power_switch).turn_off(); } else if (x == "off" && id(power_switch).state) {
} id(power_switch).turn_off();
}
}
# Define the switch to control TV power (Only toggled via Home Assistant) # Define the switch to control TV power (Only toggled via Home Assistant)
switch: switch:
- platform: template - platform: template
name: "Switch" name: "Switch"
id: power_switch id: power_switch
optimistic: true
restore_mode: RESTORE_DEFAULT_OFF restore_mode: RESTORE_DEFAULT_OFF
# turn_on_action: # turn_on_action:
# - output.turn_off: tv_relay # Set P23 LOW # - output.turn_off: tv_relay # Set P23 LOW
# - delay: 300ms # - delay: 300ms
# - output.turn_on: tv_relay # Set P23 HIGH # - output.turn_on: tv_relay # Set P23 HIGH
optimistic: true
turn_off_action: turn_off_action:
- output.turn_off: tv_relay # Set P23 LOW - lambda: |-
- delay: 1500ms if (millis() > 5000) { // Check if 5 seconds have passed since startup
- output.turn_on: tv_relay # Set P23 HIGH id(tv_relay).turn_off(); // Set P23 LOW
delay(1500); // Delay for 1500ms
id(tv_relay).turn_on(); // Set P23 HIGH
}
- platform: template
name: "Override"
id: override
optimistic: true
restore_mode: RESTORE_DEFAULT_OFF
# Define GPIO23 as the output (normally pull-up) # Define GPIO23 as the output (normally pull-up)
output: output: