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

This commit is contained in:
J. Nick Koston 2024-02-12 12:47:34 -06:00 committed by GitHub
parent 4bcfa9e315
commit 29146326fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 2 deletions

View File

@ -135,7 +135,7 @@ def async_active_zone(
is None is None
# Skip zone that are outside the radius aka the # Skip zone that are outside the radius aka the
# lat/long is outside the zone # 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 continue
@ -144,7 +144,7 @@ def async_active_zone(
zone_dist < min_dist zone_dist < min_dist
or ( or (
# If same distance, prefer smaller zone # 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 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) 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: async def test_core_config_update(hass: HomeAssistant) -> None:
"""Test updating core config will update home zone.""" """Test updating core config will update home zone."""
assert await setup.async_setup_component(hass, "zone", {}) assert await setup.async_setup_component(hass, "zone", {})