ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
hub.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
6#include "esphome/core/hal.h"
7#include "esphome/core/log.h"
8
9#include "opentherm.h"
10
11#ifdef OPENTHERM_USE_SENSOR
13#endif
14
15#ifdef OPENTHERM_USE_BINARY_SENSOR
17#endif
18
19#ifdef OPENTHERM_USE_SWITCH
21#endif
22
23#ifdef OPENTHERM_USE_OUTPUT
25#endif
26
27#ifdef OPENTHERM_USE_NUMBER
29#endif
30
31#include <functional>
32#include <memory>
33#include <unordered_map>
34#include <unordered_set>
35
36#include "opentherm_macros.h"
37
38namespace esphome::opentherm {
39
40static const uint8_t REPEATING_MESSAGE_ORDER = 255;
41static const uint8_t INITIAL_UNORDERED_MESSAGE_ORDER = 254;
42
43// OpenTherm component for ESPHome
44class OpenthermHub : public Component {
45 protected:
46 // Communication pins for the OpenTherm interface
48 // The OpenTherm interface
49 std::unique_ptr<OpenTherm> opentherm_;
50
51 OPENTHERM_SENSOR_LIST(OPENTHERM_DECLARE_SENSOR, )
52
53 OPENTHERM_BINARY_SENSOR_LIST(OPENTHERM_DECLARE_BINARY_SENSOR, )
54
55 OPENTHERM_SWITCH_LIST(OPENTHERM_DECLARE_SWITCH, )
56
57 OPENTHERM_NUMBER_LIST(OPENTHERM_DECLARE_NUMBER, )
58
59 OPENTHERM_OUTPUT_LIST(OPENTHERM_DECLARE_OUTPUT, )
60
61 OPENTHERM_INPUT_SENSOR_LIST(OPENTHERM_DECLARE_INPUT_SENSOR, )
62
63 OPENTHERM_SETTING_LIST(OPENTHERM_DECLARE_SETTING, )
64
65 bool sending_initial_ = true;
66 std::unordered_map<MessageId, uint8_t> configured_messages_;
67 std::vector<MessageId> messages_;
68 std::vector<MessageId>::const_iterator message_iterator_;
69
74
75 // Synchronous communication mode prevents other components from disabling interrupts while
76 // we are talking to the boiler. Enable if you experience random intermittent invalid response errors.
77 // Very likely to happen while using Dallas temperature sensors.
78 bool sync_mode_ = false;
79
82
83 // Create OpenTherm messages based on the message id
84 OpenthermData build_request_(MessageId request_id) const;
89 void stop_opentherm_();
91 void read_response_();
92 void check_timings_(uint32_t cur_time);
93 bool should_skip_loop_(uint32_t cur_time) const;
94 void sync_loop_();
95
96 void write_initial_messages_(std::vector<MessageId> &target);
97 void write_repeating_messages_(std::vector<MessageId> &target);
98
99 template<typename F> bool spin_wait_(uint32_t timeout, F func) {
100 auto start_time = millis();
101 while (func()) {
102 yield();
103 auto cur_time = millis();
104 if (cur_time - start_time >= timeout) {
105 return false;
106 }
107 }
108 return true;
109 }
110
111 public:
112 // Constructor with references to the global interrupt handlers
113 OpenthermHub();
114
115 // Handle responses from the OpenTherm interface
117
118 // Setters for the input and output OpenTherm interface pins
119 void set_in_pin(InternalGPIOPin *in_pin) { this->in_pin_ = in_pin; }
120 void set_out_pin(InternalGPIOPin *out_pin) { this->out_pin_ = out_pin; }
121
122 OPENTHERM_SENSOR_LIST(OPENTHERM_SET_SENSOR, )
123
124 OPENTHERM_BINARY_SENSOR_LIST(OPENTHERM_SET_BINARY_SENSOR, )
125
126 OPENTHERM_SWITCH_LIST(OPENTHERM_SET_SWITCH, )
127
128 OPENTHERM_NUMBER_LIST(OPENTHERM_SET_NUMBER, )
129
130 OPENTHERM_OUTPUT_LIST(OPENTHERM_SET_OUTPUT, )
131
132 OPENTHERM_INPUT_SENSOR_LIST(OPENTHERM_SET_INPUT_SENSOR, )
133
134 OPENTHERM_SETTING_LIST(OPENTHERM_SET_SETTING, )
135
136 // Add a request to the vector of initial requests
137 void add_initial_message(MessageId message_id) {
138 this->configured_messages_[message_id] = INITIAL_UNORDERED_MESSAGE_ORDER;
139 }
140 void add_initial_message(MessageId message_id, uint8_t order) { this->configured_messages_[message_id] = order; }
141 // Add a request to the set of repeating requests. Note that a large number of repeating
142 // requests will slow down communication with the boiler. Each request may take up to 1 second,
143 // so with all sensors enabled, it may take about half a minute before a change in setpoint
144 // will be processed.
145 void add_repeating_message(MessageId message_id) { this->configured_messages_[message_id] = REPEATING_MESSAGE_ORDER; }
146
147 // There are seven status variables, which can either be set as a simple variable,
148 // or using a switch. ch_enable and dhw_enable default to true, the others to false.
149 bool ch_enable = true, dhw_enable = true, cooling_enable = false, otc_active = false, ch2_active = false,
151
152 // Setters for the status variables
153 void set_ch_enable(bool value) { this->ch_enable = value; }
154 void set_dhw_enable(bool value) { this->dhw_enable = value; }
155 void set_cooling_enable(bool value) { this->cooling_enable = value; }
156 void set_otc_active(bool value) { this->otc_active = value; }
157 void set_ch2_active(bool value) { this->ch2_active = value; }
158 void set_summer_mode_active(bool value) { this->summer_mode_active = value; }
159 void set_dhw_block(bool value) { this->dhw_block = value; }
160 void set_sync_mode(bool sync_mode) { this->sync_mode_ = sync_mode; }
161
162 template<typename F> void add_on_before_send_callback(F &&callback) {
163 this->before_send_callback_.add(std::forward<F>(callback));
164 }
165 template<typename F> void add_on_before_process_response_callback(F &&callback) {
166 this->before_process_response_callback_.add(std::forward<F>(callback));
167 }
168
169 float get_setup_priority() const override { return setup_priority::HARDWARE; }
170
171 void setup() override;
172 void on_shutdown() override;
173 void loop() override;
174 void dump_config() override;
175};
176
177} // namespace esphome::opentherm
BedjetMode mode
BedJet operating mode.
void set_cooling_enable(bool value)
Definition hub.h:155
void add_on_before_send_callback(F &&callback)
Definition hub.h:162
void set_summer_mode_active(bool value)
Definition hub.h:158
OPENTHERM_SENSOR_LIST(OPENTHERM_DECLARE_SENSOR,) OPENTHERM_BINARY_SENSOR_LIST(OPENTHERM_DECLARE_BINARY_SENSOR
void check_timings_(uint32_t cur_time)
Definition hub.cpp:310
void dump_config() override
Definition hub.cpp:391
void on_shutdown() override
Definition hub.cpp:162
void set_in_pin(InternalGPIOPin *in_pin)
Definition hub.h:119
std::vector< MessageId > messages_
Definition hub.h:67
OpenthermData build_request_(MessageId request_id) const
Definition hub.cpp:62
void set_sync_mode(bool sync_mode)
Definition hub.h:160
OperationMode last_mode_
Definition hub.h:72
void add_initial_message(MessageId message_id, uint8_t order)
Definition hub.h:140
void set_out_pin(InternalGPIOPin *out_pin)
Definition hub.h:120
void add_repeating_message(MessageId message_id)
Definition hub.h:145
void add_on_before_process_response_callback(F &&callback)
Definition hub.h:165
OPENTHERM_SENSOR_LIST(OPENTHERM_SET_SENSOR,) OPENTHERM_BINARY_SENSOR_LIST(OPENTHERM_SET_BINARY_SENSOR
OPENTHERM_SETTING_LIST(OPENTHERM_DECLARE_SETTING,) bool sending_initial_
void set_dhw_enable(bool value)
Definition hub.h:154
InternalGPIOPin * out_pin_
Definition hub.h:47
std::unordered_map< MessageId, uint8_t > configured_messages_
Definition hub.h:66
void set_dhw_block(bool value)
Definition hub.h:159
void process_response(OpenthermData &data)
Definition hub.cpp:131
OPENTHERM_SWITCH_LIST(OPENTHERM_DECLARE_SWITCH,) OPENTHERM_NUMBER_LIST(OPENTHERM_DECLARE_NUMBER
InternalGPIOPin * in_pin_
Definition hub.h:47
void set_otc_active(bool value)
Definition hub.h:156
CallbackManager< void(OpenthermData &)> before_send_callback_
Definition hub.h:80
bool handle_error_(OperationMode mode)
Definition hub.cpp:227
CallbackManager< void(OpenthermData &)> before_process_response_callback_
Definition hub.h:81
bool spin_wait_(uint32_t timeout, F func)
Definition hub.h:99
std::vector< MessageId >::const_iterator message_iterator_
Definition hub.h:68
uint32_t last_conversation_start_
Definition hub.h:70
float get_setup_priority() const override
Definition hub.h:169
void write_initial_messages_(std::vector< MessageId > &target)
Definition hub.cpp:165
void write_repeating_messages_(std::vector< MessageId > &target)
Definition hub.cpp:180
OpenthermData last_request_
Definition hub.h:73
void set_ch2_active(bool value)
Definition hub.h:157
bool should_skip_loop_(uint32_t cur_time) const
Definition hub.cpp:319
OPENTHERM_OUTPUT_LIST(OPENTHERM_DECLARE_OUTPUT,) OPENTHERM_INPUT_SENSOR_LIST(OPENTHERM_DECLARE_INPUT_SENSOR
std::unique_ptr< OpenTherm > opentherm_
Definition hub.h:49
void set_ch_enable(bool value)
Definition hub.h:153
void yield(void)
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:41
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t
Structure to hold Opentherm data packet content.
Definition opentherm.h:183