Improve type hints in template helper (#136253)

pull/136229/head^2
epenet 2025-01-22 16:27:01 +01:00 committed by GitHub
parent 7a78f87fa6
commit 3bbd7daa7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 14 deletions

View File

@ -1735,7 +1735,7 @@ def label_entities(hass: HomeAssistant, label_id_or_name: str) -> Iterable[str]:
return [entry.entity_id for entry in entries]
def closest(hass, *args):
def closest(hass: HomeAssistant, *args: Any) -> State | None:
"""Find closest entity.
Closest to home:
@ -1775,21 +1775,24 @@ def closest(hass, *args):
)
return None
latitude = point_state.attributes.get(ATTR_LATITUDE)
longitude = point_state.attributes.get(ATTR_LONGITUDE)
latitude = point_state.attributes[ATTR_LATITUDE]
longitude = point_state.attributes[ATTR_LONGITUDE]
entities = args[1]
else:
latitude = convert(args[0], float)
longitude = convert(args[1], float)
latitude_arg = convert(args[0], float)
longitude_arg = convert(args[1], float)
if latitude is None or longitude is None:
if latitude_arg is None or longitude_arg is None:
_LOGGER.warning(
"Closest:Received invalid coordinates: %s, %s", args[0], args[1]
)
return None
latitude = latitude_arg
longitude = longitude_arg
entities = args[2]
states = expand(hass, entities)
@ -1798,20 +1801,20 @@ def closest(hass, *args):
return loc_helper.closest(latitude, longitude, states)
def closest_filter(hass, *args):
def closest_filter(hass: HomeAssistant, *args: Any) -> State | None:
"""Call closest as a filter. Need to reorder arguments."""
new_args = list(args[1:])
new_args.append(args[0])
return closest(hass, *new_args)
def distance(hass, *args):
def distance(hass: HomeAssistant, *args: Any) -> float | None:
"""Calculate distance.
Will calculate distance from home to a point or between points.
Points can be passed in using state objects or lat/lng coordinates.
"""
locations = []
locations: list[tuple[float, float]] = []
to_process = list(args)
@ -1831,10 +1834,10 @@ def distance(hass, *args):
return None
value_2 = to_process.pop(0)
latitude = convert(value, float)
longitude = convert(value_2, float)
latitude_to_process = convert(value, float)
longitude_to_process = convert(value_2, float)
if latitude is None or longitude is None:
if latitude_to_process is None or longitude_to_process is None:
_LOGGER.warning(
"Distance:Unable to process latitude and longitude: %s, %s",
value,
@ -1842,6 +1845,9 @@ def distance(hass, *args):
)
return None
latitude = latitude_to_process
longitude = longitude_to_process
else:
if not loc_helper.has_location(point_state):
_LOGGER.warning(
@ -1849,8 +1855,8 @@ def distance(hass, *args):
)
return None
latitude = point_state.attributes.get(ATTR_LATITUDE)
longitude = point_state.attributes.get(ATTR_LONGITUDE)
latitude = point_state.attributes[ATTR_LATITUDE]
longitude = point_state.attributes[ATTR_LONGITUDE]
locations.append((latitude, longitude))