Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
HomeAssistant
Core
Commits
4ee6735c
Unverified
Commit
4ee6735c
authored
1 year ago
by
J. Nick Koston
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Small cleanup to zone async_active_zone (#108629)
parent
8d4a1f47
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
homeassistant/components/zone/__init__.py
+36
-26
36 additions, 26 deletions
homeassistant/components/zone/__init__.py
with
36 additions
and
26 deletions
homeassistant/components/zone/__init__.py
+
36
−
26
View file @
4ee6735c
"""
Support for the definition of zones.
"""
from
__future__
import
annotations
from
collections.abc
import
Callable
from
collections.abc
import
Callable
,
Iterable
import
logging
from
operator
import
attrgetter
import
sys
from
typing
import
Any
,
Self
,
cast
import
voluptuous
as
vol
...
...
@@ -109,40 +110,49 @@ def async_active_zone(
This method must be run in the event loop.
"""
# Sort entity IDs so that we are deterministic if equal distance to 2 zones
min_dist
=
None
closest
=
None
min_dist
:
float
=
sys
.
maxsize
closest
:
State
|
None
=
None
# This can be called before async_setup by device tracker
zone_entity_ids
:
list
[
str
]
=
hass
.
data
.
get
(
ZONE_ENTITY_IDS
,
[])
zone_entity_ids
:
Iterable
[
str
]
=
hass
.
data
.
get
(
ZONE_ENTITY_IDS
,
())
for
entity_id
in
zone_entity_ids
:
zone
=
hass
.
states
.
get
(
entity_id
)
if
(
not
zone
not
(
zone
:
=
hass
.
states
.
get
(
entity_id
))
# Skip unavailable zones
or
zone
.
state
==
STATE_UNAVAILABLE
or
zone
.
attributes
.
get
(
ATTR_PASSIVE
)
# Skip passive zones
or
(
zone_attrs
:
=
zone
.
attributes
).
get
(
ATTR_PASSIVE
)
# Skip zones where we cannot calculate distance
or
(
zone_dist
:
=
distance
(
latitude
,
longitude
,
zone_attrs
[
ATTR_LATITUDE
],
zone_attrs
[
ATTR_LONGITUDE
],
)
)
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
)
):
continue
zone_dist
=
distance
(
latitude
,
l
on
gitude
,
zone
.
attributes
[
ATTR_LATITUDE
],
zone
.
attributes
[
ATTR_LONGITUDE
],
)
if
zone_dist
is
None
:
# If have a closest and its not closer than the closest skip it
if
closest
and
not
(
z
on
e_dist
<
min_dist
or
(
# If same distance, prefer smaller zone
zone_dist
==
min_dist
and
radius
<
closest
.
attributes
[
ATTR_RADIUS
]
)
)
:
continue
within_zone
=
zone_dist
-
radius
<
zone
.
attributes
[
ATTR_RADIUS
]
closer_zone
=
closest
is
None
or
zone_dist
<
min_dist
# type: ignore[unreachable]
smaller_zone
=
(
zone_dist
==
min_dist
and
zone
.
attributes
[
ATTR_RADIUS
]
<
cast
(
State
,
closest
).
attributes
[
ATTR_RADIUS
]
)
if
within_zone
and
(
closer_zone
or
smaller_zone
):
min_dist
=
zone_dist
closest
=
zone
# We got here which means it closer than the previous known closest
# or equal distance but this one is smaller.
min_dist
=
zone_dist
closest
=
zone
return
closest
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment