ESPHome  2024.12.2
vbus_sensor.cpp
Go to the documentation of this file.
1 #include "vbus_sensor.h"
2 #include "esphome/core/helpers.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace vbus {
7 
8 static const char *const TAG = "vbus.sensor";
9 
10 static inline uint16_t get_u16(std::vector<uint8_t> &message, int start) {
11  return (message[start + 1] << 8) + message[start];
12 }
13 
14 static inline int16_t get_i16(std::vector<uint8_t> &message, int start) {
15  return (int16_t) ((message[start + 1] << 8) + message[start]);
16 }
17 
19  ESP_LOGCONFIG(TAG, "Deltasol BS Plus:");
20  LOG_SENSOR(" ", "Temperature 1", this->temperature1_sensor_);
21  LOG_SENSOR(" ", "Temperature 2", this->temperature2_sensor_);
22  LOG_SENSOR(" ", "Temperature 3", this->temperature3_sensor_);
23  LOG_SENSOR(" ", "Temperature 4", this->temperature4_sensor_);
24  LOG_SENSOR(" ", "Pump Speed 1", this->pump_speed1_sensor_);
25  LOG_SENSOR(" ", "Pump Speed 2", this->pump_speed2_sensor_);
26  LOG_SENSOR(" ", "Operating Hours 1", this->operating_hours1_sensor_);
27  LOG_SENSOR(" ", "Operating Hours 2", this->operating_hours2_sensor_);
28  LOG_SENSOR(" ", "Heat Quantity", this->heat_quantity_sensor_);
29  LOG_SENSOR(" ", "System Time", this->time_sensor_);
30  LOG_SENSOR(" ", "FW Version", this->version_sensor_);
31 }
32 
33 void DeltaSolBSPlusSensor::handle_message(std::vector<uint8_t> &message) {
34  if (this->temperature1_sensor_ != nullptr)
35  this->temperature1_sensor_->publish_state(get_i16(message, 0) * 0.1f);
36  if (this->temperature2_sensor_ != nullptr)
37  this->temperature2_sensor_->publish_state(get_i16(message, 2) * 0.1f);
38  if (this->temperature3_sensor_ != nullptr)
39  this->temperature3_sensor_->publish_state(get_i16(message, 4) * 0.1f);
40  if (this->temperature4_sensor_ != nullptr)
41  this->temperature4_sensor_->publish_state(get_i16(message, 6) * 0.1f);
42  if (this->pump_speed1_sensor_ != nullptr)
43  this->pump_speed1_sensor_->publish_state(message[8]);
44  if (this->pump_speed2_sensor_ != nullptr)
45  this->pump_speed2_sensor_->publish_state(message[9]);
46  if (this->operating_hours1_sensor_ != nullptr)
47  this->operating_hours1_sensor_->publish_state(get_u16(message, 16));
48  if (this->operating_hours2_sensor_ != nullptr)
49  this->operating_hours2_sensor_->publish_state(get_u16(message, 18));
50  if (this->heat_quantity_sensor_ != nullptr) {
51  this->heat_quantity_sensor_->publish_state(get_u16(message, 20) + get_u16(message, 22) * 1000 +
52  get_u16(message, 24) * 1000000);
53  }
54  if (this->time_sensor_ != nullptr)
55  this->time_sensor_->publish_state(get_u16(message, 12));
56  if (this->version_sensor_ != nullptr)
57  this->version_sensor_->publish_state(get_u16(message, 26) * 0.01f);
58 }
59 
61  ESP_LOGCONFIG(TAG, "Deltasol BS 2009:");
62  LOG_SENSOR(" ", "Temperature 1", this->temperature1_sensor_);
63  LOG_SENSOR(" ", "Temperature 2", this->temperature2_sensor_);
64  LOG_SENSOR(" ", "Temperature 3", this->temperature3_sensor_);
65  LOG_SENSOR(" ", "Temperature 4", this->temperature4_sensor_);
66  LOG_SENSOR(" ", "Pump Speed 1", this->pump_speed1_sensor_);
67  LOG_SENSOR(" ", "Pump Speed 2", this->pump_speed2_sensor_);
68  LOG_SENSOR(" ", "Operating Hours 1", this->operating_hours1_sensor_);
69  LOG_SENSOR(" ", "Operating Hours 2", this->operating_hours2_sensor_);
70  LOG_SENSOR(" ", "Heat Quantity", this->heat_quantity_sensor_);
71  LOG_SENSOR(" ", "System Time", this->time_sensor_);
72  LOG_SENSOR(" ", "FW Version", this->version_sensor_);
73 }
74 
75 void DeltaSolBS2009Sensor::handle_message(std::vector<uint8_t> &message) {
76  if (this->temperature1_sensor_ != nullptr)
77  this->temperature1_sensor_->publish_state(get_i16(message, 0) * 0.1f);
78  if (this->temperature2_sensor_ != nullptr)
79  this->temperature2_sensor_->publish_state(get_i16(message, 2) * 0.1f);
80  if (this->temperature3_sensor_ != nullptr)
81  this->temperature3_sensor_->publish_state(get_i16(message, 4) * 0.1f);
82  if (this->temperature4_sensor_ != nullptr)
83  this->temperature4_sensor_->publish_state(get_i16(message, 6) * 0.1f);
84  if (this->pump_speed1_sensor_ != nullptr)
85  this->pump_speed1_sensor_->publish_state(message[8]);
86  if (this->pump_speed2_sensor_ != nullptr)
87  this->pump_speed2_sensor_->publish_state(message[12]);
88  if (this->operating_hours1_sensor_ != nullptr)
89  this->operating_hours1_sensor_->publish_state(get_u16(message, 10));
90  if (this->operating_hours2_sensor_ != nullptr)
91  this->operating_hours2_sensor_->publish_state(get_u16(message, 18));
92  if (this->heat_quantity_sensor_ != nullptr) {
93  this->heat_quantity_sensor_->publish_state(get_u16(message, 28) + get_u16(message, 30) * 1000);
94  }
95  if (this->time_sensor_ != nullptr)
96  this->time_sensor_->publish_state(get_u16(message, 22));
97  if (this->version_sensor_ != nullptr)
98  this->version_sensor_->publish_state(get_u16(message, 32) * 0.01f);
99 }
100 
102  ESP_LOGCONFIG(TAG, "Deltasol C:");
103  LOG_SENSOR(" ", "Temperature 1", this->temperature1_sensor_);
104  LOG_SENSOR(" ", "Temperature 2", this->temperature2_sensor_);
105  LOG_SENSOR(" ", "Temperature 3", this->temperature3_sensor_);
106  LOG_SENSOR(" ", "Temperature 4", this->temperature4_sensor_);
107  LOG_SENSOR(" ", "Pump Speed 1", this->pump_speed1_sensor_);
108  LOG_SENSOR(" ", "Pump Speed 2", this->pump_speed2_sensor_);
109  LOG_SENSOR(" ", "Operating Hours 1", this->operating_hours1_sensor_);
110  LOG_SENSOR(" ", "Operating Hours 2", this->operating_hours2_sensor_);
111  LOG_SENSOR(" ", "Heat Quantity", this->heat_quantity_sensor_);
112  LOG_SENSOR(" ", "System Time", this->time_sensor_);
113 }
114 
115 void DeltaSolCSensor::handle_message(std::vector<uint8_t> &message) {
116  if (this->temperature1_sensor_ != nullptr)
117  this->temperature1_sensor_->publish_state(get_i16(message, 0) * 0.1f);
118  if (this->temperature2_sensor_ != nullptr)
119  this->temperature2_sensor_->publish_state(get_i16(message, 2) * 0.1f);
120  if (this->temperature3_sensor_ != nullptr)
121  this->temperature3_sensor_->publish_state(get_i16(message, 4) * 0.1f);
122  if (this->temperature4_sensor_ != nullptr)
123  this->temperature4_sensor_->publish_state(get_i16(message, 6) * 0.1f);
124  if (this->pump_speed1_sensor_ != nullptr)
125  this->pump_speed1_sensor_->publish_state(message[8]);
126  if (this->pump_speed2_sensor_ != nullptr)
127  this->pump_speed2_sensor_->publish_state(message[9]);
128  if (this->operating_hours1_sensor_ != nullptr)
129  this->operating_hours1_sensor_->publish_state(get_u16(message, 12));
130  if (this->operating_hours2_sensor_ != nullptr)
131  this->operating_hours2_sensor_->publish_state(get_u16(message, 14));
132  if (this->heat_quantity_sensor_ != nullptr) {
133  this->heat_quantity_sensor_->publish_state(get_u16(message, 16) + get_u16(message, 18) * 1000 +
134  get_u16(message, 20) * 1000000);
135  }
136  if (this->time_sensor_ != nullptr)
137  this->time_sensor_->publish_state(get_u16(message, 22));
138 }
139 
141  ESP_LOGCONFIG(TAG, "Deltasol CS2:");
142  LOG_SENSOR(" ", "Temperature 1", this->temperature1_sensor_);
143  LOG_SENSOR(" ", "Temperature 2", this->temperature2_sensor_);
144  LOG_SENSOR(" ", "Temperature 3", this->temperature3_sensor_);
145  LOG_SENSOR(" ", "Temperature 4", this->temperature4_sensor_);
146  LOG_SENSOR(" ", "Pump Speed", this->pump_speed_sensor_);
147  LOG_SENSOR(" ", "Operating Hours", this->operating_hours_sensor_);
148  LOG_SENSOR(" ", "Heat Quantity", this->heat_quantity_sensor_);
149  LOG_SENSOR(" ", "FW Version", this->version_sensor_);
150 }
151 
152 void DeltaSolCS2Sensor::handle_message(std::vector<uint8_t> &message) {
153  if (this->temperature1_sensor_ != nullptr)
154  this->temperature1_sensor_->publish_state(get_i16(message, 0) * 0.1f);
155  if (this->temperature2_sensor_ != nullptr)
156  this->temperature2_sensor_->publish_state(get_i16(message, 2) * 0.1f);
157  if (this->temperature3_sensor_ != nullptr)
158  this->temperature3_sensor_->publish_state(get_i16(message, 4) * 0.1f);
159  if (this->temperature4_sensor_ != nullptr)
160  this->temperature4_sensor_->publish_state(get_i16(message, 6) * 0.1f);
161  if (this->pump_speed_sensor_ != nullptr)
162  this->pump_speed_sensor_->publish_state(message[12]);
163  if (this->operating_hours_sensor_ != nullptr)
164  this->operating_hours_sensor_->publish_state(get_u16(message, 14));
165  if (this->heat_quantity_sensor_ != nullptr)
166  this->heat_quantity_sensor_->publish_state((get_u16(message, 26) << 16) + get_u16(message, 24));
167  if (this->version_sensor_ != nullptr)
168  this->version_sensor_->publish_state(get_u16(message, 28) * 0.01f);
169 }
170 
172  ESP_LOGCONFIG(TAG, "Deltasol CS Plus:");
173  LOG_SENSOR(" ", "Temperature 1", this->temperature1_sensor_);
174  LOG_SENSOR(" ", "Temperature 2", this->temperature2_sensor_);
175  LOG_SENSOR(" ", "Temperature 3", this->temperature3_sensor_);
176  LOG_SENSOR(" ", "Temperature 4", this->temperature4_sensor_);
177  LOG_SENSOR(" ", "Temperature 5", this->temperature5_sensor_);
178  LOG_SENSOR(" ", "Pump Speed 1", this->pump_speed1_sensor_);
179  LOG_SENSOR(" ", "Pump Speed 2", this->pump_speed2_sensor_);
180  LOG_SENSOR(" ", "Operating Hours 1", this->operating_hours1_sensor_);
181  LOG_SENSOR(" ", "Operating Hours 2", this->operating_hours2_sensor_);
182  LOG_SENSOR(" ", "Heat Quantity", this->heat_quantity_sensor_);
183  LOG_SENSOR(" ", "System Time", this->time_sensor_);
184  LOG_SENSOR(" ", "FW Version", this->version_sensor_);
185  LOG_SENSOR(" ", "Flow Rate", this->flow_rate_sensor_);
186 }
187 
188 void DeltaSolCSPlusSensor::handle_message(std::vector<uint8_t> &message) {
189  if (this->temperature1_sensor_ != nullptr)
190  this->temperature1_sensor_->publish_state(get_i16(message, 0) * 0.1f);
191  if (this->temperature2_sensor_ != nullptr)
192  this->temperature2_sensor_->publish_state(get_i16(message, 2) * 0.1f);
193  if (this->temperature3_sensor_ != nullptr)
194  this->temperature3_sensor_->publish_state(get_i16(message, 4) * 0.1f);
195  if (this->temperature4_sensor_ != nullptr)
196  this->temperature4_sensor_->publish_state(get_i16(message, 6) * 0.1f);
197  if (this->temperature5_sensor_ != nullptr)
198  this->temperature5_sensor_->publish_state(get_i16(message, 36) * 0.1f);
199  if (this->pump_speed1_sensor_ != nullptr)
200  this->pump_speed1_sensor_->publish_state(message[8]);
201  if (this->pump_speed2_sensor_ != nullptr)
202  this->pump_speed2_sensor_->publish_state(message[12]);
203  if (this->operating_hours1_sensor_ != nullptr)
204  this->operating_hours1_sensor_->publish_state(get_u16(message, 10));
205  if (this->operating_hours2_sensor_ != nullptr)
206  this->operating_hours2_sensor_->publish_state(get_u16(message, 14));
207  if (this->heat_quantity_sensor_ != nullptr)
208  this->heat_quantity_sensor_->publish_state((get_u16(message, 30) << 16) + get_u16(message, 28));
209  if (this->time_sensor_ != nullptr)
210  this->time_sensor_->publish_state(get_u16(message, 22));
211  if (this->version_sensor_ != nullptr)
212  this->version_sensor_->publish_state(get_u16(message, 32) * 0.01f);
213  if (this->flow_rate_sensor_ != nullptr)
214  this->flow_rate_sensor_->publish_state(get_u16(message, 38));
215 }
216 
218  ESP_LOGCONFIG(TAG, "VBus Custom Sensor:");
219  if (this->source_ == 0xffff) {
220  ESP_LOGCONFIG(TAG, " Source address: ANY");
221  } else {
222  ESP_LOGCONFIG(TAG, " Source address: 0x%04x", this->source_);
223  }
224  if (this->dest_ == 0xffff) {
225  ESP_LOGCONFIG(TAG, " Dest address: ANY");
226  } else {
227  ESP_LOGCONFIG(TAG, " Dest address: 0x%04x", this->dest_);
228  }
229  if (this->command_ == 0xffff) {
230  ESP_LOGCONFIG(TAG, " Command: ANY");
231  } else {
232  ESP_LOGCONFIG(TAG, " Command: 0x%04x", this->command_);
233  }
234  ESP_LOGCONFIG(TAG, " Sensors:");
235  for (VBusCustomSubSensor *sensor : this->sensors_) {
236  LOG_SENSOR(" ", "-", sensor);
237  }
238 }
239 
240 void VBusCustomSensor::handle_message(std::vector<uint8_t> &message) {
241  for (VBusCustomSubSensor *sensor : this->sensors_)
242  sensor->parse_message(message);
243 }
244 
245 void VBusCustomSubSensor::parse_message(std::vector<uint8_t> &message) {
246  this->publish_state(this->message_parser_(message));
247 }
248 
249 } // namespace vbus
250 } // namespace esphome
void handle_message(std::vector< uint8_t > &message) override
void handle_message(std::vector< uint8_t > &message) override
void parse_message(std::vector< uint8_t > &message)
sensor::Sensor * heat_quantity_sensor_
Definition: vbus_sensor.h:33
sensor::Sensor * temperature4_sensor_
Definition: vbus_sensor.h:28
sensor::Sensor * pump_speed2_sensor_
Definition: vbus_sensor.h:30
void handle_message(std::vector< uint8_t > &message) override
sensor::Sensor * temperature1_sensor_
Definition: vbus_sensor.h:25
sensor::Sensor * temperature3_sensor_
Definition: vbus_sensor.h:27
const char *const TAG
Definition: spi.cpp:8
void handle_message(std::vector< uint8_t > &message) override
Definition: vbus_sensor.cpp:33
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
void handle_message(std::vector< uint8_t > &message) override
sensor::Sensor * operating_hours1_sensor_
Definition: vbus_sensor.h:31
void handle_message(std::vector< uint8_t > &message) override
Definition: vbus_sensor.cpp:75
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
sensor::Sensor * temperature2_sensor_
Definition: vbus_sensor.h:26
sensor::Sensor * pump_speed1_sensor_
Definition: vbus_sensor.h:29
esphome::sensor::Sensor * sensor
Definition: statsd.h:38
sensor::Sensor * operating_hours2_sensor_
Definition: vbus_sensor.h:32