Fix zone radius calculation when radius is not 0 (#110354)

pull/110720/head
J. Nick Koston 2024-02-12 12:47:34 -06:00 committed by Franck Nijhof
parent 56ceadaeeb
commit de619e4ddc
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 42 additions and 2 deletions

View File

@ -135,7 +135,7 @@ def async_active_zone(
is None
# Skip zone that are outside the radius aka the
# lat/long is outside the zone
or not (zone_dist - (radius := zone_attrs[ATTR_RADIUS]) < radius)
or not (zone_dist - (zone_radius := zone_attrs[ATTR_RADIUS]) < radius)
):
continue
@ -144,7 +144,7 @@ def async_active_zone(
zone_dist < min_dist
or (
# If same distance, prefer smaller zone
zone_dist == min_dist and radius < closest.attributes[ATTR_RADIUS]
zone_dist == min_dist and zone_radius < closest.attributes[ATTR_RADIUS]
)
):
continue

View File

@ -228,6 +228,46 @@ async def test_in_zone_works_for_passive_zones(hass: HomeAssistant) -> None:
assert zone.in_zone(hass.states.get("zone.passive_zone"), latitude, longitude)
async def test_async_active_zone_with_non_zero_radius(
hass: HomeAssistant,
) -> None:
"""Test async_active_zone with a non-zero radius."""
latitude = 32.880600
longitude = -117.237561
assert await setup.async_setup_component(
hass,
zone.DOMAIN,
{
"zone": [
{
"name": "Small Zone",
"latitude": 32.980600,
"longitude": -117.137561,
"radius": 50000,
},
{
"name": "Big Zone",
"latitude": 32.980600,
"longitude": -117.137561,
"radius": 100000,
},
]
},
)
home_state = hass.states.get("zone.home")
assert home_state.attributes["radius"] == 100
assert home_state.attributes["latitude"] == 32.87336
assert home_state.attributes["longitude"] == -117.22743
active = zone.async_active_zone(hass, latitude, longitude, 5000)
assert active.entity_id == "zone.home"
active = zone.async_active_zone(hass, latitude, longitude, 0)
assert active.entity_id == "zone.small_zone"
async def test_core_config_update(hass: HomeAssistant) -> None:
"""Test updating core config will update home zone."""
assert await setup.async_setup_component(hass, "zone", {})