-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
energy_tariffs.h
70 lines (48 loc) · 1.81 KB
/
energy_tariffs.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
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
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/preferences.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/number/number.h"
#include "esphome/components/time/real_time_clock.h"
#include "energy_tariff.h"
namespace esphome {
namespace energy_tariffs {
using sensor::Sensor;
class EnergyTariffs : public Component {
public:
class TimeOffsetNumber : public number::Number, public Parented<EnergyTariffs> {
public:
void control(float value) override {
this->parent_->rtc_.save(&value);
this->publish_state(value);
}
};
virtual ~EnergyTariffs();
float get_setup_priority() const override { return setup_priority::DATA; }
void dump_config() override;
void setup() override;
void loop() override;
void set_time(time::RealTimeClock *time) { this->time_ = time; }
void set_total(Sensor *sensor) { this->total_ = sensor; }
void set_time_offset(number::Number *time_offset) { this->time_offset_ = time_offset; }
void add_tariff(EnergyTariff *s) { this->tariffs_.push_back(s); }
EnergyTariff *get_current_tariff() const { return this->current_tariff_; }
void add_on_tariff_callback(std::function<void(Sensor *)> &&callback);
void add_on_before_tariff_callback(std::function<void()> &&callback);
protected:
ESPPreferenceObject rtc_;
time::RealTimeClock *time_{};
// input sensors
Sensor *total_{};
number::Number *time_offset_{};
// exposed sensors
std::vector<EnergyTariff *> tariffs_;
EnergyTariff *current_tariff_{};
float last_total_{NAN};
CallbackManager<void(Sensor *)> *tariff_callback_{};
CallbackManager<void()> *before_tariff_callback_{};
void process_(float total);
EnergyTariff *get_tariff_(const ESPTime &time) const;
}; // class EnergyTariffs
} // namespace energy_tariffs
} // namespace esphome