mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Improve type hints in template helper (#136253)
This commit is contained in:
parent
7a78f87fa6
commit
3bbd7daa7f
@ -1735,7 +1735,7 @@ def label_entities(hass: HomeAssistant, label_id_or_name: str) -> Iterable[str]:
|
|||||||
return [entry.entity_id for entry in entries]
|
return [entry.entity_id for entry in entries]
|
||||||
|
|
||||||
|
|
||||||
def closest(hass, *args):
|
def closest(hass: HomeAssistant, *args: Any) -> State | None:
|
||||||
"""Find closest entity.
|
"""Find closest entity.
|
||||||
|
|
||||||
Closest to home:
|
Closest to home:
|
||||||
@ -1775,21 +1775,24 @@ def closest(hass, *args):
|
|||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
latitude = point_state.attributes.get(ATTR_LATITUDE)
|
latitude = point_state.attributes[ATTR_LATITUDE]
|
||||||
longitude = point_state.attributes.get(ATTR_LONGITUDE)
|
longitude = point_state.attributes[ATTR_LONGITUDE]
|
||||||
|
|
||||||
entities = args[1]
|
entities = args[1]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
latitude = convert(args[0], float)
|
latitude_arg = convert(args[0], float)
|
||||||
longitude = convert(args[1], 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(
|
_LOGGER.warning(
|
||||||
"Closest:Received invalid coordinates: %s, %s", args[0], args[1]
|
"Closest:Received invalid coordinates: %s, %s", args[0], args[1]
|
||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
latitude = latitude_arg
|
||||||
|
longitude = longitude_arg
|
||||||
|
|
||||||
entities = args[2]
|
entities = args[2]
|
||||||
|
|
||||||
states = expand(hass, entities)
|
states = expand(hass, entities)
|
||||||
@ -1798,20 +1801,20 @@ def closest(hass, *args):
|
|||||||
return loc_helper.closest(latitude, longitude, states)
|
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."""
|
"""Call closest as a filter. Need to reorder arguments."""
|
||||||
new_args = list(args[1:])
|
new_args = list(args[1:])
|
||||||
new_args.append(args[0])
|
new_args.append(args[0])
|
||||||
return closest(hass, *new_args)
|
return closest(hass, *new_args)
|
||||||
|
|
||||||
|
|
||||||
def distance(hass, *args):
|
def distance(hass: HomeAssistant, *args: Any) -> float | None:
|
||||||
"""Calculate distance.
|
"""Calculate distance.
|
||||||
|
|
||||||
Will calculate distance from home to a point or between points.
|
Will calculate distance from home to a point or between points.
|
||||||
Points can be passed in using state objects or lat/lng coordinates.
|
Points can be passed in using state objects or lat/lng coordinates.
|
||||||
"""
|
"""
|
||||||
locations = []
|
locations: list[tuple[float, float]] = []
|
||||||
|
|
||||||
to_process = list(args)
|
to_process = list(args)
|
||||||
|
|
||||||
@ -1831,10 +1834,10 @@ def distance(hass, *args):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
value_2 = to_process.pop(0)
|
value_2 = to_process.pop(0)
|
||||||
latitude = convert(value, float)
|
latitude_to_process = convert(value, float)
|
||||||
longitude = convert(value_2, 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(
|
_LOGGER.warning(
|
||||||
"Distance:Unable to process latitude and longitude: %s, %s",
|
"Distance:Unable to process latitude and longitude: %s, %s",
|
||||||
value,
|
value,
|
||||||
@ -1842,6 +1845,9 @@ def distance(hass, *args):
|
|||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
latitude = latitude_to_process
|
||||||
|
longitude = longitude_to_process
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if not loc_helper.has_location(point_state):
|
if not loc_helper.has_location(point_state):
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
@ -1849,8 +1855,8 @@ def distance(hass, *args):
|
|||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
latitude = point_state.attributes.get(ATTR_LATITUDE)
|
latitude = point_state.attributes[ATTR_LATITUDE]
|
||||||
longitude = point_state.attributes.get(ATTR_LONGITUDE)
|
longitude = point_state.attributes[ATTR_LONGITUDE]
|
||||||
|
|
||||||
locations.append((latitude, longitude))
|
locations.append((latitude, longitude))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user