Skip to content

Commit b0a60cd

Browse files
authored
Add C++ tests (#132)
1 parent 591b506 commit b0a60cd

6 files changed

Lines changed: 662 additions & 25 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ jobs:
4848
ref: dev
4949
path: esphome
5050

51-
- name: Copy external component into the esphome project
51+
- name: Copy external component and tests into the esphome project
5252
run: |
5353
cd esphome
5454
cp -r ../components/* esphome/components/
55+
mkdir -p tests/components
56+
cp -r ../tests/components/* tests/components/
5557
git config user.name "ci"
5658
git config user.email "ci@github.com"
5759
git add .
@@ -410,6 +412,55 @@ jobs:
410412
. venv/bin/activate
411413
pytest tests/ -v
412414
415+
cpp-unit-tests:
416+
name: Run C++ unit tests
417+
runs-on: ubuntu-24.04
418+
needs:
419+
- bundle
420+
- common
421+
defaults:
422+
run:
423+
working-directory: esphome
424+
steps:
425+
- name: Download prepared repository
426+
uses: pyTooling/download-artifact@dc575e4e9df4b6e3580712285f1c90f579bb8712 # v8
427+
with:
428+
name: bundle
429+
path: .
430+
431+
- name: Update index to make "git diff-index" happy
432+
run: git update-index -q --really-refresh
433+
434+
- name: Restore Python
435+
uses: ./.github/actions/restore-python
436+
with:
437+
python-version: ${{ env.DEFAULT_PYTHON }}
438+
cache-key: ${{ needs.common.outputs.cache-key }}
439+
440+
- name: Cache platformio
441+
if: github.ref == 'refs/heads/main'
442+
uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
443+
with:
444+
path: ~/.platformio
445+
key: platformio-host-${{ hashFiles('esphome/platformio.ini') }}
446+
restore-keys: platformio-host-
447+
448+
- name: Cache platformio
449+
if: github.ref != 'refs/heads/main'
450+
uses: actions/cache/restore@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
451+
with:
452+
path: ~/.platformio
453+
key: platformio-host-${{ hashFiles('esphome/platformio.ini') }}
454+
restore-keys: platformio-host-
455+
456+
- name: Run C++ unit tests
457+
run: |
458+
. venv/bin/activate
459+
script/cpp_unit_test.py atorch_dl24
460+
env:
461+
PLATFORMIO_LIBDEPS_DIR: ~/.platformio/libdeps
462+
ASAN_OPTIONS: detect_leaks=0
463+
413464
esphome-config:
414465
name: Validate example configurations
415466
runs-on: ubuntu-24.04

components/atorch_dl24/atorch_dl24.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,16 @@
22
#include "esphome/core/log.h"
33
#include "esphome/core/version.h"
44

5-
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025, 12, 0)
6-
#define ADDR_STR(x) x
7-
#else
8-
#define ADDR_STR(x) (x).c_str()
9-
#endif
10-
11-
#ifdef USE_ESP32
12-
135
namespace esphome {
146
namespace atorch_dl24 {
157

168
static const char *const TAG = "atorch_dl24";
179

18-
static const uint16_t DL24_SERVICE_UUID = 0xFFE0;
19-
static const uint16_t DL24_REPORT_CHARACTERISTIC_UUID = 0xFFE1;
20-
static const uint16_t DL24_COMMAND_CHARACTERISTIC_UUID = 0xFFE2;
21-
2210
static const uint8_t START_OF_FRAME_BYTE1 = 0xFF;
2311
static const uint8_t START_OF_FRAME_BYTE2 = 0x55;
2412

2513
static const uint8_t MESSAGE_TYPE_REPORT = 0x01;
2614
static const uint8_t MESSAGE_TYPE_REPLY = 0x02;
27-
static const uint8_t MESSAGE_TYPE_COMMAND = 0x11;
2815

2916
static const uint8_t DEVICE_TYPE_AC_METER = 0x01;
3017
static const uint8_t DEVICE_TYPE_DC_METER = 0x02;
@@ -34,6 +21,19 @@ static const uint8_t COMMAND_SUCCESS = 0x01;
3421
static const uint8_t COMMAND_FAILED = 0x02;
3522
static const uint8_t COMMAND_UNSUPPORTED = 0x03;
3623

24+
#ifdef USE_ESP32
25+
static const uint16_t DL24_SERVICE_UUID = 0xFFE0;
26+
static const uint16_t DL24_REPORT_CHARACTERISTIC_UUID = 0xFFE1;
27+
static const uint16_t DL24_COMMAND_CHARACTERISTIC_UUID = 0xFFE2;
28+
static const uint8_t MESSAGE_TYPE_COMMAND = 0x11;
29+
30+
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025, 12, 0)
31+
#define ADDR_STR(x) x
32+
#else
33+
#define ADDR_STR(x) (x).c_str()
34+
#endif
35+
#endif
36+
3737
uint8_t crc(const uint8_t data[], const uint16_t len) {
3838
uint8_t crc = 0;
3939

@@ -45,6 +45,7 @@ uint8_t crc(const uint8_t data[], const uint16_t len) {
4545
}
4646

4747
bool AtorchDL24::write_register(uint8_t device_type, uint8_t address, uint32_t value) {
48+
#ifdef USE_ESP32
4849
uint8_t frame[10];
4950
frame[0] = START_OF_FRAME_BYTE1;
5051
frame[1] = START_OF_FRAME_BYTE2;
@@ -66,6 +67,9 @@ bool AtorchDL24::write_register(uint8_t device_type, uint8_t address, uint32_t v
6667
ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", ADDR_STR(this->parent_->address_str()), status);
6768

6869
return (status == 0);
70+
#else
71+
return false;
72+
#endif
6973
}
7074

7175
void AtorchDL24::dump_config() {
@@ -87,6 +91,7 @@ void AtorchDL24::dump_config() {
8791
LOG_TEXT_SENSOR(" ", "Runtime Formatted", this->runtime_formatted_text_sensor_);
8892
}
8993

94+
#ifdef USE_ESP32
9095
void AtorchDL24::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
9196
esp_ble_gattc_cb_param_t *param) {
9297
switch (event) {
@@ -169,6 +174,7 @@ void AtorchDL24::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t g
169174
break;
170175
}
171176
}
177+
#endif
172178

173179
// AC Meter
174180
//
@@ -483,5 +489,3 @@ void AtorchDL24::publish_state_(text_sensor::TextSensor *text_sensor, const std:
483489

484490
} // namespace atorch_dl24
485491
} // namespace esphome
486-
487-
#endif

components/atorch_dl24/atorch_dl24.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
#pragma once
22

33
#include "esphome/core/component.h"
4-
#include "esphome/components/ble_client/ble_client.h"
5-
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
64
#include "esphome/components/binary_sensor/binary_sensor.h"
75
#include "esphome/components/sensor/sensor.h"
86
#include "esphome/components/text_sensor/text_sensor.h"
97
#include "esphome/core/hal.h"
108

119
#ifdef USE_ESP32
12-
10+
#include "esphome/components/ble_client/ble_client.h"
11+
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
1312
#include <esp_gattc_api.h>
13+
namespace espbt = esphome::esp32_ble_tracker;
14+
#endif
1415

1516
namespace esphome {
1617
namespace atorch_dl24 {
1718

18-
namespace espbt = esphome::esp32_ble_tracker;
19-
20-
class AtorchDL24 : public esphome::ble_client::BLEClientNode, public Component {
19+
class AtorchDL24 :
20+
#ifdef USE_ESP32
21+
public esphome::ble_client::BLEClientNode,
22+
#endif
23+
public Component {
2124
public:
25+
#ifdef USE_ESP32
2226
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
2327
esp_ble_gattc_cb_param_t *param) override;
28+
#endif
2429
void dump_config() override;
2530
float get_setup_priority() const override { return setup_priority::DATA; }
2631

@@ -109,18 +114,18 @@ class AtorchDL24 : public esphome::ble_client::BLEClientNode, public Component {
109114

110115
text_sensor::TextSensor *runtime_formatted_text_sensor_{nullptr};
111116

112-
bool check_crc_;
117+
bool check_crc_{false};
113118

114119
uint8_t previous_value_ = 61;
115120
std::vector<uint8_t> frame_buffer_;
116121
uint8_t device_type_ = 0x00;
122+
#ifdef USE_ESP32
117123
uint16_t char_notify_handle_{0};
118124
uint16_t char_command_handle_{0};
125+
#endif
119126
uint32_t last_publish_{0};
120127
uint32_t throttle_{0};
121128
};
122129

123130
} // namespace atorch_dl24
124131
} // namespace esphome
125-
126-
#endif

0 commit comments

Comments
 (0)