Polygonal zones

Hey all,

I was looking into how to create polyogonal zones. Found some discussions about it and a couple of examples in node-red. I even found a proposal to integrate it into hass core. while waiting for a bit to see if there was any traction for it i decided to create a custom integration for it.

Enter my first integration, Polygonal zones. It is lacking in a large amount of features but it supplies a way to configure from the ui and the ability to easely store multiple zones.

The current version makes use of GeoJSON to store the zone information and shapely to check in which zone you are. there are a large amount of improvements that can still be made. The current version is a proof of concept i hope you guys can help me test.

Installing/Configuring the integration

installing the integration is as simple as copying the custom_components/polygonal_zones folder into your config/custom_components folder and following the configuration flow.

However before following the integration flow you need to create a geojson file accessible for a homeassistant via the web. the easiest place i found is to just place it into your config/www folder. once you have done that you can simple configure the integration via the ui. the first field is for the url of the GeoJSON file and the second is the entities to track.

How it works

Once you have the integration configured it will create a sensor for each entity you selected. it will update this every time the location or accuracy of the tracked device changes.

What makes it different

Not much differentiates it form the different implementations there are but the biggest difference i can find is that it takes into account the accuracy of the gps signal. all other options i found just check the latitude and longitude. however that didnt work for me as i was using some realy inprecise gps trackers. besides that it isnt implemented in node red and is actualy functional

Limitations / TODO

There are still a large amount of things to improve. the biggest limitation currently is you cant track a single entity multiple times. besides that there are not tests for the code. also it might not work on armhf as i heard that shapely doesn’t support it(current is only tested on a intel nuc).

If you are interested in this please dont just use this but contribute to the discussion/proposal to get it integrated into core Allow zone to be a polygon · home-assistant/architecture · Discussion #1014 · GitHub. if you want to improve this my integration feel free to open a pr/issue.

3 Likes

@MichelGerding Great idea for polygonal zones, love to have this implemented.

Trying to test the integration with some google map device_tracker entities.

I created and saved the GeoJSON file using geojson.io as map.geojson in config/www folder.
Configuration of Polygon Zones integration went fine without error but the entity created had unknown state.

Reloading the integration showed ‘Failed to set up’, log had this:

2024-09-03 22:20:22.726 ERROR (MainThread) [homeassistant.components.sensor] Error adding entity sensor.polygonal_zone_tracker_device_tracker_google_maps_[device name] for domain sensor with platform polygonal_zones
Traceback (most recent call last):
File “/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/entity_platform.py”, line 912, in _async_add_entity
await entity.add_to_platform_finish()
File “/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/entity.py”, line 1365, in add_to_platform_finish
await self.async_added_to_hass()
File “/home/homeassistant/.homeassistant/custom_components/polygonal_zones/sensor.py”, line 91, in async_added_to_hass
self._zones = self.hass.data[DOMAIN][self._config_entry_id][DATA_ZONES]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
TypeError: ‘ConfigEntry’ object is not subscriptable

Would you please help figure out what’s going on?
TIA