ESPHome  2024.11.0
opentherm_macros.h
Go to the documentation of this file.
1 #pragma once
2 namespace esphome {
3 namespace 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 
32 // Use macros to create fields for every entity specified in the ESPHome configuration
33 #define OPENTHERM_DECLARE_SENSOR(entity) sensor::Sensor *entity;
34 #define OPENTHERM_DECLARE_BINARY_SENSOR(entity) binary_sensor::BinarySensor *entity;
35 #define OPENTHERM_DECLARE_SWITCH(entity) OpenthermSwitch *entity;
36 #define OPENTHERM_DECLARE_NUMBER(entity) OpenthermNumber *entity;
37 #define OPENTHERM_DECLARE_OUTPUT(entity) OpenthermOutput *entity;
38 #define OPENTHERM_DECLARE_INPUT_SENSOR(entity) sensor::Sensor *entity;
39 
40 // Setter macros
41 #define OPENTHERM_SET_SENSOR(entity) \
42  void set_##entity(sensor::Sensor *sensor) { this->entity = sensor; }
43 
44 #define OPENTHERM_SET_BINARY_SENSOR(entity) \
45  void set_##entity(binary_sensor::BinarySensor *binary_sensor) { this->entity = binary_sensor; }
46 
47 #define OPENTHERM_SET_SWITCH(entity) \
48  void set_##entity(OpenthermSwitch *sw) { this->entity = sw; }
49 
50 #define OPENTHERM_SET_NUMBER(entity) \
51  void set_##entity(OpenthermNumber *number) { this->entity = number; }
52 
53 #define OPENTHERM_SET_OUTPUT(entity) \
54  void set_##entity(OpenthermOutput *output) { this->entity = output; }
55 
56 #define OPENTHERM_SET_INPUT_SENSOR(entity) \
57  void set_##entity(sensor::Sensor *sensor) { this->entity = sensor; }
58 
59 // ===== hub.cpp macros =====
60 
61 // *_MESSAGE_HANDLERS are generated in defines.h and look like this:
62 // OPENTHERM_NUMBER_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep) MESSAGE(COOLING_CONTROL)
63 // ENTITY(cooling_control_number, f88) postscript msg_sep They contain placeholders for message part and entities parts,
64 // since one message can contain multiple entities. MESSAGE part is substituted with OPENTHERM_MESSAGE_WRITE_MESSAGE,
65 // OPENTHERM_MESSAGE_READ_MESSAGE or OPENTHERM_MESSAGE_RESPONSE_MESSAGE. ENTITY part is substituted with
66 // OPENTHERM_MESSAGE_WRITE_ENTITY or OPENTHERM_MESSAGE_RESPONSE_ENTITY. OPENTHERM_IGNORE is used for sensor read
67 // requests since no data needs to be sent or processed, just the data id.
68 
69 // In order for things not to break, we define empty lists here in case some platforms are not used in config.
70 #ifndef OPENTHERM_SENSOR_MESSAGE_HANDLERS
71 #define OPENTHERM_SENSOR_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
72 #endif
73 #ifndef OPENTHERM_BINARY_SENSOR_MESSAGE_HANDLERS
74 #define OPENTHERM_BINARY_SENSOR_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
75 #endif
76 #ifndef OPENTHERM_SWITCH_MESSAGE_HANDLERS
77 #define OPENTHERM_SWITCH_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
78 #endif
79 #ifndef OPENTHERM_NUMBER_MESSAGE_HANDLERS
80 #define OPENTHERM_NUMBER_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
81 #endif
82 #ifndef OPENTHERM_OUTPUT_MESSAGE_HANDLERS
83 #define OPENTHERM_OUTPUT_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
84 #endif
85 #ifndef OPENTHERM_INPUT_SENSOR_MESSAGE_HANDLERS
86 #define OPENTHERM_INPUT_SENSOR_MESSAGE_HANDLERS(MESSAGE, ENTITY, entity_sep, postscript, msg_sep)
87 #endif
88 
89 // Write data request builders
90 #define OPENTHERM_MESSAGE_WRITE_MESSAGE(msg) \
91  case MessageId::msg: { \
92  data.type = MessageType::WRITE_DATA; \
93  data.id = request_id;
94 #define OPENTHERM_MESSAGE_WRITE_ENTITY(key, msg_data) message_data::write_##msg_data(this->key->state, data);
95 #define OPENTHERM_MESSAGE_WRITE_POSTSCRIPT \
96  return data; \
97  }
98 
99 // Read data request builder
100 #define OPENTHERM_MESSAGE_READ_MESSAGE(msg) \
101  case MessageId::msg: \
102  data.type = MessageType::READ_DATA; \
103  data.id = request_id; \
104  return data;
105 
106 // Data processing builders
107 #define OPENTHERM_MESSAGE_RESPONSE_MESSAGE(msg) case MessageId::msg:
108 #define OPENTHERM_MESSAGE_RESPONSE_ENTITY(key, msg_data) this->key->publish_state(message_data::parse_##msg_data(data));
109 #define OPENTHERM_MESSAGE_RESPONSE_POSTSCRIPT break;
110 
111 #define OPENTHERM_IGNORE(x, y)
112 
113 // Default macros for STATUS entities
114 #ifndef OPENTHERM_READ_ch_enable
115 #define OPENTHERM_READ_ch_enable true
116 #endif
117 #ifndef OPENTHERM_READ_dhw_enable
118 #define OPENTHERM_READ_dhw_enable true
119 #endif
120 #ifndef OPENTHERM_READ_t_set
121 #define OPENTHERM_READ_t_set 0.0
122 #endif
123 #ifndef OPENTHERM_READ_cooling_enable
124 #define OPENTHERM_READ_cooling_enable false
125 #endif
126 #ifndef OPENTHERM_READ_cooling_control
127 #define OPENTHERM_READ_cooling_control 0.0
128 #endif
129 #ifndef OPENTHERM_READ_otc_active
130 #define OPENTHERM_READ_otc_active false
131 #endif
132 #ifndef OPENTHERM_READ_ch2_active
133 #define OPENTHERM_READ_ch2_active false
134 #endif
135 #ifndef OPENTHERM_READ_t_set_ch2
136 #define OPENTHERM_READ_t_set_ch2 0.0
137 #endif
138 #ifndef OPENTHERM_READ_summer_mode_active
139 #define OPENTHERM_READ_summer_mode_active false
140 #endif
141 #ifndef OPENTHERM_READ_dhw_block
142 #define OPENTHERM_READ_dhw_block false
143 #endif
144 
145 // These macros utilize the structure of *_LIST macros in order
146 #define ID(x) x
147 #define SHOW_INNER(x) #x
148 #define SHOW(x) SHOW_INNER(x)
149 
150 } // namespace opentherm
151 } // namespace esphome
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7