Skip to content

Commit

Permalink
add set_debounce_delay() to allow multiple subsequent call of control…
Browse files Browse the repository at this point in the history
…() method in short delays issue #21
  • Loading branch information
echavet committed Feb 5, 2024
1 parent e9033b4 commit 5cf28d5
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 18 deletions.
1 change: 1 addition & 0 deletions components/cn105/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ struct wantedHeatpumpSettings : heatpumpSettings {
bool hasChanged;
bool hasBeenSent;
uint8_t nb_deffered_requests;
long lastChange;
wantedHeatpumpSettings& operator=(const wantedHeatpumpSettings& other) {
if (this != &other) { // protection contre l'auto-affectation
heatpumpSettings::operator=(other); // Appel à l'opérateur d'affectation de la classe de base
Expand Down
11 changes: 8 additions & 3 deletions components/cn105/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
CN105Climate = cg.global_ns.class_("CN105Climate", climate.Climate, cg.Component)

CONF_REMOTE_TEMP_TIMEOUT = "remote_temperature_timeout"
CONF_DEBOUNCE_DELAY = "debounce_delay"

VaneOrientationSelect = cg.global_ns.class_(
"VaneOrientationSelect", select.Select, cg.Component
Expand Down Expand Up @@ -84,7 +85,10 @@ def valid_uart(uart):
cv.Optional(CONF_VERTICAL_SWING_SELECT): SELECT_SCHEMA,
cv.Optional(CONF_COMPRESSOR_FREQUENCY_SENSOR): SENSOR_SCHEMA,
cv.Optional(CONF_ISEE_SENSOR): ISEE_SENSOR_SCHEMA,
cv.Optional(CONF_REMOTE_TEMP_TIMEOUT, default="never"): cv.update_interval,
cv.Optional(CONF_REMOTE_TEMP_TIMEOUT, default="never"): cv.All(
cv.update_interval
),
cv.Optional(CONF_DEBOUNCE_DELAY, default="750ms"): cv.All(cv.update_interval),
# Optionally override the supported ClimateTraits.
cv.Optional(CONF_SUPPORTS, default={}): cv.Schema(
{
Expand Down Expand Up @@ -127,8 +131,9 @@ def to_code(config):
for mode in supports[CONF_SWING_MODE]:
cg.add(traits.add_supported_swing_mode(climate.CLIMATE_SWING_MODES[mode]))

if CONF_REMOTE_TEMP_TIMEOUT in config:
var.set_remote_temp_timeout(config[CONF_REMOTE_TEMP_TIMEOUT])
cg.add(var.set_remote_temp_timeout(config[CONF_REMOTE_TEMP_TIMEOUT]))

cg.add(var.set_debounce_delay(config[CONF_DEBOUNCE_DELAY]))

if CONF_HORIZONTAL_SWING_SELECT in config:
conf = config[CONF_HORIZONTAL_SWING_SELECT]
Expand Down
6 changes: 4 additions & 2 deletions components/cn105/climateControls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ using namespace esphome;

void CN105Climate::checkPendingWantedSettings() {

long now = CUSTOM_MILLIS;

if (this->firstRun) {
if ((this->firstRun) || (this->wantedSettings.hasBeenSent) || (now - this->wantedSettings.lastChange < this->debounce_delay_)) {
return;
}
if (this->currentSettings != this->wantedSettings) {
Expand All @@ -16,7 +17,7 @@ void CN105Climate::checkPendingWantedSettings() {
if (!this->wantedSettings.hasBeenSent) {
ESP_LOGD(TAG, "checkPendingWantedSettings - wanted settings have changed, sending them to the heatpump...");
this->sendWantedSettings();
this->set_timeout("checkWantedSettings", 1000, [this]() { this->wantedSettings.hasBeenSent = false; });
//this->set_timeout("checkWantedSettings", 1000, [this]() { this->wantedSettings.hasBeenSent = false; });
}
} else {
ESP_LOGI(TAG, "checkPendingWantedSettings - detected a change from IR Remote Control");
Expand Down Expand Up @@ -80,6 +81,7 @@ void CN105Climate::control(const esphome::climate::ClimateCall& call) {
ESP_LOGD(LOG_ACTION_EVT_TAG, "clim.control() -> User changed something...");
this->wantedSettings.hasChanged = true;
this->wantedSettings.hasBeenSent = false;
this->wantedSettings.lastChange = CUSTOM_MILLIS;
this->debugSettings("control (wantedSettings)", this->wantedSettings);

// we don't call sendWantedSettings() anymore because it will be called by the loop() method
Expand Down
5 changes: 5 additions & 0 deletions components/cn105/cn105.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ void CN105Climate::set_remote_temp_timeout(uint32_t timeout) {
}
}

void CN105Climate::set_debounce_delay(uint32_t delay) {
this->debounce_delay_ = delay;
ESP_LOGI(LOG_ACTION_EVT_TAG, "set_debounce_delay is set to %d", delay);
}

int CN105Climate::get_compressor_frequency() {
return currentStatus.compressorFrequency;
}
Expand Down
3 changes: 3 additions & 0 deletions components/cn105/cn105.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class CN105Climate : public climate::Climate, public Component, public uart::UAR

void set_remote_temp_timeout(uint32_t timeout);

void set_debounce_delay(uint32_t delay);

// this is the ping or heartbeat of the setRemotetemperature for timeout management
void setExternalTemperatureCheckout();

Expand Down Expand Up @@ -207,6 +209,7 @@ class CN105Climate : public climate::Climate, public Component, public uart::UAR
unsigned long lastResponseMs;

uint32_t remote_temp_timeout_;
uint32_t debounce_delay_;

int baud_ = 0;
int tx_pin_ = -1;
Expand Down
5 changes: 5 additions & 0 deletions components/cn105/componentEntries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ void CN105Climate::setup() {
this->swing_mode = climate::CLIMATE_SWING_OFF;
this->initBytePointer();
this->lastResponseMs = CUSTOM_MILLIS;

ESP_LOGI(TAG, "tx_pin: %d rx_pin: %d", this->tx_pin_, this->rx_pin_);
ESP_LOGI(TAG, "remote_temp_timeout is set to %d", this->remote_temp_timeout_);
ESP_LOGI(TAG, "debounce_delay is set to %d", this->debounce_delay_);

this->setupUART();
this->sendFirstConnectionPacket();
}
Expand Down
2 changes: 2 additions & 0 deletions components/cn105/extraComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void CN105Climate::set_vertical_vane_select(
this->setVaneSetting(setting);
this->wantedSettings.hasChanged = true;
this->wantedSettings.hasBeenSent = false;
this->wantedSettings.lastChange = CUSTOM_MILLIS;
});

}
Expand All @@ -46,6 +47,7 @@ void CN105Climate::set_horizontal_vane_select(
this->setWideVaneSetting(setting);
this->wantedSettings.hasChanged = true;
this->wantedSettings.hasBeenSent = false;
this->wantedSettings.lastChange = CUSTOM_MILLIS;
});

}
Expand Down
14 changes: 8 additions & 6 deletions components/cn105/hp_readings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,9 @@ void CN105Climate::updateSuccess() {
//this->last_received_packet_sensor->publish_state("0x61: update success");
// as the update was successful, we can set currentSettings to wantedSettings
// even if the next settings request will do the same.
if (this->wantedSettings.hasChanged) {
if (this->wantedSettings.hasBeenSent) {
ESP_LOGI(TAG, "And it was a wantedSetting ACK!");
this->wantedSettings.hasChanged = false;
//this->wantedSettings.hasChanged = false;
this->wantedSettings.hasBeenSent = false;
this->wantedSettings.nb_deffered_requests = 0; // reset the counter which is tested each update_request_interval in buildAndSendRequestsInfoPackets()
//this->settingsChanged(this->wantedSettings, "WantedSettingsUpdateSuccess");
Expand Down Expand Up @@ -370,8 +370,8 @@ void CN105Climate::wantedSettingsUpdateSuccess(heatpumpSettings settings) {

// as wantedSettings has been received with ACK by the heatpump
// we can update the surrentSettings
this->currentSettings = this->wantedSettings;
this->debugSettings("current", currentSettings);
//this->currentSettings = this->wantedSettings;
this->debugSettings("curStgs", currentSettings);
}

void CN105Climate::extTempUpdateSuccess() {
Expand All @@ -388,10 +388,12 @@ void CN105Climate::heatpumpUpdate(heatpumpSettings settings) {

if (this->firstRun) {
ESP_LOGD(TAG, "first run detected, setting wantedSettings to receivedSettings");

this->wantedSettings = settings;
this->wantedSettings.hasChanged = false;
this->wantedSettings.hasBeenSent = false;
this->wantedSettings.nb_deffered_requests = 0; // reset the counter which is tested each update_request_interval in buildAndSendRequestsInfoPackets()
this->wantedSettings.lastChange = 0; // never knew a change

this->firstRun = false;
this->debugSettings("received", settings);
Expand All @@ -410,9 +412,9 @@ void CN105Climate::heatpumpUpdate(heatpumpSettings settings) {

// no difference wt wantedSettings and received ones
// by security tag wantedSettings hasChanged to false
wantedSettings.hasChanged = false;
/*this->wantedSettings.hasChanged = false;
this->wantedSettings.hasBeenSent = false;
this->wantedSettings.nb_deffered_requests = 0;
this->wantedSettings.nb_deffered_requests = 0;*/
} else {
this->debugSettings("current", this->currentSettings);
this->debugSettings("wanted", this->wantedSettings);
Expand Down
3 changes: 2 additions & 1 deletion components/cn105/hp_writings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ void CN105Climate::createPacket(uint8_t* packet, heatpumpSettings settings) {
void CN105Climate::sendWantedSettings() {

if (this->isHeatpumpConnectionActive() && this->isConnected_) {
if (CUSTOM_MILLIS - this->lastSend > 500) { // we don't want to send too many packets
if (CUSTOM_MILLIS - this->lastSend > 100) { // we don't want to send too many packets

this->wantedSettings.hasBeenSent = true;
this->wantedSettings.hasChanged = false;
this->lastSend = CUSTOM_MILLIS;
ESP_LOGI(TAG, "sending wantedSettings..");

Expand Down
10 changes: 4 additions & 6 deletions esp32-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ logger:
READ : WARN
Header: INFO
Decoder : INFO
CONTROL_WANTED_SETTINGS: INFO
CONTROL_WANTED_SETTINGS: INFO


# Enable Home Assistant API
Expand Down Expand Up @@ -166,8 +166,6 @@ sensor:
- lambda: |-
id(esp32_clim).set_remote_temperature(x);
uart:
id: HP_UART
baud_rate: 2400
Expand All @@ -186,9 +184,9 @@ climate:
name: Orientation de la Vane Horizontale
isee_sensor:
name: ISEE Sensor
remote_temperature_timeout: 30min
update_interval: 10s # shouldn't be less than 1 second

remote_temperature_timeout: 5min
update_interval: 1s # shouldn't be less than 1 second
debounce_delay : 500ms # delay to prevent bouncing


# Configuration pour l'objet 'switch' (bouton)
Expand Down

0 comments on commit 5cf28d5

Please sign in to comment.