Skip to content

Commit

Permalink
Add sensor unavailable state to Zones
Browse files Browse the repository at this point in the history
  • Loading branch information
TillFleisch committed Feb 7, 2024
1 parent d419a7e commit 304f28c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
17 changes: 16 additions & 1 deletion components/LD2450/LD2450.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,25 @@ namespace esphome::ld2450
peek_status_ = 0;
}
}

if (sensor_available_ && millis() - last_message_received_ > SENSOR_UNAVAILABLE_TIMEOUT)
{
sensor_available_ = false;

ESP_LOGE(TAG, "LD2450-Sensor stopped sending updates!");

// Update zones and related components
for (Zone *zone : zones_)
{
zone->update(targets_, sensor_available_);
}
}
}

void LD2450::process_message(uint8_t *msg, int len)
{
sensor_available_ = true;
last_message_received_ = millis();
for (int i = 0; i < 3; i++)
{
int offset = 8 * i;
Expand Down Expand Up @@ -251,7 +266,7 @@ namespace esphome::ld2450
// Update zones and related components
for (Zone *zone : zones_)
{
zone->update(targets_);
zone->update(targets_, sensor_available_);
}
}

Expand Down
17 changes: 17 additions & 0 deletions components/LD2450/LD2450.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "esphome/components/button/button.h"
#endif

#define SENSOR_UNAVAILABLE_TIMEOUT 1000

#define COMMAND_MAX_RETRIES 5
#define COMMAND_RETRY_DELAY 100

Expand Down Expand Up @@ -215,6 +217,15 @@ namespace esphome::ld2450
return targets_[i];
}

/**
* @brief Gets the state of the connected sensor
* @return True if the sensor is connected and communicating, False otherwise
*/
bool is_sensor_available()
{
return sensor_available_;
}

/**
* @brief Reads and logs the sensors version number.
*/
Expand Down Expand Up @@ -315,12 +326,18 @@ namespace esphome::ld2450
/// @brief Indicated that the sensor is currently factory resetting
bool is_applying_changes_ = false;

/// @brief indicates if the sensor is communicating
bool sensor_available_ = false;

/// @brief Expected length of the configuration message
int configuration_message_length_ = 0;

/// @brief timestamp of the last message which was sent to the sensor
long command_last_sent_ = 0;

/// @brief timestamp of the last received message
uint32_t last_message_received_ = 0;

/// @brief Queue of commands to execute
std::vector<std::vector<uint8_t>> command_queue_;

Expand Down
15 changes: 14 additions & 1 deletion components/LD2450/zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,21 @@ namespace esphome::ld2450
#endif
}

void Zone::update(std::vector<Target *> &targets)
void Zone::update(std::vector<Target *> &targets, bool sensor_available)
{
if (!sensor_available)
{
#ifdef USE_BINARY_SENSOR
if (occupancy_binary_sensor_ != nullptr)
occupancy_binary_sensor_->publish_state(false);
#endif
#ifdef USE_SENSOR
if (target_count_sensor_ != nullptr)
target_count_sensor_->publish_state(NAN);
#endif
return;
}

if (polygon_.size() < 3)
return;

Expand Down
3 changes: 2 additions & 1 deletion components/LD2450/zone.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ namespace esphome::ld2450
/**
* @brief Updates sensors related to this zone.
* @param targets Reference to a vector of targets which will be used for calculation
* @param available True if the sensor is currently available, false otherwise
* */
void update(std::vector<Target *> &targets);
void update(std::vector<Target *> &targets, bool sensor_available);

/**
* Logs the Zone configuration.
Expand Down

0 comments on commit 304f28c

Please sign in to comment.