Please check this discussion: home-assistant/architecture#1014
Proof of concept for polygon zones in Home Assistant.
Whipped up quickly, so it needs optimization, refactoring, cleaning up, and possibly a complete rewrite from scratch.
It's based on using shapely
I'll try to upload the editor based on @geoman-io/leaflet-geoman-free when I get some free time.
In short:
- I created a new class called
polygon_zonethat inherits from zone. - I added an attribute named
points. - If the zone has points, then it’s a
polygon_zoneotherwise I use the old class, so I don’t have issues with backward compatibility. - I determine the center of the zone and its area.
- In the
_init_.pyfile, in the in_zone method, I check if the instance has thepointsattribute. If it does, I check if the tracker’spointis within the polygon usingpolygon.contains(point). This is on lines 97 to 104. - Otherwise, I check if the point is within the radius, like for normal zone.