Added uart client timeout fix

This commit is contained in:
ElVit 2025-12-01 19:34:59 +01:00
parent c3c6106ade
commit 98836df74f
3 changed files with 22 additions and 0 deletions

View File

@ -9,6 +9,7 @@ MULTICONF = True
CONF_PANASONIC_HEATPUMP_ID = "panasonic_heatpump" CONF_PANASONIC_HEATPUMP_ID = "panasonic_heatpump"
CONF_UART_CLIENT = "uart_client_id" CONF_UART_CLIENT = "uart_client_id"
CONF_UART_CLIENT_TIMEOUT = "uart_client_timeout"
CONF_LOG_UART_MSG = "log_uart_msg" CONF_LOG_UART_MSG = "log_uart_msg"
panasonic_heatpump_ns = cg.esphome_ns.namespace("panasonic_heatpump") panasonic_heatpump_ns = cg.esphome_ns.namespace("panasonic_heatpump")
@ -21,6 +22,7 @@ CONFIG_SCHEMA = (
{ {
cv.GenerateID(): cv.declare_id(PanasonicHeatpumpComponent), cv.GenerateID(): cv.declare_id(PanasonicHeatpumpComponent),
cv.Optional(CONF_UART_CLIENT): cv.use_id(uart.UARTComponent), cv.Optional(CONF_UART_CLIENT): cv.use_id(uart.UARTComponent),
cv.Optional(CONF_UART_CLIENT_TIMEOUT, default="10000ms"): cv.positive_time_period_milliseconds,
cv.Optional(CONF_LOG_UART_MSG, default=False): cv.boolean, cv.Optional(CONF_LOG_UART_MSG, default=False): cv.boolean,
} }
) )
@ -37,5 +39,6 @@ async def to_code(config):
if CONF_UART_CLIENT in config: if CONF_UART_CLIENT in config:
uart_client = await cg.get_variable(config[CONF_UART_CLIENT]) uart_client = await cg.get_variable(config[CONF_UART_CLIENT])
cg.add(var.set_uart_client(uart_client)) cg.add(var.set_uart_client(uart_client))
cg.add(var.set_uart_client_timeout(config[CONF_UART_CLIENT_TIMEOUT]))
cg.add(var.set_log_uart_msg(config[CONF_LOG_UART_MSG])) cg.add(var.set_log_uart_msg(config[CONF_LOG_UART_MSG]))

View File

@ -25,6 +25,14 @@ void PanasonicHeatpumpComponent::update() {
} }
void PanasonicHeatpumpComponent::loop() { void PanasonicHeatpumpComponent::loop() {
// Check if no request was sent for uart_client_timeout when uart_client is configured
if (this->uart_client_ != nullptr && this->uart_client_timeout_ > 0) {
uint32_t current_time = millis();
if (current_time - this->last_request_time_ >= this->uart_client_timeout_) {
this->next_request_ = RequestType::POLLING;
}
}
switch (this->loop_state_) { switch (this->loop_state_) {
case LoopState::READ_RESPONSE: case LoopState::READ_RESPONSE:
this->read_response(); this->read_response();
@ -184,6 +192,9 @@ void PanasonicHeatpumpComponent::send_request(RequestType requestType) {
break; break;
}; };
// Update last request time when request was sent
this->last_request_time_ = millis();
this->next_request_ = RequestType::NONE; this->next_request_ = RequestType::NONE;
} }
@ -232,6 +243,9 @@ void PanasonicHeatpumpComponent::read_request() {
this->request_receiving_ = false; this->request_receiving_ = false;
if (this->log_uart_msg_) if (this->log_uart_msg_)
PanasonicHelpers::log_uart_hex(UART_LOG_TX, this->request_message_, ','); PanasonicHelpers::log_uart_hex(UART_LOG_TX, this->request_message_, ',');
// Update last request time when request is complete
this->last_request_time_ = millis();
} }
} }
} }

View File

@ -74,6 +74,9 @@ class PanasonicHeatpumpComponent : public PollingComponent, public uart::UARTDev
void set_uart_client(uart::UARTComponent* uart) { void set_uart_client(uart::UARTComponent* uart) {
this->uart_client_ = uart; this->uart_client_ = uart;
} }
void set_uart_client_timeout(uint32_t timeout_ms) {
this->uart_client_timeout_ = timeout_ms;
}
void set_log_uart_msg(bool active) { void set_log_uart_msg(bool active) {
this->log_uart_msg_ = active; this->log_uart_msg_ = active;
} }
@ -115,6 +118,8 @@ class PanasonicHeatpumpComponent : public PollingComponent, public uart::UARTDev
// options variables // options variables
uart::UARTComponent* uart_client_{nullptr}; uart::UARTComponent* uart_client_{nullptr};
bool log_uart_msg_{false}; bool log_uart_msg_{false};
uint32_t last_request_time_{0};
uint32_t uart_client_timeout_{10000};
// uart message variables // uart message variables
std::vector<uint8_t> heatpump_default_message_; std::vector<uint8_t> heatpump_default_message_;
std::vector<uint8_t> heatpump_extra_message_; std::vector<uint8_t> heatpump_extra_message_;