Skip to content

Commit

Permalink
Waze calc_route_info -> calc_routes HA 2024.4 #16
Browse files Browse the repository at this point in the history
  • Loading branch information
rodpayne committed Apr 22, 2024
1 parent 0aba642 commit 017ae43
Showing 1 changed file with 141 additions and 107 deletions.
248 changes: 141 additions & 107 deletions custom_components/person_location/reverse_geocode.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,136 @@ def calculate_initial_compass_bearing(pointA, pointB):

return compass_bearing

def _get_waze_driving_miles_and_minutes(
target,
new_latitude,
new_longitude,
):
"""
Figured it out from:
https://github.com/home-assistant/core/blob/dev/homeassistant/components/waze_travel_time/sensor.py
https://github.com/kovacsbalu/WazeRouteCalculator
https://github.com/home-assistant/core/pull/108613/files
https://github.com/home-assistant/home-assistant.io/pull/32062
Updates target.attributes:
ATTR_DRIVING_MILES
ATTR_DRIVING_MINUTES
ATTR_ATTRIBUTION
May update pli.attributes:
"waze_error_count"
"""

entity_id = target.entity_id
if not pli.configuration["use_waze"]:
return
if (
target.attributes[ATTR_METERS_FROM_HOME]
< WAZE_MIN_METERS_FROM_HOME
):
target.attributes[
ATTR_DRIVING_MILES
] = target.attributes[ATTR_MILES_FROM_HOME]
target.attributes[ATTR_DRIVING_MINUTES] = "0"
return

try:
_LOGGER.debug(
"(" + entity_id + ") Waze calculation"
)

async def async_get_waze_route(
from_location,
to_location,
waze_region,
):
client = WazeRouteCalculator(
region=waze_region,
client=get_async_client(pli.hass),
)
# route = await client.calc_route_info(
routes = await client.calc_routes(
from_location,
to_location,
avoid_toll_roads=True,
)
_LOGGER.debug(f"Waze route = {routes}")

if len(routes) < 1:
return 0, 0

route = routes[0]
return route.duration, route.distance

from_location = (
str(new_latitude) + "," + str(new_longitude)
)
to_location = (
str(pli.attributes["home_latitude"])
+ ","
+ str(pli.attributes["home_longitude"])
)
route_time, route_distance = asyncio.run_coroutine_threadsafe(
async_get_waze_route(
from_location,
to_location,
pli.configuration["waze_region"].upper(),
), pli.hass.loop
).result()
_LOGGER.debug(
"("
+ entity_id
+ ") Waze route_distance "
+ str(route_distance)
) # km
route_distance = (
route_distance * METERS_PER_KM / METERS_PER_MILE
) # miles
if route_distance <= 0:
target.attributes[
ATTR_DRIVING_MILES
] = target.attributes[ATTR_MILES_FROM_HOME]
elif route_distance >= 100:
target.attributes[ATTR_DRIVING_MILES] = str(
round(route_distance, 0)
)
elif route_distance >= 10:
target.attributes[ATTR_DRIVING_MILES] = str(
round(route_distance, 1)
)
else:
target.attributes[ATTR_DRIVING_MILES] = str(
round(route_distance, 2)
)
_LOGGER.debug(
"("
+ entity_id
+ ") Waze route_time "
+ str(route_time)
) # minutes
target.attributes[ATTR_DRIVING_MINUTES] = str(
round(route_time, 1)
)
target.attributes[ATTR_ATTRIBUTION] += (
'"Data by Waze App. https://waze.com"; '
)
except Exception as e:
_LOGGER.error(
"("
+ entity_id
+ ") Waze Exception "
+ type(e).__name__
+ ": "
+ str(e)
)
_LOGGER.debug(traceback.format_exc())
pli.attributes["waze_error_count"] += 1

target.attributes[
ATTR_DRIVING_MILES
] = target.attributes[ATTR_MILES_FROM_HOME]

def handle_reverse_geocode(call):
"""
Handle the reverse_geocode service.
Expand Down Expand Up @@ -210,7 +340,8 @@ def handle_reverse_geocode(call):
_LOGGER.debug("TARGET_LOCK obtained")

target = PERSON_LOCATION_ENTITY(entity_id, pli)
attribution = ""
target.entity_id = entity_id
target.attributes[ATTR_ATTRIBUTION] = ""

if ATTR_LATITUDE in target.attributes:
new_latitude = target.attributes[ATTR_LATITUDE]
Expand Down Expand Up @@ -481,7 +612,8 @@ def handle_reverse_geocode(call):

if "licence" in osm_decoded:
osm_attribution = '"' + osm_decoded["licence"] + '"'
attribution += osm_attribution + "; "
target.attributes[ATTR_ATTRIBUTION] += osm_attribution + "; "

else:
osm_attribution = ""

Expand Down Expand Up @@ -580,7 +712,7 @@ def handle_reverse_geocode(call):
locality = component["long_name"]

google_attribution = '"powered by Google"'
attribution += google_attribution + "; "
target.attributes[ATTR_ATTRIBUTION] += google_attribution + "; "

if (
ATTR_GEOCODED
Expand Down Expand Up @@ -725,7 +857,7 @@ def handle_reverse_geocode(call):
]
+ '"'
)
attribution += mapquest_attribution + "; "
target.attributes[ATTR_ATTRIBUTION] += mapquest_attribution + "; "

if (
ATTR_GEOCODED
Expand Down Expand Up @@ -785,109 +917,11 @@ def handle_reverse_geocode(call):
target.attributes[ATTR_BREAD_CRUMBS] = newBreadCrumb

# Call WazeRouteCalculator if not at Home:
if not pli.configuration["use_waze"]:
pass
elif (
target.attributes[ATTR_METERS_FROM_HOME]
< WAZE_MIN_METERS_FROM_HOME
):
target.attributes[
ATTR_DRIVING_MILES
] = target.attributes[ATTR_MILES_FROM_HOME]
target.attributes[ATTR_DRIVING_MINUTES] = "0"
else:
try:
"""
Figure it out from:
https://github.com/home-assistant/core/blob/dev/homeassistant/components/waze_travel_time/sensor.py
https://github.com/kovacsbalu/WazeRouteCalculator
"""
_LOGGER.debug(
"(" + entity_id + ") Waze calculation"
)

async def async_get_waze_route (
from_location,
to_location,
waze_region,
):
client = WazeRouteCalculator(
region=waze_region,
client=get_async_client(pli.hass),
)
route = await client.calc_route_info(
from_location,
to_location,
avoid_toll_roads=True,
)
return route

from_location = (
str(new_latitude) + "," + str(new_longitude)
)
to_location = (
str(pli.attributes["home_latitude"])
+ ","
+ str(pli.attributes["home_longitude"])
)
routeTime, routeDistance = asyncio.run_coroutine_threadsafe(
async_get_waze_route(
from_location,
to_location,
pli.configuration["waze_region"].upper(),
# "US",
),pli.hass.loop
).result()
_LOGGER.debug(
"("
+ entity_id
+ ") Waze routeDistance "
+ str(routeDistance)
) # km
routeDistance = (
routeDistance * METERS_PER_KM / METERS_PER_MILE
) # miles
if routeDistance >= 100:
target.attributes[ATTR_DRIVING_MILES] = str(
round(routeDistance, 0)
)
elif routeDistance >= 10:
target.attributes[ATTR_DRIVING_MILES] = str(
round(routeDistance, 1)
)
else:
target.attributes[ATTR_DRIVING_MILES] = str(
round(routeDistance, 2)
)
_LOGGER.debug(
"("
+ entity_id
+ ") Waze routeTime "
+ str(routeTime)
) # minutes
target.attributes[ATTR_DRIVING_MINUTES] = str(
round(routeTime, 1)
)
attribution += (
'"Data by Waze App. https://waze.com"; '
)
except Exception as e:
_LOGGER.error(
"("
+ entity_id
+ ") Waze Exception "
+ type(e).__name__
+ ": "
+ str(e)
)
_LOGGER.debug(traceback.format_exc())
pli.attributes["waze_error_count"] += 1

target.attributes[
ATTR_DRIVING_MILES
] = target.attributes[ATTR_MILES_FROM_HOME]

target.attributes[ATTR_ATTRIBUTION] = attribution
_get_waze_driving_miles_and_minutes(
target,
new_latitude,
new_longitude,
)

target.set_state()

Expand Down

0 comments on commit 017ae43

Please sign in to comment.