Skip to content

Commit a50f1db

Browse files
authored
Merge pull request #191 from librick/fix-rotation-infinite-recursion
Fix crash when all page switches are disabled
2 parents 506df08 + bb30742 commit a50f1db

3 files changed

Lines changed: 65 additions & 22 deletions

File tree

configuration.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Pairing ESPHome with Home Assistant opens a multitude of opportunities to create
8181
8282
## Disable Display and LED based on time
8383
84-
Enabling the "Blank Page" switch will disable output on the display. It is not necessary to turn off any existing pages, and disabling all pages will result in the device rebooting
84+
Enabling the "Blank Page" switch will disable output on the display. It is not necessary to turn off any existing pages.
8585
8686
1. In Home Assistant, navigate to Settings>Automations and scenes
8787
2. Click "Create Automation" button, then "Create new automation"
@@ -90,9 +90,8 @@ Enabling the "Blank Page" switch will disable output on the display. It is not
9090
2. Set to your desired time to turn off the display and/or LED
9191
2. Then do
9292
1. Add Action>Device>Select your AirGradient from the ESPHome Integration
93-
2. In the Action field, select "Turn on `<Your AirGradient Name> Display Blank Page`"
94-
1. For the base config with only the single page display package, this will set the display to show an empty page
95-
2. If using the multi_page package, may need to add additional actions to turn off the other enabled pages
93+
2. In the Action field, select "Turn on `<Your AirGradient Name> Display Blank Page`" (if your display package provides this switch)
94+
1. This will set the display to show an empty page
9695
3. Repeat action for "Turn Off `<Your AirGradient Name> LED Strip`" (If applicable)
9796
3. Click the Save button and give it a name, such as "AirGradient Night Mode"
9897
3. Repeat with a new Automation, with the actions reversed (Turn off Display Blank Page and turn on LED Strip), at the desired time with a name such as "AirGradient Night Mode Off"

packages/display_sh1106_multi_page.yaml

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ font:
1818
glyphs: '!"%()+=,-_.:°0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz/µ³'
1919

2020

21+
binary_sensor:
22+
# True if and only if at least one non-blank page switch is on.
23+
# Any new page switches should be added here
24+
- platform: template
25+
id: any_non_blank_page_enabled
26+
internal: True
27+
lambda: |-
28+
return id(display_ag_default_page).state
29+
|| id(display_boot_page).state
30+
|| id(display_summary_pages).state
31+
|| id(display_huge_no_units_page).state
32+
|| id(display_air_quality_page).state
33+
|| id(display_air_temp_page).state
34+
|| id(display_voc_page).state
35+
|| id(display_combo_page).state;
36+
37+
2138
display:
2239
- platform: ssd1306_i2c
2340
# https://esphome.io/components/display/ssd1306.html
@@ -202,11 +219,12 @@ display:
202219
- display.page.show_next: oled_display
203220
- component.update: oled_display
204221
- to: blank
205-
# Skip blank page unless it is turned on and the interval: will only display it
206222
then:
207223
- if:
224+
# Only change pages if a page switch for a non-blank page is actually enabled;
225+
# acts as a base case to prevent recursive page change loop
208226
condition:
209-
switch.is_off: display_blank_page
227+
binary_sensor.is_on: any_non_blank_page_enabled
210228
then:
211229
- display.page.show_next: oled_display
212230
- component.update: oled_display
@@ -227,14 +245,14 @@ interval:
227245
then:
228246
- if:
229247
condition:
230-
# If the blank page switch is on, only display the blank page, otherwise, rotate to next page
231-
switch.is_on: display_blank_page
248+
and:
249+
- switch.is_off: display_blank_page
250+
- binary_sensor.is_on: any_non_blank_page_enabled
232251
then:
233-
- display.page.show: blank
252+
- display.page.show_next: oled_display
234253
- component.update: oled_display
235254
else:
236-
# Change page on display
237-
- display.page.show_next: oled_display
255+
- display.page.show: blank
238256
- component.update: oled_display
239257

240258

@@ -322,8 +340,15 @@ switch:
322340
- display.page.show: blank
323341
- component.update: oled_display
324342
on_turn_off:
325-
- display.page.show_next: oled_display
326-
- component.update: oled_display
343+
- if:
344+
condition:
345+
binary_sensor.is_on: any_non_blank_page_enabled
346+
then:
347+
- display.page.show_next: oled_display
348+
- component.update: oled_display
349+
else:
350+
- display.page.show: blank
351+
- component.update: oled_display
327352

328353
number:
329354
- platform: template

packages/display_sh1106_single_page.yaml

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ font:
1414
bpp: 2
1515

1616

17+
binary_sensor:
18+
# True if and only if at least one non-blank page switch is on.
19+
# Any new page switches should be added here
20+
- platform: template
21+
id: any_non_blank_page_enabled
22+
internal: True
23+
lambda: |-
24+
return id(display_ag_default_page).state
25+
|| id(display_boot_page).state;
26+
27+
1728
display:
1829
- platform: ssd1306_i2c
1930
# https://esphome.io/components/display/ssd1306.html
@@ -73,11 +84,12 @@ display:
7384
- display.page.show_next: oled_display
7485
- component.update: oled_display
7586
- to: blank
76-
# Skip blank page unless it is turned on and the interval: will only display it
7787
then:
7888
- if:
89+
# Only change pages if a page switch for a non-blank page is actually enabled;
90+
# acts as a base case to prevent recursive page change loop
7991
condition:
80-
switch.is_off: display_blank_page
92+
binary_sensor.is_on: any_non_blank_page_enabled
8193
then:
8294
- display.page.show_next: oled_display
8395
- component.update: oled_display
@@ -98,14 +110,14 @@ interval:
98110
then:
99111
- if:
100112
condition:
101-
# If the blank page switch is on, only display the blank page, otherwise, rotate to next page
102-
switch.is_on: display_blank_page
113+
and:
114+
- switch.is_off: display_blank_page
115+
- binary_sensor.is_on: any_non_blank_page_enabled
103116
then:
104-
- display.page.show: blank
117+
- display.page.show_next: oled_display
105118
- component.update: oled_display
106119
else:
107-
# Change page on display
108-
- display.page.show_next: oled_display
120+
- display.page.show: blank
109121
- component.update: oled_display
110122

111123

@@ -145,8 +157,15 @@ switch:
145157
- display.page.show: blank
146158
- component.update: oled_display
147159
on_turn_off:
148-
- display.page.show_next: oled_display
149-
- component.update: oled_display
160+
- if:
161+
condition:
162+
binary_sensor.is_on: any_non_blank_page_enabled
163+
then:
164+
- display.page.show_next: oled_display
165+
- component.update: oled_display
166+
else:
167+
- display.page.show: blank
168+
- component.update: oled_display
150169

151170
number:
152171
- platform: template

0 commit comments

Comments
 (0)