Skip to content

Commit 636e54a

Browse files
committed
Fix proximity distance calculation
The distance is now calculated to the edge of the zone instead of the centre
1 parent 13fe2a9 commit 636e54a

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

homeassistant/components/proximity/coordinator.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,21 @@ def _calc_distance_to_zone(
164164
)
165165
return None
166166

167-
distance_to_zone = distance(
167+
distance_to_centre = distance(
168168
zone.attributes[ATTR_LATITUDE],
169169
zone.attributes[ATTR_LONGITUDE],
170170
latitude,
171171
longitude,
172172
)
173173

174174
# it is ensured, that distance can't be None, since zones must have lat/lon coordinates
175-
assert distance_to_zone is not None
176-
return round(distance_to_zone)
175+
assert distance_to_centre is not None
176+
177+
zone_radius: float = zone.attributes["radius"]
178+
distance_to_edge: int = round(distance_to_centre - zone_radius)
179+
# return 0 if distance to edge is negative as that means the device is located inside the zone
180+
# but the device's current zone is a different one than the zone in this distance calculation
181+
return max(distance_to_edge, 0)
177182

178183
def _calc_direction_of_travel(
179184
self,

0 commit comments

Comments
 (0)