Optionally set baud_rate in YAML

Some models require different baud rates (e.g.
https://github.com/SwiCago/HeatPump/pull/158).
This commit is contained in:
Simon Knopp 2021-05-26 21:20:11 +12:00
parent 65edd60078
commit 83c00b8d10
3 changed files with 14 additions and 2 deletions

View File

@ -5,6 +5,7 @@ from esphome.components.logger import HARDWARE_UART_TO_SERIAL
from esphome.const import (
CONF_ID,
CONF_HARDWARE_UART,
CONF_BAUD_RATE,
CONF_UPDATE_INTERVAL,
CONF_MODE,
CONF_FAN_MODE,
@ -37,6 +38,7 @@ CONFIG_SCHEMA = climate.CLIMATE_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(MitsubishiHeatPump),
cv.Optional(CONF_HARDWARE_UART, default="UART0"): valid_uart,
cv.Optional(CONF_BAUD_RATE): cv.positive_int,
# If polling interval is greater than 9 seconds, the HeatPump library
# reconnects, but doesn't then follow up with our data request.
@ -65,6 +67,9 @@ def to_code(config):
serial = HARDWARE_UART_TO_SERIAL[config[CONF_HARDWARE_UART]]
var = cg.new_Pvariable(config[CONF_ID], cg.RawExpression(f'&{serial}'))
if CONF_BAUD_RATE in config:
cg.add(var.set_baud_rate(config[CONF_BAUD_RATE]))
traits = []
for mode in config[CONF_SUPPORTS][CONF_MODE]:
if mode == 'OFF':

View File

@ -64,6 +64,10 @@ void MitsubishiHeatPump::update() {
#endif
}
void MitsubishiHeatPump::set_baud_rate(int baud) {
this->baud_ = baud;
}
/**
* Get our supported traits.
*
@ -443,7 +447,7 @@ void MitsubishiHeatPump::setup() {
ESP_LOGCONFIG(TAG, "Calling hp->connect(%p)", this->get_hw_serial_());
if (hp->connect(this->get_hw_serial_())) {
if (hp->connect(this->get_hw_serial_(), this->baud_)) {
hp->sync();
}
else {

View File

@ -62,6 +62,9 @@ class MitsubishiHeatPump : public PollingComponent, public climate::Climate {
ESPMHP_VERSION);
}
// Set the baud rate. Must be called before setup() to have any effect.
void set_baud_rate(int);
// print the current configuration
void dump_config() override;
@ -124,7 +127,7 @@ class MitsubishiHeatPump : public PollingComponent, public climate::Climate {
private:
// Retrieve the HardwareSerial pointer from friend and subclasses.
HardwareSerial *hw_serial_;
int baud_ = 0;
};
#endif