Refactor display logic for better readability and maintainability; update TV power control logic with override functionality
This commit is contained in:
		
							parent
							
								
									a07c1ad06e
								
							
						
					
					
						commit
						1623f3afdf
					
				| @ -38,12 +38,10 @@ output: | ||||
|     id: output_cold | ||||
|     pin: P26 | ||||
|     max_power: 70%  # Limit the cold white channel to 80% | ||||
|     min_power: 14% | ||||
|   - platform: libretiny_pwm | ||||
|     id: output_warm | ||||
|     pin: P24 | ||||
|     max_power: 70%  # Limit the warm white channel to 80% | ||||
|     min_power: 14% | ||||
| 
 | ||||
| light: | ||||
|   - platform: cwww | ||||
|  | ||||
							
								
								
									
										116
									
								
								display.yaml
									
									
									
									
									
								
							
							
						
						
									
										116
									
								
								display.yaml
									
									
									
									
									
								
							| @ -57,14 +57,16 @@ display: | ||||
|       int row_y = margin + 25; | ||||
|       int col_offset = margin + 25; | ||||
|       int icon_size = y_step * 0.75; | ||||
|       int col; | ||||
| 
 | ||||
|       // 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}; | ||||
| 
 | ||||
|       it.fill(Color(0, 0, 0));  // Clear screen | ||||
|       // Clear screen | ||||
|       it.fill(Color(0, 0, 0)); | ||||
| 
 | ||||
|       // Function to calculate row position | ||||
|       auto row = [&](int row_in) { | ||||
|       auto row_calc = [&](int row_in) { | ||||
|         if (row_in == 0) { | ||||
|           return margin; | ||||
|         } else { | ||||
| @ -74,12 +76,12 @@ display: | ||||
| 
 | ||||
|       // Function to handle all state conditions | ||||
|       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") { | ||||
|                 text = text + "°C"; | ||||
|             } else if (type == "humidity") { | ||||
| @ -89,61 +91,66 @@ display: | ||||
|          | ||||
|         if (text == "unavailable" || text == "unknown") { | ||||
|           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.print((center + icon_size / 3) + 1, y, id(my_font), Color(255, 255, 255), "!"); | ||||
|             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, row_calc(row_index), id(my_font), Color(255, 255, 255), "!"); | ||||
|           } | ||||
|         } 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") { | ||||
|           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") { | ||||
|           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") { | ||||
|           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 { | ||||
|           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) | ||||
|       handle_state(1, 0, "", "icon"); | ||||
|       handle_state(1, 1, id(bath_door_status).state, "door"); | ||||
|       handle_state(1, 2, id(hall_door_status).state, "door"); | ||||
|       handle_state(1, 3, "", "door"); | ||||
|       handle_state(1, 4, id(tomek_door_status).state, "door"); | ||||
|       handle_state(1, 5, id(mateusz_door_status).state, "door"); | ||||
|       // Room names (column 0) | ||||
|       col = 0; | ||||
|       handle_state(col,0, "", "icon"); | ||||
|       handle_state(col,1, "Bath", "name"); | ||||
|       handle_state(col,2, "Hall", "name"); | ||||
|       handle_state(col,3, "Kitch", "name");; | ||||
|       handle_state(col,4, "Tom", "name"); | ||||
|       handle_state(col,5, "Mate", "name"); | ||||
| 
 | ||||
|       // Motion status (third column) | ||||
|       handle_state(2, 0, "", "icon"); | ||||
|       handle_state(2, 1, id(bath_motion_status).state, "motion"); | ||||
|       handle_state(2, 2, id(hall_motion_status).state, "motion"); | ||||
|       handle_state(2, 3, id(kitchen_motion_status).state, "motion"); | ||||
|       handle_state(2, 4, "", "motion"); | ||||
|       handle_state(2, 5, "", "motion"); | ||||
|       // Door status (column 1) | ||||
|       col = 1; | ||||
|       handle_state(col, 0, "", "icon"); | ||||
|       handle_state(col, 1, id(bath_door_status).state, "door"); | ||||
|       handle_state(col, 2, id(hall_door_status).state, "door"); | ||||
|       handle_state(col, 3, "", "door"); | ||||
|       handle_state(col, 4, id(tomek_door_status).state, "door"); | ||||
|       handle_state(col, 5, id(mateusz_door_status).state, "door"); | ||||
| 
 | ||||
|       // Temperature (fourth column) | ||||
|       handle_state(3, 0, "", "icon"); | ||||
|       handle_state(3, 1, id(bath_temperature).state, "temperature"); | ||||
|       handle_state(3, 2, id(hall_temperature).state, "temperature"); | ||||
|       handle_state(3, 3, id(kitchen_temperature).state, "temperature"); | ||||
|       handle_state(3, 4, id(tomek_temperature).state, "temperature"); | ||||
|       handle_state(3, 5, id(mateusz_temperature).state, "temperature"); | ||||
|       // Motion status (column 2) | ||||
|       col = 2; | ||||
|       handle_state(col, 0, "", "icon"); | ||||
|       handle_state(col, 1, id(bath_motion_status).state, "motion"); | ||||
|       handle_state(col, 2, id(hall_motion_status).state, "motion"); | ||||
|       handle_state(col, 3, id(kitchen_motion_status).state, "motion"); | ||||
|       handle_state(col, 4, "", "motion"); | ||||
|       handle_state(col, 5, "", "motion"); | ||||
| 
 | ||||
|       // Humidity (fifth column) | ||||
|       handle_state(4, 0, "", "icon"); | ||||
|       handle_state(4, 1, id(bath_humidity).state, "humidity"); | ||||
|       handle_state(4, 2, id(hall_humidity).state, "humidity"); | ||||
|       handle_state(4, 3, id(kitchen_humidity).state, "humidity"); | ||||
|       handle_state(4, 4, id(tomek_humidity).state, "humidity"); | ||||
|       handle_state(4, 5, id(mateusz_humidity).state, "humidity"); | ||||
|       // Temperature (column 3) | ||||
|       col = 3; | ||||
|       handle_state(col, 0, "", "icon"); | ||||
|       handle_state(col, 1, id(bath_temperature).state, "temperature"); | ||||
|       handle_state(col, 2, id(hall_temperature).state, "temperature"); | ||||
|       handle_state(col, 3, id(kitchen_temperature).state, "temperature"); | ||||
|       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 | ||||
|       int progress_width = screen_width - (2 * margin) - 120; | ||||
| @ -151,16 +158,16 @@ display: | ||||
| 
 | ||||
|       // 3D Printer progress bar | ||||
|       if (id(octo_state).state == "Printing") { | ||||
|         it.print(margin, row(6), id(my_font), Color(255, 255, 255), "3D Printer:"); | ||||
|         it.filled_rectangle(margin + 90, row(6), progress, 20, Color(0, 255, 0)); | ||||
|         it.rectangle(margin + 90, row(6), progress_width, 20, Color(255, 255, 255)); | ||||
|         it.print(margin, row_calc(6), id(my_font), Color(255, 255, 255), "3D Printer:"); | ||||
|         it.filled_rectangle(margin + 90, row_calc(6), progress, 20, Color(0, 255, 0)); | ||||
|         it.rectangle(margin + 90, row_calc(6), progress_width, 20, Color(255, 255, 255)); | ||||
|       } | ||||
| 
 | ||||
|       // 3D Printer light status | ||||
|       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") { | ||||
|         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 | ||||
| @ -243,6 +250,7 @@ font: | ||||
|   - id: my_font | ||||
|     file: "fonts/Roboto.ttf" | ||||
|     size: 20 | ||||
| 
 | ||||
|   - id: icon_font | ||||
|     file: "fonts/JetBrains.ttf" | ||||
|     size: 20 | ||||
|  | ||||
| @ -43,8 +43,10 @@ binary_sensor: | ||||
|     device_class: power | ||||
|     on_state: | ||||
|       - lambda: |- | ||||
|           if (!id(power_switch).state == x) { | ||||
|             id(power_switch).publish_state(x); | ||||
|           if (id(override).state == false) { | ||||
|             if (id(power_switch).state != x) { | ||||
|               id(power_switch).publish_state(x); | ||||
|             } | ||||
|           } | ||||
| 
 | ||||
| text_sensor: | ||||
| @ -53,27 +55,38 @@ text_sensor: | ||||
|     id: tv_status | ||||
|     on_value: | ||||
|       - lambda: |- | ||||
|           if (x == "on") { | ||||
|             id(power_switch).turn_on(); | ||||
|           } else { | ||||
|             id(power_switch).turn_off(); | ||||
|           } | ||||
|             if (id(override).state == false) { | ||||
|               if (x == "on" && !id(power_switch).state) { | ||||
|                 id(power_switch).turn_on(); | ||||
|               } 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) | ||||
| switch: | ||||
|   - platform: template | ||||
|     name: "Switch" | ||||
|     id: power_switch | ||||
|     optimistic: true | ||||
|     restore_mode: RESTORE_DEFAULT_OFF | ||||
|     # turn_on_action: | ||||
|     #   - output.turn_off: tv_relay  # Set P23 LOW | ||||
|     #   - delay: 300ms | ||||
|     #   - output.turn_on: tv_relay   # Set P23 HIGH | ||||
|     optimistic: true | ||||
|     turn_off_action: | ||||
|       - output.turn_off: tv_relay  # Set P23 LOW | ||||
|       - delay: 1500ms | ||||
|       - output.turn_on: tv_relay   # Set P23 HIGH | ||||
|       - lambda: |- | ||||
|             if (millis() > 5000) {  // Check if 5 seconds have passed since startup | ||||
|               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) | ||||
| output: | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user