-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnspanel_esphome_datetime.yaml
More file actions
207 lines (180 loc) · 8.36 KB
/
nspanel_esphome_datetime.yaml
File metadata and controls
207 lines (180 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#####################################################################################################
##### NSPanel Easy - https://github.com/edwardtfn/NSPanel-Easy #####
#####################################################################################################
##### ESPHOME CORE - DATE & TIME #####
##### PLEASE only make changes if it is necessary and also the required knowledge is available. #####
##### For normal use with the Blueprint, no changes are necessary. #####
#####################################################################################################
---
substitutions:
##############################
## Change only in your ##
## local yaml substitutions ##
##############################
TAG_DATETIME: nspanel.datetime
DATE_FORMAT_DEFAULT: "%A, %d.%m"
TIME_FORMAT_DEFAULT: "%H:%M"
esphome:
platformio_options:
build_flags:
- -D NSPANEL_EASY_DATETIME
globals:
- id: mui_date_format
type: std::string
restore_value: true
max_restore_data_length: 30
initial_value: '"${DATE_FORMAT_DEFAULT}"'
- id: mui_time_format
type: std::string
restore_value: true
max_restore_data_length: 15
initial_value: '"${TIME_FORMAT_DEFAULT}"'
script:
- id: !extend action_component_text
then:
- lambda: |-
if (page != "mem") return;
if (component == "date_format") {
id(mui_date_format) = txt;
return;
}
if (component == "time_format") {
id(mui_time_format) = txt;
return;
}
- id: new_minute
mode: restart
then:
- lambda: |-
refresh_datetime->execute();
refresh_hardware_buttons_bars->execute(3);
refresh_wifi_icon->execute();
- id: !extend page_home
then:
- script.execute: refresh_datetime
- id: refresh_datetime
mode: restart
then:
- lambda: |-
if (system_flags.tft_upload_active) return; // Do not refresh if uploading TFT
// Time
ESPTime now = id(time_provider).now();
if (!now.is_valid()) {
ESP_LOGW("${TAG_DATETIME}", "refresh_datetime: time provider not yet synchronized");
return;
}
std::string time_format_str = id(mui_time_format);
// Resolve %-H (no-padding 24h hour)
nspanel_easy::replace_all(time_format_str, "%-H", to_string(static_cast<int>(now.hour)).c_str());
// Resolve %-I (no-padding 12h hour)
const int hour12 = (now.hour == 0) ? 12 : (now.hour > 12) ? (now.hour - 12) : now.hour;
nspanel_easy::replace_all(time_format_str, "%-I", to_string(hour12).c_str());
const char* const meridiem_text = (now.hour < 12) ? "${LOCALIZATION[LANG].meridiem.am}"
: "${LOCALIZATION[LANG].meridiem.pm}";
const bool has_meridiem = (time_format_str.find("%p") != std::string::npos);
if (id(screensaver_display_time) and current_page_id == get_page_id("screensaver")) {
std::string time_format_str_sleep = time_format_str;
nspanel_easy::replace_all(time_format_str_sleep, "%p", meridiem_text);
disp1->set_component_text("text", now.strftime(time_format_str_sleep).c_str());
disp1->show_component("text");
}
// Resolve %p for home page — strip from time string, set meridiem component separately
// Trim both ends after replacement to avoid leading/trailing whitespace
nspanel_easy::replace_all(time_format_str, "%p", "");
while (!time_format_str.empty() && time_format_str.front() == ' ')
time_format_str.erase(time_format_str.begin());
while (!time_format_str.empty() && time_format_str.back() == ' ')
time_format_str.pop_back();
disp1->set_component_text("home.meridiem", has_meridiem ? meridiem_text : " ");
disp1->set_component_text("home.time", now.strftime(time_format_str).c_str());
// Date
render_date->execute("home.date", static_cast<uint32_t>(now.timestamp));
- id: render_date
mode: restart
parameters:
component: string
timestamp: uint32_t
then:
- lambda: |-
static const char* const weekdays[7] = {
"${LOCALIZATION[LANG].weekdays.mon}",
"${LOCALIZATION[LANG].weekdays.tue}",
"${LOCALIZATION[LANG].weekdays.wed}",
"${LOCALIZATION[LANG].weekdays.thu}",
"${LOCALIZATION[LANG].weekdays.fri}",
"${LOCALIZATION[LANG].weekdays.sat}",
"${LOCALIZATION[LANG].weekdays.sun}"
};
static const char* const weekdays_short[7] = {
"${LOCALIZATION[LANG].weekdays_short.mon}",
"${LOCALIZATION[LANG].weekdays_short.tue}",
"${LOCALIZATION[LANG].weekdays_short.wed}",
"${LOCALIZATION[LANG].weekdays_short.thu}",
"${LOCALIZATION[LANG].weekdays_short.fri}",
"${LOCALIZATION[LANG].weekdays_short.sat}",
"${LOCALIZATION[LANG].weekdays_short.sun}"
};
static const char* const months[12] = {
"${LOCALIZATION[LANG].months.jan}", "${LOCALIZATION[LANG].months.feb}",
"${LOCALIZATION[LANG].months.mar}", "${LOCALIZATION[LANG].months.apr}",
"${LOCALIZATION[LANG].months.may}", "${LOCALIZATION[LANG].months.jun}",
"${LOCALIZATION[LANG].months.jul}", "${LOCALIZATION[LANG].months.aug}",
"${LOCALIZATION[LANG].months.sep}", "${LOCALIZATION[LANG].months.oct}",
"${LOCALIZATION[LANG].months.nov}", "${LOCALIZATION[LANG].months.dec}"
};
static const char* const months_short[12] = {
"${LOCALIZATION[LANG].months_short.jan}", "${LOCALIZATION[LANG].months_short.feb}",
"${LOCALIZATION[LANG].months_short.mar}", "${LOCALIZATION[LANG].months_short.apr}",
"${LOCALIZATION[LANG].months_short.may}", "${LOCALIZATION[LANG].months_short.jun}",
"${LOCALIZATION[LANG].months_short.jul}", "${LOCALIZATION[LANG].months_short.aug}",
"${LOCALIZATION[LANG].months_short.sep}", "${LOCALIZATION[LANG].months_short.oct}",
"${LOCALIZATION[LANG].months_short.nov}", "${LOCALIZATION[LANG].months_short.dec}"
};
if (component.empty()) return;
ESPTime dt = id(time_provider).now();
if (timestamp > 0)
dt = ESPTime::from_epoch_local(timestamp);
if (!dt.is_valid()) {
if (timestamp > 0) {
ESP_LOGW("${TAG_DATETIME}", "render_date(%s): invalid time from epoch %" PRIu32,
component.c_str(), timestamp);
} else {
ESP_LOGW("${TAG_DATETIME}", "render_date(%s): time provider not yet synchronized",
component.c_str());
}
return;
}
const int dow = (dt.day_of_week + 5) % 7;
const int mon = dt.month - 1;
std::string date_str = id(mui_date_format);
nspanel_easy::replace_all(date_str, "%A", weekdays[dow]);
nspanel_easy::replace_all(date_str, "%a", weekdays_short[dow]);
nspanel_easy::replace_all(date_str, "%B", months[mon]);
nspanel_easy::replace_all(date_str, "%b", months_short[mon]);
nspanel_easy::replace_all(date_str, "%-d", to_string(dt.day_of_month).c_str());
nspanel_easy::replace_all(date_str, "%-m", to_string(dt.month).c_str());
const std::string date_string = dt.strftime(date_str);
if (date_string == "ERROR") {
ESP_LOGW("${TAG_DATETIME}", "render_date(%s): strftime failed - format '%s' (raw: '%s')",
component.c_str(), date_str.c_str(), id(mui_date_format).c_str());
return;
}
disp1->set_component_text(component.c_str(), date_string.c_str());
- id: !extend stop_all
then:
- lambda: |-
refresh_datetime->stop();
render_date->stop();
time: # Keeps time display updated
- id: time_provider
platform: homeassistant
on_time:
- seconds: 0
then:
- script.execute: new_minute
on_time_sync:
then:
- lambda: |-
ESP_LOGD("${TAG_DATETIME}", "System clock synchronized");
refresh_datetime->execute();
...