From ff5d82192780cbd31a2fea97a0ad66621dd7f7cf Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Fri, 24 Mar 2023 10:03:51 +0100 Subject: [PATCH] Migrate hard coded fake traffic to the YAML (#26) --- components/votronic/__init__.py | 3 -- components/votronic/votronic.cpp | 20 +++------- components/votronic/votronic.h | 4 +- components/votronic_ble/__init__.py | 3 -- components/votronic_ble/votronic_ble.cpp | 25 +----------- components/votronic_ble/votronic_ble.h | 8 ++-- esp32-ble-example-faker.yaml | 39 ++++++++++++++++--- esp8266-charger-example-debug.yaml | 9 +++++ esp8266-charger-example-faker.yaml | 23 ++++------- esp8266-charging-converter-example-debug.yaml | 9 +++++ esp8266-charging-converter-example-faker.yaml | 23 ++++------- esp8266-solar-charger-example-debug.yaml | 9 +++++ esp8266-solar-charger-example-faker.yaml | 23 ++++------- 13 files changed, 92 insertions(+), 106 deletions(-) diff --git a/components/votronic/__init__.py b/components/votronic/__init__.py index 0c7c6b3..b200b20 100644 --- a/components/votronic/__init__.py +++ b/components/votronic/__init__.py @@ -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])) diff --git a/components/votronic/votronic.cpp b/components/votronic/votronic.cpp index fd15c99..ab927ea 100644 --- a/components/votronic/votronic.cpp +++ b/components/votronic/votronic.cpp @@ -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 data(this->rx_buffer_.begin(), this->rx_buffer_.begin() + frame_len - 1); + std::vector 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 &data) { +void Votronic::on_votronic_data(const std::vector &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 &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_); diff --git a/components/votronic/votronic.h b/components/votronic/votronic.h index 6912032..1db0f85 100644 --- a/components/votronic/votronic.h +++ b/components/votronic/votronic.h @@ -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 &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 &data); void decode_solar_charger_data_(const std::vector &data); void decode_charger_data_(const uint8_t &frame_type, const std::vector &data); bool parse_votronic_byte_(uint8_t byte); diff --git a/components/votronic_ble/__init__.py b/components/votronic_ble/__init__.py index a49bd14..38136ab 100644 --- a/components/votronic_ble/__init__.py +++ b/components/votronic_ble/__init__.py @@ -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])) diff --git a/components/votronic_ble/votronic_ble.cpp b/components/votronic_ble/votronic_ble.cpp index 29a715b..3c19627 100644 --- a/components/votronic_ble/votronic_ble.cpp +++ b/components/votronic_ble/votronic_ble.cpp @@ -82,7 +82,7 @@ void VotronicBle::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t std::vector 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 &data) { +void VotronicBle::on_votronic_ble_data(const uint8_t &handle, const std::vector &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_); diff --git a/components/votronic_ble/votronic_ble.h b/components/votronic_ble/votronic_ble.h index 0d0f1e4..3fe7788 100644 --- a/components/votronic_ble/votronic_ble.h +++ b/components/votronic_ble/votronic_ble.h @@ -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 &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 &data); void decode_solar_charger_data_(const std::vector &data); void decode_battery_computer_data_(const std::vector &data); void publish_state_(binary_sensor::BinarySensor *binary_sensor, const bool &state); diff --git a/esp32-ble-example-faker.yaml b/esp32-ble-example-faker.yaml index a5bc9aa..fc33f84 100644 --- a/esp32-ble-example-faker.yaml +++ b/esp32-ble-example-faker.yaml @@ -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 + }); diff --git a/esp8266-charger-example-debug.yaml b/esp8266-charger-example-debug.yaml index 54e158f..b17f80d 100644 --- a/esp8266-charger-example-debug.yaml +++ b/esp8266-charger-example-debug.yaml @@ -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 diff --git a/esp8266-charger-example-faker.yaml b/esp8266-charger-example-faker.yaml index 5209931..ad1f75b 100644 --- a/esp8266-charger-example-faker.yaml +++ b/esp8266-charger-example-faker.yaml @@ -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 + }); diff --git a/esp8266-charging-converter-example-debug.yaml b/esp8266-charging-converter-example-debug.yaml index 30ea171..b46b05d 100644 --- a/esp8266-charging-converter-example-debug.yaml +++ b/esp8266-charging-converter-example-debug.yaml @@ -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 diff --git a/esp8266-charging-converter-example-faker.yaml b/esp8266-charging-converter-example-faker.yaml index a589928..32f6789 100644 --- a/esp8266-charging-converter-example-faker.yaml +++ b/esp8266-charging-converter-example-faker.yaml @@ -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 + }); diff --git a/esp8266-solar-charger-example-debug.yaml b/esp8266-solar-charger-example-debug.yaml index 645046a..494b65a 100644 --- a/esp8266-solar-charger-example-debug.yaml +++ b/esp8266-solar-charger-example-debug.yaml @@ -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 diff --git a/esp8266-solar-charger-example-faker.yaml b/esp8266-solar-charger-example-faker.yaml index 7e1fea4..b76f7be 100644 --- a/esp8266-solar-charger-example-faker.yaml +++ b/esp8266-solar-charger-example-faker.yaml @@ -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 + });