ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
polygon_zone.cpp
Go to the documentation of this file.
1#include "polygon_zone.h"
2#ifdef LD2450_POLYGON_ZONE_COUNT
3
4#include "esphome/core/log.h"
5
6namespace esphome::ld2450 {
7
8ESPHOME_LOG_TAG(TAG, "ld2450.polygon_zone");
9
10void PolygonZone::setup() {
11 // Bump the version when Polygon's layout changes
13 Polygon saved;
14 if (this->pref_.load(&saved)) {
15 if (saved.is_valid()) {
16 this->polygon_ = saved;
17 } else {
18 ESP_LOGW(TAG, "'%s': saved polygon is invalid; zone disabled", LOG_STR_ARG(this->get_log_name()));
19 }
20 }
21 // Presence stays unknown until the first radar frame
22 this->publish_polygon_();
23}
24
26 char polygon_s[POLYGON_STR_SIZE];
27 this->polygon_.format(polygon_s);
28 LOG_TEXT(" ", "PolygonZone", this);
29 ESP_LOGCONFIG(TAG, " Polygon: '%s'", polygon_s);
30 LOG_BINARY_SENSOR(" ", "Presence", this->presence_binary_sensor_);
31}
32
33void PolygonZone::control(const std::string &value) {
34 Polygon polygon;
35 if (!polygon.parse(value.c_str(), value.size())) {
36 ESP_LOGW(TAG, "'%s': invalid polygon '%s'; expected 'x,y;x,y;...' with %u to %u points",
37 LOG_STR_ARG(this->get_log_name()), value.c_str(), MIN_POLYGON_POINTS, MAX_POLYGON_POINTS);
38 } else if (!this->pref_.save(&polygon)) {
39 // On ESP8266, the space for saved settings is small and can run out with several zones
40 ESP_LOGW(TAG, "'%s': could not save the polygon; keeping the previous one", LOG_STR_ARG(this->get_log_name()));
41 } else {
42 this->polygon_ = polygon;
43 // A user-made edit is written to flash now, as wall-powered sensors lose power without warning
45 // A target seen inside the previous polygon must not keep the new one on
46 this->last_inside_ms_ = 0;
47 if (this->polygon_.empty())
48 this->presence_binary_sensor_->invalidate_state();
49 }
50 // On error this publishes the previous polygon again, so the frontend shows the value in use
51 this->publish_polygon_();
52}
53
55 char polygon_s[POLYGON_STR_SIZE];
56 this->polygon_.format(polygon_s);
57 this->publish_state(polygon_s);
58}
59
60} // namespace esphome::ld2450
61
62#endif // LD2450_POLYGON_ZONE_COUNT
friend void::setup()
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
ESPPreferenceObject pref_
void control(const std::string &value) override
void publish_state(const std::string &state)
Definition text.h:25
ESPHOME_LOG_TAG(TAG, "ld2450")
uint32_t value
Definition hal.cpp:41
ESPPreferences * global_preferences
bool sync()
Commit pending writes to flash.
Definition preferences.h:33
A polygon in radar coordinates. Trivially copyable so it can be saved to preferences as-is.
Definition polygon.h:26
void format(char *buf) const
Write the polygon as "x,y;x,y;..." into buf, which must hold at least POLYGON_STR_SIZE bytes.
Definition polygon.cpp:114
bool parse(const char *str, size_t len)
Parse "x,y;x,y;..." (mm) into this polygon.
Definition polygon.cpp:91
bool is_valid() const
True when the polygon is empty, or has a valid number of points that are all inside the radar area.
Definition polygon.cpp:60
bool empty() const
True when the polygon has no points, which disables the zone.
Definition polygon.h:31