Skip to content

Commit

Permalink
Publish only last command on sending
Browse files Browse the repository at this point in the history
  • Loading branch information
AzonInc committed Aug 13, 2024
1 parent 015a977 commit ff66087
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
51 changes: 29 additions & 22 deletions components/tcs_intercom/tcs_intercom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ namespace esphome
if(s.s_cmdReady)
{
ESP_LOGD(TAG, "Received command %x", s.s_cmd);
this->publish_command(s.s_cmd);
this->publish_command(s.s_cmd, true);

s.s_cmdReady = false;
s.s_cmd = 0;
Expand Down Expand Up @@ -280,44 +280,48 @@ namespace esphome
}
}

void TCSComponent::publish_command(uint32_t command)
void TCSComponent::publish_command(uint32_t command, bool fire_events)
{
// Convert to HEX
char byte_cmd[9];
sprintf(byte_cmd, "%08x", command);

// Fire Home Assistant Event
if (strcmp(event_, "esphome.none") != 0)
{
auto capi = new esphome::api::CustomAPIDevice();

ESP_LOGD(TAG, "Send event to home assistant on %s", event_);
capi->fire_homeassistant_event(event_, {{"command", byte_cmd}});
}

// Publish Command to Sensors
if (this->bus_command_ != nullptr)
{
this->bus_command_->publish_state(byte_cmd);
}

for (auto &listener : listeners_)
if(fire_events)
{
if (listener->f_.has_value())
// Fire Binary Sensors
for (auto &listener : listeners_)
{
auto val = (*listener->f_)();

if (val == command)
if (listener->f_.has_value())
{
auto val = (*listener->f_)();

if (val == command)
{
listener->turn_on(&listener->timer_, listener->auto_off_);
}
}
else
{
listener->turn_on(&listener->timer_, listener->auto_off_);
if (listener->command_ == command)
{
listener->turn_on(&listener->timer_, listener->auto_off_);
}
}
}
else

// Fire Home Assistant Event
if (strcmp(event_, "esphome.none") != 0)
{
if (listener->command_ == command)
{
listener->turn_on(&listener->timer_, listener->auto_off_);
}
auto capi = new esphome::api::CustomAPIDevice();

ESP_LOGD(TAG, "Send event to home assistant on %s", event_);
capi->fire_homeassistant_event(event_, {{"command", byte_cmd}});
}
}
}
Expand Down Expand Up @@ -379,6 +383,9 @@ namespace esphome
// Resume reading
ESP_LOGD(TAG, "Resume reading");
this->rx_pin_->attach_interrupt(TCSComponentStore::gpio_intr, &this->store_, gpio::INTERRUPT_ANY_EDGE);

// Publish received Command on Sensors, Events, etc.
this->publish_command(command, false);
}
}

Expand Down
2 changes: 1 addition & 1 deletion components/tcs_intercom/tcs_intercom.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace esphome
void set_hardware_version_sensor(text_sensor::TextSensor *hardware_version) { this->hardware_version_ = hardware_version; }

void send_command(uint32_t command);
void publish_command(uint32_t command);
void publish_command(uint32_t command, bool fire_events);

bool sending;

Expand Down

0 comments on commit ff66087

Please sign in to comment.