Merge pull request #47 from syssi/add-throttle

This commit is contained in:
Jiří Bětuňák 2022-05-21 09:57:46 +02:00 committed by GitHub
commit be1c30bfd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 19 additions and 1 deletions

View File

@ -105,6 +105,8 @@ esphome run esp8266-example.yaml
The `uart_id` and `victron_id` is optional if you use a single UART / victron device. All sensors are optional.
The victron device pushs one status message per second. To reduce the update interval of the ESPHome entities please use the `throttle` parameter to discard some messages.
The available numeric sensors are:
- `max_power_yesterday`
- `max_power_today`

View File

@ -1,7 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import uart
from esphome.const import CONF_ID
from esphome.const import CONF_ID, CONF_THROTTLE
AUTO_LOAD = ["binary_sensor", "sensor", "text_sensor"]
@ -19,6 +19,7 @@ CONF_VICTRON_ID = "victron_id"
CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(VictronComponent),
cv.Optional(CONF_THROTTLE, default="1s"): cv.positive_time_period_milliseconds,
}
)
@ -27,3 +28,5 @@ def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield uart.register_uart_device(var, config)
cg.add(var.set_throttle(config[CONF_THROTTLE]))

View File

@ -393,6 +393,12 @@ static const std::string device_type_text(int value) {
void VictronComponent::handle_value_() {
int value;
const uint32_t now = millis();
if (now - this->last_publish_ < this->throttle_) {
return;
}
this->last_publish_ = now;
if (label_ == "H23") {
if (max_power_yesterday_sensor_ != nullptr)
max_power_yesterday_sensor_->publish_state(atoi(value_.c_str())); // NOLINT(cert-err34-c)

View File

@ -11,6 +11,7 @@ namespace victron {
class VictronComponent : public uart::UARTDevice, public Component {
public:
void set_throttle(uint32_t throttle) { this->throttle_ = throttle; }
void set_load_state_binary_sensor(binary_sensor::BinarySensor *load_state_binary_sensor) {
load_state_binary_sensor_ = load_state_binary_sensor;
}
@ -119,6 +120,8 @@ class VictronComponent : public uart::UARTDevice, public Component {
std::string label_;
std::string value_;
uint32_t last_transmission_{0};
uint32_t last_publish_{0};
uint32_t throttle_{0};
};
} // namespace victron

View File

@ -61,6 +61,7 @@ uart:
victron:
id: victron0
uart_id: uart_bus
throttle: 10s
sensor:
- platform: victron

View File

@ -44,8 +44,10 @@ uart:
victron:
- id: victron0
uart_id: uart0
throttle: 10s
- id: victron1
uart_id: uart1
throttle: 10s
sensor:
- platform: victron

View File

@ -37,6 +37,7 @@ uart:
victron:
uart_id: uart0
id: victron0
throttle: 10s
sensor:
- platform: victron