ESPHome 2025.5.0
Loading...
Searching...
No Matches
opentherm_macros.h
Go to the documentation of this file.
1#pragma once
2namespace esphome {
3namespace opentherm {
4
5// ===== hub.h macros =====
6
7// *_LIST macros will be generated in defines.h if at least one sensor from each platform is used.
8// These lists will look like this:
9// #define OPENTHERM_BINARY_SENSOR_LIST(F, sep) F(sensor_1) sep F(sensor_2)
10// These lists will be used in hub.h to define sensor fields (passing macros like OPENTHERM_DECLARE_SENSOR as F)
11// and setters (passing macros like OPENTHERM_SET_SENSOR as F) (see below)
12// In order for things not to break, we define empty lists here in case some platforms are not used in config.
13#ifndef OPENTHERM_SENSOR_LIST
14#define OPENTHERM_SENSOR_LIST(F, sep)
15#endif
16#ifndef OPENTHERM_BINARY_SENSOR_LIST
17#define OPENTHERM_BINARY_SENSOR_LIST(F, sep)
18#endif
19#ifndef OPENTHERM_SWITCH_LIST
20#define OPENTHERM_SWITCH_LIST(F, sep)
21#endif
22#ifndef OPENTHERM_NUMBER_LIST
23#define OPENTHERM_NUMBER_LIST(F, sep)
24#endif
25#ifndef OPENTHERM_OUTPUT_LIST
26#define OPENTHERM_OUTPUT_LIST(F, sep)
27#endif
28#ifndef OPENTHERM_INPUT_SENSOR_LIST
29#define OPENTHERM_INPUT_SENSOR_LIST(F, sep)
30#endif
31#ifndef OPENTHERM_SETTING_LIST
32#define OPENTHERM_SETTING_LIST(F, sep)
33#endif
34
35// Use macros to create fields for every entity specified in the ESPHome configuration
36#define OPENTHERM_DECLARE_SENSOR(entity) sensor::Sensor *entity;
37#define OPENTHERM_DECLARE_BINARY_SENSOR(entity) binary_sensor::BinarySensor *entity;
38#define OPENTHERM_DECLARE_SWITCH(entity) OpenthermSwitch *entity;
39#define OPENTHERM_DECLARE_NUMBER(entity) OpenthermNumber *entity;
40#define OPENTHERM_DECLARE_OUTPUT(entity) OpenthermOutput *entity;
41#define OPENTHERM_DECLARE_INPUT_SENSOR(entity) sensor::Sensor *entity;
42#define OPENTHERM_DECLARE_SETTING(type, entity, def) type entity = def;
43
44// Setter macros
45#define OPENTHERM_SET_SENSOR(entity) \
46 void set_##entity(sensor::Sensor *sensor) { this->entity = sensor; }
47
48#define OPENTHERM_SET_BINARY_SENSOR(entity) \
49 void set_##entity(binary_sensor::BinarySensor *binary_sensor) { this->entity = binary_sensor; }
50
51#define OPENTHERM_SET_SWITCH(entity) \
52 void set_##entity(OpenthermSwitch *sw) { this->entity = sw; }
53
54#define OPENTHERM_SET_NUMBER(entity) \
55 void set_##entity(OpenthermNumber *number) { this->entity = number; }
56
57#define OPENTHERM_SET_OUTPUT(entity) \
58 void set_##entity(OpenthermOutput *output) { this->entity = output; }
59
60#define OPENTHERM_SET_INPUT_SENSOR(entity) \
61 void set_##entity(sensor::Sensor *sensor) { this->entity = sensor; }
62
63#define OPENTHERM_SET_SETTING(type, entity, def) \
64 void set_##entity(type value) { this->entity = value; }
65
66// ===== hub.cpp macros =====
67
68// *_MESSAGE_HANDLERS are generated in defines.h and look like this:
69// OPENTHERM_NUMBER_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep) MESSAGE(COOLING_CONTROL)
70// ENTITY(cooling_control_number, f88) postscript msg_sep They contain placeholders for message part and entities parts,
71// since one message can contain multiple entities. MESSAGE part is substituted with OPENTHERM_MESSAGE_WRITE_MESSAGE,
72// OPENTHERM_MESSAGE_READ_MESSAGE or OPENTHERM_MESSAGE_RESPONSE_MESSAGE. ENTITY part is substituted with
73// OPENTHERM_MESSAGE_WRITE_ENTITY or OPENTHERM_MESSAGE_RESPONSE_ENTITY. OPENTHERM_IGNORE is used for sensor read
74// requests since no data needs to be sent or processed, just the data id.
75
76// In order for things not to break, we define empty lists here in case some platforms are not used in config.
77#ifndef OPENTHERM_SENSOR_MESSAGE_HANDLERS
78#define OPENTHERM_SENSOR_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
79#endif
80#ifndef OPENTHERM_BINARY_SENSOR_MESSAGE_HANDLERS
81#define OPENTHERM_BINARY_SENSOR_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
82#endif
83#ifndef OPENTHERM_SWITCH_MESSAGE_HANDLERS
84#define OPENTHERM_SWITCH_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
85#endif
86#ifndef OPENTHERM_NUMBER_MESSAGE_HANDLERS
87#define OPENTHERM_NUMBER_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
88#endif
89#ifndef OPENTHERM_OUTPUT_MESSAGE_HANDLERS
90#define OPENTHERM_OUTPUT_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
91#endif
92#ifndef OPENTHERM_INPUT_SENSOR_MESSAGE_HANDLERS
93#define OPENTHERM_INPUT_SENSOR_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
94#endif
95#ifndef OPENTHERM_SETTING_MESSAGE_HANDLERS
96#define OPENTHERM_SETTING_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
97#endif
98
99// Write data request builders
100#define OPENTHERM_MESSAGE_WRITE_MESSAGE(msg) \
101 case MessageId::msg: { \
102 data.type = MessageType::WRITE_DATA; \
103 data.id = request_id;
104#define OPENTHERM_MESSAGE_WRITE_ENTITY(key, msg_data) message_data::write_##msg_data(this->key->state, data);
105#define OPENTHERM_MESSAGE_WRITE_SETTING(key, msg_data) message_data::write_##msg_data(this->key, data);
106#define OPENTHERM_MESSAGE_WRITE_POSTSCRIPT \
107 return data; \
108 }
109
110// Read data request builder
111#define OPENTHERM_MESSAGE_READ_MESSAGE(msg) \
112 case MessageId::msg: \
113 data.type = MessageType::READ_DATA; \
114 data.id = request_id; \
115 return data;
116
117// Data processing builders
118#define OPENTHERM_MESSAGE_RESPONSE_MESSAGE(msg) case MessageId::msg:
119#define OPENTHERM_MESSAGE_RESPONSE_ENTITY(key, msg_data) this->key->publish_state(message_data::parse_##msg_data(data));
120#define OPENTHERM_MESSAGE_RESPONSE_POSTSCRIPT break;
121
122#define OPENTHERM_IGNORE(x, y)
123
124// Default macros for STATUS entities
125#ifndef OPENTHERM_READ_ch_enable
126#define OPENTHERM_READ_ch_enable true
127#endif
128#ifndef OPENTHERM_READ_dhw_enable
129#define OPENTHERM_READ_dhw_enable true
130#endif
131#ifndef OPENTHERM_READ_t_set
132#define OPENTHERM_READ_t_set 0.0
133#endif
134#ifndef OPENTHERM_READ_cooling_enable
135#define OPENTHERM_READ_cooling_enable false
136#endif
137#ifndef OPENTHERM_READ_cooling_control
138#define OPENTHERM_READ_cooling_control 0.0
139#endif
140#ifndef OPENTHERM_READ_otc_active
141#define OPENTHERM_READ_otc_active false
142#endif
143#ifndef OPENTHERM_READ_ch2_active
144#define OPENTHERM_READ_ch2_active false
145#endif
146#ifndef OPENTHERM_READ_t_set_ch2
147#define OPENTHERM_READ_t_set_ch2 0.0
148#endif
149#ifndef OPENTHERM_READ_summer_mode_active
150#define OPENTHERM_READ_summer_mode_active false
151#endif
152#ifndef OPENTHERM_READ_dhw_block
153#define OPENTHERM_READ_dhw_block false
154#endif
155
156// These macros utilize the structure of *_LIST macros in order
157#define ID(x) x
158#define SHOW_INNER(x) #x
159#define SHOW(x) SHOW_INNER(x)
160
161} // namespace opentherm
162} // namespace esphome
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7