Please check this discussion: home-assistant/architecture#1014
Proof of concept for polygon zones in Home Assistant.
![Zrzut ekranu 2023-10-02 085400](https://private-user-images.githubusercontent.com/38595/271901474-d9de9c49-3155-4856-ad80-8208b4f68527.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk3OTc4MDAsIm5iZiI6MTczOTc5NzUwMCwicGF0aCI6Ii8zODU5NS8yNzE5MDE0NzQtZDlkZTljNDktMzE1NS00ODU2LWFkODAtODIwOGI0ZjY4NTI3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE3VDEzMDUwMFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTMwYmQzMjk5MmZiOWY1NmMxOWE5ODY2OWNjMzQ1NzRmMGFkMTI0YjlkZmE1NzU1ZWQ5YTIwY2Q1MjMwZTA1NjUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.4Oj8_xKErLoiUJPtqGxUoNb7LNeZ8XiBkDKpsDoDt-0)
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_zone
that inherits from zone. - I added an attribute named
points
. - If the zone has points, then it’s a
polygon_zone
otherwise 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_.py
file, in the in_zone method, I check if the instance has thepoints
attribute. If it does, I check if the tracker’spoint
is 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.