ESPHome 2026.11.0-dev
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
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", this->get_name().c_str());
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_);
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", this->get_name().c_str(),
37 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", this->get_name().c_str());
41 } else {
42 this->polygon_ = polygon;
43 // A target seen inside the previous polygon must not keep the new one on
44 this->last_inside_ms_ = 0;
45 if (this->polygon_.empty())
47 }
48 // On error this publishes the previous polygon again, so the frontend shows the value in use
49 this->publish_polygon_();
50}
51
53 char polygon_s[POLYGON_STR_SIZE];
54 this->polygon_.format(polygon_s);
55 this->publish_state(polygon_s);
56}
57
58} // namespace esphome::ld2450
59
60#endif // LD2450_POLYGON_ZONE_COUNT
const StringRef & get_name() const
Definition entity_base.h:71
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
void invalidate_state()
Clear the state — sets has_state() to false and fires callbacks with nullopt.
void setup()
Load the saved polygon and publish it. Called from the hub's setup().
ESPPreferenceObject pref_
void control(const std::string &value) override
binary_sensor::BinarySensor * presence_
void publish_state(const std::string &state)
Definition text.h:26
ESPHOME_LOG_TAG(TAG, "ld2450")
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