Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a feature to periodically republish the remote temperature #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/mitsubishi_heatpump/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
VERTICAL_SWING_OPTIONS = ["swing", "auto", "up", "up_center", "center", "down_center", "down"]

# Remote temperature timeout configuration
CONF_REMOTE_PUBLISH_FREQUENCY = "remote_publish_frequency_seconds"
CONF_REMOTE_OPERATING_TIMEOUT = "remote_temperature_operating_timeout_minutes"
CONF_REMOTE_IDLE_TIMEOUT = "remote_temperature_idle_timeout_minutes"
CONF_REMOTE_PING_TIMEOUT = "remote_temperature_ping_timeout_minutes"
Expand Down Expand Up @@ -68,6 +69,7 @@ def valid_uart(uart):
cv.GenerateID(): cv.declare_id(MitsubishiHeatPump),
cv.Optional(CONF_HARDWARE_UART, default="UART0"): valid_uart,
cv.Optional(CONF_BAUD_RATE): cv.positive_int,
cv.Optional(CONF_REMOTE_PUBLISH_FREQUENCY): cv.positive_int,
cv.Optional(CONF_REMOTE_OPERATING_TIMEOUT): cv.positive_int,
cv.Optional(CONF_REMOTE_IDLE_TIMEOUT): cv.positive_int,
cv.Optional(CONF_REMOTE_PING_TIMEOUT): cv.positive_int,
Expand Down Expand Up @@ -110,6 +112,9 @@ def to_code(config):
if CONF_TX_PIN in config:
cg.add(var.set_tx_pin(config[CONF_TX_PIN]))

if CONF_REMOTE_PUBLISH_FREQUENCY in config:
cg.add(var.set_remote_publish_frequency_seconds(config[CONF_REMOTE_PUBLISH_FREQUENCY]))

if CONF_REMOTE_OPERATING_TIMEOUT in config:
cg.add(var.set_remote_operating_timeout_minutes(config[CONF_REMOTE_OPERATING_TIMEOUT]))

Expand Down
46 changes: 38 additions & 8 deletions components/mitsubishi_heatpump/espmhp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void MitsubishiHeatPump::update() {
this->hpStatusChanged(currentStatus);
#endif
this->enforce_remote_temperature_sensor_timeout();
this->set_remote_temperature(this->remote_temperature);
}

void MitsubishiHeatPump::set_baud_rate(int baud) {
Expand Down Expand Up @@ -620,22 +621,50 @@ void MitsubishiHeatPump::hpStatusChanged(heatpumpStatus currentStatus) {
}

void MitsubishiHeatPump::set_remote_temperature(float temp) {
ESP_LOGD(TAG, "Setting remote temp: %.1f", temp);
if (temp > 0) {
last_remote_temperature_sensor_update_ =
std::chrono::steady_clock::now();
} else {
last_remote_temperature_sensor_update_.reset();

bool isTempNew = (this->remote_temperature != temp);

if (isTempNew) {
if (temp > 0) {
this->remote_temperature = temp;

last_remote_temperature_sensor_update_ =
std::chrono::steady_clock::now();
} else {
this->remote_temperature = 0;
last_remote_temperature_sensor_update_.reset();
}
}

this->hp->setRemoteTemperature(temp);
auto time_since_last_remote_temperature_publish =
last_remote_temperature_publish_.has_value() ?
(std::chrono::steady_clock::now() - last_remote_temperature_publish_.value()) :
std::chrono::seconds(99999999);

bool refreshRemoteTemp = remote_publish_frequency_.has_value() ?
(time_since_last_remote_temperature_publish >= remote_publish_frequency_.value()) :
false;

// Do not send zeros UNLESS the zero is new, which means it wasn't zero at one point
// and now we need to update the HP to be zero once. This zero check also prevents
// us from telling the HP about a remote temperature if there is none
if (isTempNew || (refreshRemoteTemp && this->remote_temperature != 0)) {
ESP_LOGD(TAG, "Setting remote temp: %.1f", this->remote_temperature);
this->hp->setRemoteTemperature(this->remote_temperature);
last_remote_temperature_publish_ = std::chrono::steady_clock::now();
}
}

void MitsubishiHeatPump::ping() {
ESP_LOGD(TAG, "Ping request received");
last_ping_request_ = std::chrono::steady_clock::now();
}

void MitsubishiHeatPump::set_remote_publish_frequency_seconds(int seconds) {
ESP_LOGD(TAG, "Setting remote publish frequency: %d seconds", seconds);
remote_publish_frequency_ = std::chrono::seconds(seconds);
}

void MitsubishiHeatPump::set_remote_operating_timeout_minutes(int minutes) {
ESP_LOGD(TAG, "Setting remote operating timeout time: %d minutes", minutes);
remote_operating_timeout_ = std::chrono::minutes(minutes);
Expand Down Expand Up @@ -692,6 +721,7 @@ void MitsubishiHeatPump::setup() {
ESP_LOGCONFIG(TAG, "Initializing new HeatPump object.");
this->hp = new HeatPump();
this->current_temperature = NAN;
this->remote_temperature = 0;
this->target_temperature = NAN;
this->fan_mode = climate::CLIMATE_FAN_OFF;
this->swing_mode = climate::CLIMATE_SWING_OFF;
Expand Down Expand Up @@ -792,4 +822,4 @@ void MitsubishiHeatPump::log_packet(byte* packet, unsigned int length, char* pac
}

ESP_LOGV(TAG, "PKT: [%s] %s", packetDirection, packetHex.c_str());
}
}
11 changes: 11 additions & 0 deletions components/mitsubishi_heatpump/espmhp.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ class MitsubishiHeatPump : public esphome::PollingComponent, public esphome::cli
// and this heatpump.
void ping();

// Number of seconds to tell the HP about the remote temperature (if set).
// Newer HP seem to forget the remote temperature much faster and therefore
// require the remote temp to set even if there isn't a change. Anecdottaly,
// they seem to forget the temperature every 60s or so.
void set_remote_publish_frequency_seconds(int seconds);

// Number of minutes before the heatpump reverts back to the internal
// temperature sensor if the machine is currently operating.
void set_remote_operating_timeout_minutes(int);
Expand All @@ -127,6 +133,9 @@ class MitsubishiHeatPump : public esphome::PollingComponent, public esphome::cli
// The ClimateTraits supported by this HeatPump.
esphome::climate::ClimateTraits traits_;

// Remote Temperature
float remote_temperature{0};

// Vane position
void update_swing_horizontal(const std::string &swing);
void update_swing_vertical(const std::string &swing);
Expand Down Expand Up @@ -182,6 +191,8 @@ class MitsubishiHeatPump : public esphome::PollingComponent, public esphome::cli
std::optional<std::chrono::duration<long long, std::ratio<60>>> remote_operating_timeout_;
std::optional<std::chrono::duration<long long, std::ratio<60>>> remote_idle_timeout_;
std::optional<std::chrono::duration<long long, std::ratio<60>>> remote_ping_timeout_;
std::optional<std::chrono::duration<long long, std::ratio<1>>> remote_publish_frequency_;
std::optional<std::chrono::time_point<std::chrono::steady_clock>> last_remote_temperature_publish_;
std::optional<std::chrono::time_point<std::chrono::steady_clock>> last_remote_temperature_sensor_update_;
std::optional<std::chrono::time_point<std::chrono::steady_clock>> last_ping_request_;
};
Expand Down