ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
delta_wallbox.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <cstdint>
6
8
10
11inline uint64_t read_be(const uint8_t *p, size_t n) {
12 uint64_t value = 0;
13 for (size_t i = 0; i < n; i++)
14 value = (value << 8) | p[i];
15 return value;
16}
17
19 float power_kw;
22 uint8_t state;
23};
24
25// Decodes the 0x011B charging info response, starting at the unit byte: [3..9] session energy (Wh),
26// [10..13] power (W), [14..16] hours, minutes and seconds, [17] state, [18..20] unused.
27inline ChargingInfo decode_charging_info(const uint8_t *frame) {
28 const uint32_t power_raw = std::min<uint32_t>(encode_value<uint32_t>(&frame[10]), 99999);
29 const uint64_t energy_raw = std::min<uint64_t>(read_be(&frame[3], 7), 999900);
30 const uint8_t hours = std::min<uint8_t>(frame[14], 99);
31 const uint8_t minutes = std::min<uint8_t>(frame[15], 59);
32 const uint8_t seconds = std::min<uint8_t>(frame[16], 59);
33 return {power_raw / 1000.0f, energy_raw / 1000.0f, hours * 60.0f + minutes + seconds / 60.0f, frame[17]};
34}
35
36} // namespace esphome::delta_wallbox
37
38#ifdef USE_ESP32
39
40#include <array>
47
48#ifdef USE_TIME
50#endif
51
52namespace esphome::delta_wallbox {
53// Largest FC03 response: unit, function, byte count, 255 data bytes, CRC.
54static constexpr size_t MAX_FRAME_LEN = 260;
71
73 public:
74 DeltaWallbox(const char *user, const char *password) : user_(user), password_(password) {}
75 void update() override;
76 void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
77 esp_ble_gattc_cb_param_t *param) override;
78 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override;
79 void dump_config() override;
80
81 void set_pin(uint32_t pin) {
82 this->pin_ = pin;
83 this->has_pin_ = true;
84 }
85#ifdef USE_TIME
86 void set_time_id(time::RealTimeClock *time_id) { this->time_id_ = time_id; }
87#endif
88
90 SUB_SENSOR(energy)
91 SUB_SENSOR(charging_time)
92 SUB_SENSOR(charger_state)
93 SUB_SENSOR(max_current)
94 SUB_NUMBER(max_current) // settable max-current number entity
95 SUB_TEXT_SENSOR(serial_number)
96 SUB_TEXT_SENSOR(fw_version)
97 SUB_TEXT_SENSOR(bt_fw_version)
98 SUB_TEXT_SENSOR(model_name)
99 SUB_TEXT_SENSOR(error_code)
100 void set_charging_state(bool state);
101 void set_max_current(float current);
102 void start_charging();
103 void stop_charging();
104
109
110 protected:
111 void write_frame_(const uint8_t *frame, size_t len);
112 void send_frame_(const uint8_t *body, size_t len);
114 void retransmit_();
116 void handle_frame_(const uint8_t *frame, size_t len);
117 void drop_rx_bytes_(size_t count);
118 void reset_state_();
119 // Stops the handshake until the next BLE disconnect; used for failures a retry can't fix.
120 void halt_(const LogString *reason);
121
122 // Members ordered by size so nothing is padded
126#ifdef USE_TIME
128#endif
129 size_t rx_len_{0};
130
131 uint16_t rx_char_handle_{0};
132 uint16_t tx_char_handle_{0};
136 bool has_pin_{false};
138 std::array<uint8_t, MAX_FRAME_LEN> rx_buffer_{};
139};
140} // namespace esphome::delta_wallbox
141#endif
This class simplifies creating components that periodically check a state.
Definition component.h:532
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
void write_frame_(const uint8_t *frame, size_t len)
void halt_(const LogString *reason)
void set_time_id(time::RealTimeClock *time_id)
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
SUB_SENSOR(power) SUB_SENSOR(energy) SUB_SENSOR(charging_time) SUB_SENSOR(charger_state) SUB_SENSOR(max_current) SUB_NUMBER(max_current) SUB_TEXT_SENSOR(serial_number) SUB_TEXT_SENSOR(fw_version) SUB_TEXT_SENSOR(bt_fw_version) SUB_TEXT_SENSOR(model_name) SUB_TEXT_SENSOR(error_code) void set_charging_state(bool state)
void handle_frame_(const uint8_t *frame, size_t len)
std::array< uint8_t, MAX_FRAME_LEN > rx_buffer_
void send_frame_(const uint8_t *body, size_t len)
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
DeltaWallbox(const char *user, const char *password)
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
NameCopy * next
bool state
Definition fan.h:2
unsigned count
Definition gif.cpp:51
uint64_t read_be(const uint8_t *p, size_t n)
ChargingInfo decode_charging_info(const uint8_t *frame)
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
constexpr T encode_value(const uint8_t *bytes)
Encode a value from its constituent bytes (from most to least significant) in an array with length si...
Definition helpers.h:1064
static void uint32_t
char serial_number[10]
Definition sun_gtil2.cpp:15