-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
automation.h
31 lines (26 loc) · 876 Bytes
/
automation.h
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
#pragma once
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/preferences.h"
namespace esphome {
namespace onetime_init {
class OnetimeInitTrigger : public Trigger<>, public Component {
public:
explicit OnetimeInitTrigger(float setup_priority) : setup_priority_(setup_priority) {}
void setup() override {
ESPPreferenceObject pref = global_preferences->make_preference<uint8_t>(fnv1_hash("onetime_init"), true);
uint8_t prev_state{};
if (!pref.load(&prev_state) || prev_state == 0) {
this->trigger();
prev_state = 1;
pref.save(&prev_state);
global_preferences->sync();
}
}
float get_setup_priority() const override { return this->setup_priority_; }
protected:
float setup_priority_;
};
} // namespace onetime_init
} // namespace esphome