Migrate hard coded fake traffic to the YAML (#26)

This commit is contained in:
Sebastian Muszynski
2023-03-24 10:03:51 +01:00
committed by GitHub
parent c4e9aceec0
commit ff5d821927
13 changed files with 92 additions and 106 deletions

View File

@@ -8,7 +8,6 @@ CODEOWNERS = ["@syssi"]
MULTI_CONF = True
CONF_VOTRONIC_ID = "votronic_id"
CONF_ENABLE_FAKE_TRAFFIC = "enable_fake_traffic"
CONF_RX_TIMEOUT = "rx_timeout"
votronic_ns = cg.esphome_ns.namespace("votronic")
@@ -21,7 +20,6 @@ CONFIG_SCHEMA = (
cv.Optional(
CONF_THROTTLE, default="2s"
): cv.positive_time_period_milliseconds,
cv.Optional(CONF_ENABLE_FAKE_TRAFFIC, default=False): cv.boolean,
cv.Optional(
CONF_RX_TIMEOUT, default="150ms"
): cv.positive_time_period_milliseconds,
@@ -38,5 +36,4 @@ async def to_code(config):
await uart.register_uart_device(var, config)
cg.add(var.set_throttle(config[CONF_THROTTLE]))
cg.add(var.set_enable_fake_traffic(config[CONF_ENABLE_FAKE_TRAFFIC]))
cg.add(var.set_rx_timeout(config[CONF_RX_TIMEOUT]))

View File

@@ -77,16 +77,7 @@ void Votronic::loop() {
}
}
void Votronic::update() {
if (this->enable_fake_traffic_) {
this->on_votronic_data_(
{0xAA, 0x1A, 0x17, 0x05, 0xEA, 0x06, 0x22, 0x00, 0x00, 0x00, 0x00, 0x22, 0x22, 0x00, 0x09}); // 0xCF
this->on_votronic_data_(
{0xAA, 0x3A, 0xA0, 0x05, 0xA4, 0x06, 0x78, 0x00, 0x00, 0x00, 0xA0, 0x15, 0x03, 0x00, 0x00}); // 0xF3
this->on_votronic_data_(
{0xAA, 0x7A, 0xA0, 0x05, 0xA4, 0x06, 0x78, 0x00, 0x00, 0x00, 0xA0, 0x15, 0x03, 0x00, 0x00}); // 0xB3
}
}
void Votronic::update() {}
bool Votronic::parse_votronic_byte_(uint8_t byte) {
size_t at = this->rx_buffer_.size();
@@ -116,15 +107,15 @@ bool Votronic::parse_votronic_byte_(uint8_t byte) {
ESP_LOGVV(TAG, "RX <- %s", format_hex_pretty(raw, at + 1).c_str());
std::vector<uint8_t> data(this->rx_buffer_.begin(), this->rx_buffer_.begin() + frame_len - 1);
std::vector<uint8_t> data(this->rx_buffer_.begin(), this->rx_buffer_.begin() + frame_len);
this->on_votronic_data_(data);
this->on_votronic_data(data);
// return false to reset buffer
return false;
}
void Votronic::on_votronic_data_(const std::vector<uint8_t> &data) {
void Votronic::on_votronic_data(const std::vector<uint8_t> &data) {
const uint32_t now = millis();
if (now - this->last_frame_ < this->throttle_) {
return;
@@ -132,7 +123,7 @@ void Votronic::on_votronic_data_(const std::vector<uint8_t> &data) {
this->last_frame_ = now;
uint8_t data_len = data.size();
if (data_len != VOTRONIC_FRAME_LENGTH - 1) {
if (data_len != VOTRONIC_FRAME_LENGTH) {
ESP_LOGW(TAG, "Skipping frame because of invalid length: %d", data_len);
return;
}
@@ -237,7 +228,6 @@ void Votronic::decode_charger_data_(const uint8_t &frame_type, const std::vector
void Votronic::dump_config() {
ESP_LOGCONFIG(TAG, "Votronic:");
ESP_LOGCONFIG(TAG, " RX timeout: %d ms", this->rx_timeout_);
ESP_LOGCONFIG(TAG, " Fake traffic enabled: %s", YESNO(this->enable_fake_traffic_));
LOG_BINARY_SENSOR("", "Charging", this->charging_binary_sensor_);
LOG_BINARY_SENSOR("", "Discharging", this->discharging_binary_sensor_);

View File

@@ -75,8 +75,8 @@ class Votronic : public uart::UARTDevice, public PollingComponent {
charging_mode_setting_text_sensor_ = charging_mode_setting_text_sensor;
}
void on_votronic_data(const std::vector<uint8_t> &data);
void set_throttle(uint16_t throttle) { this->throttle_ = throttle; }
void set_enable_fake_traffic(bool enable_fake_traffic) { enable_fake_traffic_ = enable_fake_traffic; }
void set_rx_timeout(uint16_t rx_timeout) { rx_timeout_ = rx_timeout; }
protected:
@@ -110,9 +110,7 @@ class Votronic : public uart::UARTDevice, public PollingComponent {
uint32_t last_frame_{0};
uint16_t throttle_;
uint16_t rx_timeout_{150};
bool enable_fake_traffic_;
void on_votronic_data_(const std::vector<uint8_t> &data);
void decode_solar_charger_data_(const std::vector<uint8_t> &data);
void decode_charger_data_(const uint8_t &frame_type, const std::vector<uint8_t> &data);
bool parse_votronic_byte_(uint8_t byte);

View File

@@ -8,7 +8,6 @@ CODEOWNERS = ["@syssi"]
MULTI_CONF = True
CONF_VOTRONIC_BLE_ID = "votronic_ble_id"
CONF_ENABLE_FAKE_TRAFFIC = "enable_fake_traffic"
votronic_ble_ns = cg.esphome_ns.namespace("votronic_ble")
VotronicBle = votronic_ble_ns.class_(
@@ -22,7 +21,6 @@ CONFIG_SCHEMA = (
cv.Optional(
CONF_THROTTLE, default="2s"
): cv.positive_time_period_milliseconds,
cv.Optional(CONF_ENABLE_FAKE_TRAFFIC, default=False): cv.boolean,
}
)
.extend(ble_client.BLE_CLIENT_SCHEMA)
@@ -36,4 +34,3 @@ async def to_code(config):
await ble_client.register_ble_node(var, config)
cg.add(var.set_throttle(config[CONF_THROTTLE]))
cg.add(var.set_enable_fake_traffic(config[CONF_ENABLE_FAKE_TRAFFIC]))

View File

@@ -82,7 +82,7 @@ void VotronicBle::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t
std::vector<uint8_t> data(param->notify.value, param->notify.value + param->notify.value_len);
this->on_votronic_ble_data_(param->notify.handle, data);
this->on_votronic_ble_data(param->notify.handle, data);
break;
}
default:
@@ -91,33 +91,13 @@ void VotronicBle::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t
}
void VotronicBle::update() {
if (this->enable_fake_traffic_) {
this->char_solar_charger_handle_ = 0x25;
this->char_battery_computer_handle_ = 0x22;
// Solar charger status frame
this->on_votronic_ble_data_(0x25, {0xE8, 0x04, 0x76, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x56, 0x00, 0x09,
0x18, 0x00, 0x22, 0x00, 0x00, 0x00});
// Battery computer status frame
this->on_votronic_ble_data_(0x22, {0xE8, 0x04, 0xBF, 0x04, 0x09, 0x01, 0x60, 0x00, 0x5F, 0x00,
0x9A, 0xFE, 0xFF, 0xF0, 0x0A, 0x5E, 0x14, 0x54, 0x02, 0x04});
// Battery computer status frame (max current: +8388.607 A)
this->on_votronic_ble_data_(0x22, {0xE8, 0x04, 0xBF, 0x04, 0x09, 0x01, 0x60, 0x00, 0x5F, 0x00,
0xFF, 0xFF, 0x7F, 0xF0, 0x0A, 0x5E, 0x14, 0x54, 0x02, 0x04});
// Battery computer status frame (min current: -8388.608 A)
this->on_votronic_ble_data_(0x22, {0xE8, 0x04, 0xBF, 0x04, 0x09, 0x01, 0x60, 0x00, 0x5F, 0x00,
0x00, 0x00, 0x80, 0xF0, 0x0A, 0x5E, 0x14, 0x54, 0x02, 0x04});
}
if (this->node_state != espbt::ClientState::ESTABLISHED) {
ESP_LOGW(TAG, "[%s] Not connected", this->parent_->address_str().c_str());
return;
}
}
void VotronicBle::on_votronic_ble_data_(const uint8_t &handle, const std::vector<uint8_t> &data) {
void VotronicBle::on_votronic_ble_data(const uint8_t &handle, const std::vector<uint8_t> &data) {
if (handle == this->char_solar_charger_handle_) {
this->decode_solar_charger_data_(data);
return;
@@ -229,7 +209,6 @@ void VotronicBle::dump_config() {
ESP_LOGCONFIG(TAG, " Battery Computer Characteristic UUID: %s",
this->char_battery_computer_uuid_.to_string().c_str());
ESP_LOGCONFIG(TAG, " Solar Charger Characteristic UUID : %s", this->char_solar_charger_uuid_.to_string().c_str());
ESP_LOGCONFIG(TAG, " Fake traffic enabled: %s", YESNO(this->enable_fake_traffic_));
LOG_BINARY_SENSOR("", "Charging", this->charging_binary_sensor_);
LOG_BINARY_SENSOR("", "Discharging", this->discharging_binary_sensor_);

View File

@@ -81,8 +81,8 @@ class VotronicBle : public esphome::ble_client::BLEClientNode, public PollingCom
pv_controller_status_text_sensor_ = pv_controller_status_text_sensor;
}
void on_votronic_ble_data(const uint8_t &handle, const std::vector<uint8_t> &data);
void set_throttle(uint16_t throttle) { this->throttle_ = throttle; }
void set_enable_fake_traffic(bool enable_fake_traffic) { enable_fake_traffic_ = enable_fake_traffic; }
protected:
binary_sensor::BinarySensor *charging_binary_sensor_;
@@ -109,12 +109,11 @@ class VotronicBle : public esphome::ble_client::BLEClientNode, public PollingCom
text_sensor::TextSensor *battery_status_text_sensor_;
text_sensor::TextSensor *pv_controller_status_text_sensor_;
uint16_t char_battery_computer_handle_;
uint16_t char_solar_charger_handle_;
uint16_t char_battery_computer_handle_{0x22};
uint16_t char_solar_charger_handle_{0x25};
uint32_t last_battery_computer_data_{0};
uint32_t last_solar_charger_data_{0};
uint16_t throttle_;
bool enable_fake_traffic_;
esp32_ble_tracker::ESPBTUUID service_bond_uuid_ =
esp32_ble_tracker::ESPBTUUID::from_raw("70521e61-022d-f899-d046-4885a76acbd0");
@@ -133,7 +132,6 @@ class VotronicBle : public esphome::ble_client::BLEClientNode, public PollingCom
esp32_ble_tracker::ESPBTUUID char_bulk_data_uuid_ =
esp32_ble_tracker::ESPBTUUID::from_raw("b8a37ffe-c57b-4007-b3c1-ca05a6b7f0c6");
void on_votronic_ble_data_(const uint8_t &handle, const std::vector<uint8_t> &data);
void decode_solar_charger_data_(const std::vector<uint8_t> &data);
void decode_battery_computer_data_(const std::vector<uint8_t> &data);
void publish_state_(binary_sensor::BinarySensor *binary_sensor, const bool &state);

View File

@@ -1,8 +1,35 @@
<<: !include esp32-ble-example-debug.yaml
votronic_ble:
- ble_client_id: client0
id: votronic0
throttle: 0ms
update_interval: 1s
enable_fake_traffic: true
interval:
- interval: 8s
then:
# Solar charger status frame
- lambda: |-
id(votronic0).on_votronic_ble_data(0x25, {
0xE8, 0x04, 0x76, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
0x56, 0x00, 0x09, 0x18, 0x00, 0x22, 0x00, 0x00, 0x00
});
- delay: 2s
# Battery computer status frame
- lambda: |-
id(votronic0).on_votronic_ble_data(0x22, {
0xE8, 0x04, 0xBF, 0x04, 0x09, 0x01, 0x60, 0x00, 0x5F, 0x00,
0x9A, 0xFE, 0xFF, 0xF0, 0x0A, 0x5E, 0x14, 0x54, 0x02, 0x04
});
- delay: 2s
# Battery computer status frame (max current: +8388.607 A)
- lambda: |-
id(votronic0).on_votronic_ble_data(0x22, {
0xE8, 0x04, 0xBF, 0x04, 0x09, 0x01, 0x60, 0x00, 0x5F, 0x00,
0xFF, 0xFF, 0x7F, 0xF0, 0x0A, 0x5E, 0x14, 0x54, 0x02, 0x04
});
- delay: 2s
# Battery computer status frame (min current: -8388.608 A)
- lambda: |-
id(votronic0).on_votronic_ble_data(0x22, {
0xE8, 0x04, 0xBF, 0x04, 0x09, 0x01, 0x60, 0x00, 0x5F, 0x00,
0x00, 0x00, 0x80, 0xF0, 0x0A, 0x5E, 0x14, 0x54, 0x02, 0x04
});

View File

@@ -16,3 +16,12 @@ logger:
mqtt.switch: INFO
api.service: INFO
api: INFO
uart:
- id: uart0
baud_rate: 1000
tx_pin: ${tx_pin}
rx_pin: ${rx_pin}
debug:
direction: BOTH
dummy_receiver: false

View File

@@ -1,18 +1,9 @@
<<: !include esp8266-charger-example-debug.yaml
uart:
- id: uart0
baud_rate: 1000
tx_pin: ${tx_pin}
rx_pin: ${rx_pin}
debug:
direction: BOTH
dummy_receiver: false
votronic:
- id: votronic0
uart_id: uart0
rx_timeout: ${rx_timeout}
throttle: 0ms
update_interval: 1s
enable_fake_traffic: true
interval:
- interval: 2s
then:
- lambda: |-
id(votronic0).on_votronic_data({
0xAA, 0x3A, 0xA0, 0x05, 0xA4, 0x06, 0x78, 0x00, 0x00, 0x00, 0xA0, 0x15, 0x03, 0x00, 0x00, 0xF3
});

View File

@@ -16,3 +16,12 @@ logger:
mqtt.switch: INFO
api.service: INFO
api: INFO
uart:
- id: uart0
baud_rate: 1000
tx_pin: ${tx_pin}
rx_pin: ${rx_pin}
debug:
direction: BOTH
dummy_receiver: false

View File

@@ -1,18 +1,9 @@
<<: !include esp8266-charging-converter-example-debug.yaml
uart:
- id: uart0
baud_rate: 1000
tx_pin: ${tx_pin}
rx_pin: ${rx_pin}
debug:
direction: BOTH
dummy_receiver: false
votronic:
- id: votronic0
uart_id: uart0
rx_timeout: ${rx_timeout}
throttle: 0ms
update_interval: 1s
enable_fake_traffic: true
interval:
- interval: 2s
then:
- lambda: |-
id(votronic0).on_votronic_data({
0xAA, 0x7A, 0xA0, 0x05, 0xA4, 0x06, 0x78, 0x00, 0x00, 0x00, 0xA0, 0x15, 0x03, 0x00, 0x00, 0xB3
});

View File

@@ -16,3 +16,12 @@ logger:
mqtt.switch: INFO
api.service: INFO
api: INFO
uart:
- id: uart0
baud_rate: 1000
tx_pin: ${tx_pin}
rx_pin: ${rx_pin}
debug:
direction: BOTH
dummy_receiver: false

View File

@@ -1,18 +1,9 @@
<<: !include esp8266-solar-charger-example-debug.yaml
uart:
- id: uart0
baud_rate: 1000
tx_pin: ${tx_pin}
rx_pin: ${rx_pin}
debug:
direction: BOTH
dummy_receiver: false
votronic:
- id: votronic0
uart_id: uart0
rx_timeout: ${rx_timeout}
throttle: 0ms
update_interval: 1s
enable_fake_traffic: true
interval:
- interval: 2s
then:
- lambda: |-
id(votronic0).on_votronic_data({
0xAA, 0x1A, 0x17, 0x05, 0xEA, 0x06, 0x22, 0x00, 0x00, 0x00, 0x00, 0x22, 0x22, 0x00, 0x09, 0xCF
});