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.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwNDQyMDgsIm5iZiI6MTcyNDA0MzkwOCwicGF0aCI6Ii8zODU5NS8yNzE5MDE0NzQtZDlkZTljNDktMzE1NS00ODU2LWFkODAtODIwOGI0ZjY4NTI3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODE5VDA1MDUwOFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWJlZjU3OWM0N2VkOWE1YTI5ODgwYmFjNWU2NjljN2RiYzQ4OTlmNjQxNzExZmE0ZDMxYzc2MzM3NmE3MmJmZWUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.wuElnj2cs33o2TGXjUBmzRa_0BYQGvW2EL1B1CzbDY0)
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.