Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

Commit 8608b20

Browse files
committed
More changes for 1.12 (#552)
* More 1.12 updates * Fix log * Lint * Lint * Simplify * Update api_server.cpp * Update rotary_encoder.cpp * Fix global var array * Lint
1 parent bb2c69b commit 8608b20

13 files changed

+101
-65
lines changed

platformio.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ platform = [email protected]
3838
board = nodemcuv2
3939
framework = arduino
4040
lib_deps = ${common.lib_deps}
41-
build_flags = ${common.build_flags}
41+
build_flags = ${common.build_flags} -DESPHOME_LOG_LEVEL=6
4242
src_filter = ${common.src_filter} +<examples/livingroom8266/livingroom8266.cpp>
4343

4444
[env:custombmp180]

src/esphome/api/api_server.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ void APIServer::loop() {
9090
ESP_LOGE(TAG, "No client connected to API. Rebooting...");
9191
reboot("api");
9292
}
93+
this->status_set_warning();
9394
} else {
9495
this->last_connected_ = now;
96+
this->status_clear_warning();
9597
}
9698
}
9799
}
@@ -714,6 +716,12 @@ bool APIConnection::send_buffer(APIMessageType type) {
714716
}
715717

716718
void APIConnection::loop() {
719+
if (!network_is_connected()) {
720+
// when network is disconnected force disconnect immediately
721+
// don't wait for timeout
722+
this->fatal_error_();
723+
return;
724+
}
717725
if (this->client_->disconnected()) {
718726
// failsafe for disconnect logic
719727
this->on_disconnect_();

src/esphome/application.h

+8
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ class Application {
334334
template<typename T> GlobalVariableComponent<T> *make_global_variable();
335335

336336
template<typename T> GlobalVariableComponent<T> *make_global_variable(T initial_value);
337+
template<typename T>
338+
GlobalVariableComponent<T> *make_global_variable(
339+
std::array<typename std::remove_extent<T>::type, std::extent<T>::value> initial_value);
337340

338341
/* _ _ _______ ____ __ __ _______ _____ ____ _ _
339342
* /\ | | | |__ __/ __ \| \/ | /\|__ __|_ _/ __ \| \ | |
@@ -1233,6 +1236,11 @@ template<typename T> GlobalVariableComponent<T> *Application::make_global_variab
12331236
template<typename T> GlobalVariableComponent<T> *Application::make_global_variable(T initial_value) {
12341237
return this->register_component(new GlobalVariableComponent<T>(initial_value));
12351238
}
1239+
template<typename T>
1240+
GlobalVariableComponent<T> *Application::make_global_variable(
1241+
std::array<typename std::remove_extent<T>::type, std::extent<T>::value> initial_value) {
1242+
return this->register_component(new GlobalVariableComponent<T>(initial_value));
1243+
}
12361244

12371245
#ifdef USE_NEO_PIXEL_BUS_LIGHT
12381246
template<typename T_METHOD, typename T_COLOR_FEATURE>

src/esphome/automation.h

+2
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ template<typename T> class GlobalVariableComponent : public Component {
276276
public:
277277
explicit GlobalVariableComponent();
278278
explicit GlobalVariableComponent(T initial_value);
279+
explicit GlobalVariableComponent(
280+
std::array<typename std::remove_extent<T>::type, std::extent<T>::value> initial_value);
279281

280282
T &value();
281283

src/esphome/automation.tcc

+5
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ template<typename... Ts> ScriptStopAction<Ts...> *Script::make_stop_action() {
207207

208208
template<typename T> GlobalVariableComponent<T>::GlobalVariableComponent() {}
209209
template<typename T> GlobalVariableComponent<T>::GlobalVariableComponent(T initial_value) : value_(initial_value) {}
210+
template<typename T>
211+
GlobalVariableComponent<T>::GlobalVariableComponent(
212+
std::array<typename std::remove_extent<T>::type, std::extent<T>::value> initial_value) {
213+
memcpy(this->value_, initial_value.data(), sizeof(T));
214+
}
210215
template<typename T> T &GlobalVariableComponent<T>::value() { return this->value_; }
211216
template<typename T> void GlobalVariableComponent<T>::setup() {
212217
if (this->restore_value_) {

src/esphome/binary_sensor/binary_sensor.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,15 @@ void MultiClickTrigger::on_state_(bool state) {
194194
MultiClickTriggerEvent evt = this->timing_[*this->at_index_];
195195

196196
if (evt.max_length != 4294967294UL) {
197-
ESP_LOGV(TAG, "A i=%u min=%u max=%u", *this->at_index_, evt.min_length, evt.max_length);
197+
ESP_LOGV(TAG, "A i=%u min=%u max=%u", *this->at_index_, evt.min_length, evt.max_length); // NOLINT
198198
this->schedule_is_valid_(evt.min_length);
199199
this->schedule_is_not_valid_(evt.max_length);
200200
} else if (*this->at_index_ + 1 != this->timing_.size()) {
201-
ESP_LOGV(TAG, "B i=%u min=%u", *this->at_index_, evt.min_length);
201+
ESP_LOGV(TAG, "B i=%u min=%u", *this->at_index_, evt.min_length); // NOLINT
202202
this->cancel_timeout("is_not_valid");
203203
this->schedule_is_valid_(evt.min_length);
204204
} else {
205-
ESP_LOGV(TAG, "C i=%u min=%u", *this->at_index_, evt.min_length);
205+
ESP_LOGV(TAG, "C i=%u min=%u", *this->at_index_, evt.min_length); // NOLINT
206206
this->is_valid_ = false;
207207
this->cancel_timeout("is_not_valid");
208208
this->set_timeout("trigger", evt.min_length, [this]() { this->trigger_(); });

src/esphome/esppreferences.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ bool ESPPreferenceObject::load_() {
3232

3333
bool valid = this->data_[this->length_words_] == this->calculate_crc_();
3434

35-
ESP_LOGVV(TAG, "LOAD %u: valid=%s, 0=0x%08X 1=0x%08X (Type=%u, CRC=0x%08X)", this->rtc_offset_, YESNO(valid),
35+
ESP_LOGVV(TAG, "LOAD %zu: valid=%s, 0=0x%08X 1=0x%08X (Type=%u, CRC=0x%08X)", this->rtc_offset_, YESNO(valid),
3636
this->data_[0], this->data_[1], this->type_, this->calculate_crc_());
3737
return valid;
3838
}
@@ -45,7 +45,7 @@ bool ESPPreferenceObject::save_() {
4545
this->data_[this->length_words_] = this->calculate_crc_();
4646
if (!this->save_internal_())
4747
return false;
48-
ESP_LOGVV(TAG, "SAVE %u: 0=0x%08X 1=0x%08X (Type=%u, CRC=0x%08X)", this->rtc_offset_, this->data_[0], this->data_[1],
48+
ESP_LOGVV(TAG, "SAVE %zu: 0=0x%08X 1=0x%08X (Type=%u, CRC=0x%08X)", this->rtc_offset_, this->data_[0], this->data_[1],
4949
this->type_, this->calculate_crc_());
5050
return true;
5151
}
@@ -80,7 +80,6 @@ static inline bool esp_rtc_user_mem_write(uint32_t index, uint32_t value) {
8080
auto *ptr = &ESP_RTC_USER_MEM[index];
8181
#ifdef USE_ESP8266_PREFERENCES_FLASH
8282
if (*ptr != value) {
83-
ESP_LOGV(TAG, "RTC flash is dirty...");
8483
esp8266_preferences_modified = true;
8584
}
8685
#endif

src/esphome/light/light_color_values.cpp

+23-8
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,33 @@ void LightColorValues::normalize_color(const LightTraits &traits) {
8282
float max_value = fmaxf(this->get_red(), fmaxf(this->get_green(), this->get_blue()));
8383
if (traits.has_rgb_white_value()) {
8484
max_value = fmaxf(max_value, this->get_white());
85-
this->set_white(this->get_white() / max_value);
85+
if (max_value == 0.0f) {
86+
this->set_white(1.0f);
87+
} else {
88+
this->set_white(this->get_white() / max_value);
89+
}
90+
}
91+
if (max_value == 0.0f) {
92+
this->set_red(1.0f);
93+
this->set_green(1.0f);
94+
this->set_blue(1.0f);
95+
} else {
96+
this->set_red(this->get_red() / max_value);
97+
this->set_green(this->get_green() / max_value);
98+
this->set_blue(this->get_blue() / max_value);
8699
}
87-
this->set_red(this->get_red() / max_value);
88-
this->set_green(this->get_green() / max_value);
89-
this->set_blue(this->get_blue() / max_value);
90100
}
91101

92102
if (traits.has_brightness() && this->get_brightness() == 0.0f) {
93-
// 0% brightness means off
94-
this->set_state(false);
95-
// reset brightness to 100% (0% brightness is not allowed)
96-
this->set_brightness(1.0f);
103+
if (traits.has_rgb_white_value()) {
104+
// 0% brightness for RGBW[W] means no RGB channel, but white channel on.
105+
// do nothing
106+
} else {
107+
// 0% brightness means off
108+
this->set_state(false);
109+
// reset brightness to 100%
110+
this->set_brightness(1.0f);
111+
}
97112
}
98113
}
99114

src/esphome/light/light_state.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ void LightState::dump_json(JsonObject &root) {
124124
}
125125

126126
struct LightStateRTCState {
127-
bool state;
128-
float brightness;
129-
float red;
130-
float green;
131-
float blue;
132-
float white;
133-
float color_temp;
134-
uint32_t effect;
127+
bool state{false};
128+
float brightness{1.0f};
129+
float red{1.0f};
130+
float green{1.0f};
131+
float blue{1.0f};
132+
float white{1.0f};
133+
float color_temp{1.0f};
134+
uint32_t effect{0};
135135
};
136136

137137
void LightState::setup() {
@@ -143,9 +143,9 @@ void LightState::setup() {
143143
}
144144

145145
this->rtc_ = global_preferences.make_preference<LightStateRTCState>(this->get_object_id_hash());
146-
LightStateRTCState recovered;
147-
if (!this->rtc_.load(&recovered))
148-
return;
146+
LightStateRTCState recovered{};
147+
// Attempt to load from preferences, else fall back to default values from struct
148+
this->rtc_.load(&recovered);
149149

150150
auto call = this->make_call();
151151
call.set_state(recovered.state);
@@ -438,7 +438,7 @@ void LightState::StateCall::perform() const {
438438
ESP_LOGD(TAG, " Color Temperature: %.1f mireds", v.get_color_temperature());
439439
}
440440

441-
v.normalize_color(this->state_->output_->get_traits());
441+
v.normalize_color(traits);
442442

443443
if (traits.has_rgb() && (this->red_.has_value() || this->green_.has_value() || this->blue_.has_value())) {
444444
if (traits.has_rgb_white_value() && this->white_.has_value()) {

src/esphome/remote/remote_receiver.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ void RemoteReceiverComponent::loop() {
326326
// TODO: Handle case when loop() is not called quickly enough to catch idle
327327
return;
328328

329-
ESP_LOGVV(TAG, "read_at=%u write_at=%u dist=%u now=%u end=%u", this->buffer_read_at_, write_at, dist, now,
330-
this->buffer_[write_at]);
329+
ESP_LOGVV(TAG, "read_at=%u write_at=%u dist=%u now=%u end=%u", s.buffer_read_at, write_at, dist, now,
330+
s.buffer[write_at]);
331331

332332
// Skip first value, it's from the previous idle level
333333
s.buffer_read_at = (s.buffer_read_at + 1) % s.buffer_size;
@@ -345,8 +345,8 @@ void RemoteReceiverComponent::loop() {
345345
break;
346346
}
347347

348-
ESP_LOGVV(TAG, " i=%u buffer[%u]=%u - buffer[%u]=%u -> %d", i, this->buffer_read_at_,
349-
this->buffer_[this->buffer_read_at_], prev, this->buffer_[prev], multiplier * delta);
348+
ESP_LOGVV(TAG, " i=%u buffer[%u]=%u - buffer[%u]=%u -> %d", i, s.buffer_read_at, s.buffer[s.buffer_read_at], prev,
349+
s.buffer[prev], multiplier * delta);
350350
this->temp_.push_back(multiplier * delta);
351351
prev = s.buffer_read_at;
352352
s.buffer_read_at = (s.buffer_read_at + 1) % s.buffer_size;

src/esphome/sensor/cse7766.cpp

+23-31
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,8 @@ bool CSE7766Component::check_byte_() {
4343
uint8_t index = this->raw_data_index_;
4444
uint8_t byte = this->raw_data_[index];
4545
if (index == 0) {
46-
// Header - 0x55
47-
// These messages are verbose because the CSE7766
48-
// reports these when no load is attached. If the checksum doesn't match
49-
// though, then we should print a warning.
50-
if (byte == 0xAA) {
51-
ESP_LOGV(TAG, "CSE7766 not calibrated!");
52-
return false;
53-
}
54-
if ((byte & 0xF0) == 0xF0) {
55-
ESP_LOGV(TAG, "CSE7766 reports abnormal hardware: (0x%02X)", byte);
56-
if ((byte >> 3) & 1) {
57-
ESP_LOGV(TAG, " Voltage cycle exceeds range.");
58-
}
59-
if ((byte >> 2) & 1) {
60-
ESP_LOGV(TAG, " Current cycle exceeds range.");
61-
}
62-
if ((byte >> 1) & 1) {
63-
ESP_LOGV(TAG, " Power cycle exceeds range.");
64-
}
65-
if ((byte >> 0) & 1) {
66-
ESP_LOGV(TAG, " Coefficient storage area is abnormal.");
67-
}
68-
return false;
69-
}
70-
if (byte != 0x55) {
71-
ESP_LOGV(TAG, "Invalid Header Start from CSE7766: 0x%02X!", byte);
72-
return false;
73-
}
46+
// Header, usually 0x55, contains data about calibration etc.
47+
// this is validated in parse_data_
7448
return true;
7549
}
7650

@@ -103,6 +77,18 @@ void CSE7766Component::parse_data_() {
10377
ESP_LOGVV(TAG, " i=%u: 0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", i, BYTE_TO_BINARY(this->raw_data_[i]),
10478
this->raw_data_[i]);
10579
}
80+
81+
uint8_t header1 = this->raw_data_[0];
82+
if (header1 == 0xAA) {
83+
ESP_LOGW(TAG, "CSE7766 not calibrated!");
84+
return;
85+
}
86+
if ((header1 & 0xF0) == 0xF0 && ((header1 >> 0) & 1) == 1) {
87+
ESP_LOGW(TAG, "CSE7766 reports abnormal hardware: (0x%02X)", header1);
88+
ESP_LOGW(TAG, " Coefficient storage area is abnormal.");
89+
return;
90+
}
91+
10692
const uint32_t now = micros();
10793
const float d = (now - this->last_reading_) / 1000.0f;
10894
this->last_reading_ = now;
@@ -116,19 +102,25 @@ void CSE7766Component::parse_data_() {
116102

117103
uint8_t adj = this->raw_data_[20];
118104

119-
if ((adj >> 6 != 0) && voltage_cycle != 0) {
105+
if ((adj >> 6 != 0) && voltage_cycle != 0 &&
106+
// voltage cycle exceeds range
107+
((header1 >> 3) & 1) == 0) {
120108
// voltage cycle of serial port outputted is a complete cycle;
121109
float voltage = voltage_calib / float(voltage_cycle);
122110
this->voltage_acc_ += voltage * d;
123111
}
124112

125-
if ((adj >> 5 != 0) && power_cycle != 0 && current_cycle != 0) {
113+
if ((adj >> 5 != 0) && power_cycle != 0 && current_cycle != 0 &&
114+
// current cycle exceeds range
115+
((header1 >> 2) & 1) == 0) {
126116
// indicates current cycle of serial port outputted is a complete cycle;
127117
float current = current_calib / float(current_cycle);
128118
this->current_acc_ += current * d;
129119
}
130120

131-
if ((adj >> 4 != 0) && power_cycle != 0) {
121+
if ((adj >> 4 != 0) && power_cycle != 0 &&
122+
// power cycle exceeds range
123+
((header1 >> 1) & 1) == 0) {
132124
// power cycle of serial port outputted is a complete cycle;
133125
float active_power = power_calib / float(power_cycle);
134126
this->power_acc_ += active_power * d;

src/esphome/sensor/rotary_encoder.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void ICACHE_RAM_ATTR HOT RotaryEncoderSensorStore::gpio_intr(RotaryEncoderSensor
8888
if (arg->pin_b->digital_read())
8989
input_state |= STATE_PIN_B_HIGH;
9090

91-
uint8_t new_state = STATE_LOOKUP_TABLE[input_state];
91+
uint16_t new_state = STATE_LOOKUP_TABLE[input_state];
9292
if ((new_state & arg->resolution & STATE_HAS_INCREMENTED) != 0) {
9393
if (arg->counter < arg->max_value)
9494
arg->counter++;

src/esphome/wifi_component.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void WiFiComponent::start_connecting(const WiFiAP &ap, bool two) {
174174
} else {
175175
ESP_LOGV(TAG, " BSSID: Not Set");
176176
}
177-
ESP_LOGV(TAG, " Password: " LOG_SECRET("'%s'"), ap.get_password());
177+
ESP_LOGV(TAG, " Password: " LOG_SECRET("'%s'"), ap.get_password().c_str());
178178
if (ap.get_channel().has_value()) {
179179
ESP_LOGV(TAG, " Channel: %u", *ap.get_channel());
180180
} else {
@@ -779,6 +779,12 @@ const char *get_disconnect_reason_str(uint8_t reason) {
779779
}
780780

781781
void WiFiComponent::wifi_event_callback(System_Event_t *event) {
782+
#ifdef ESPHOME_LOG_HAS_VERBOSE
783+
// TODO: this callback is called while in cont context, so delay will fail
784+
// We need to defer the log messages until we're out of this context
785+
// only affects verbose log level
786+
// reproducible by enabling verbose log level and letting the ESP disconnect and
787+
// then reconnect to WiFi.
782788
switch (event->event) {
783789
case EVENT_STAMODE_CONNECTED: {
784790
auto it = event->event_info.connected;
@@ -794,7 +800,7 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
794800
char buf[33];
795801
memcpy(buf, it.ssid, it.ssid_len);
796802
buf[it.ssid_len] = '\0';
797-
ESP_LOGW(TAG, "Event: Disconnected ssid='%s' bssid=%s reason='%s'", buf, format_mac_addr(it.bssid).c_str(),
803+
ESP_LOGV(TAG, "Event: Disconnected ssid='%s' bssid=%s reason='%s'", buf, format_mac_addr(it.bssid).c_str(),
798804
get_disconnect_reason_str(it.reason));
799805
break;
800806
}
@@ -844,6 +850,7 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
844850
default:
845851
break;
846852
}
853+
#endif
847854

848855
if (event->event == EVENT_STAMODE_DISCONNECTED) {
849856
global_wifi_component->error_from_callback_ = true;

0 commit comments

Comments
 (0)