From 017ae430c697069fe8d4463e598de9a72d2b5ba3 Mon Sep 17 00:00:00 2001 From: Rod Payne Date: Sun, 21 Apr 2024 21:14:57 -0600 Subject: [PATCH] Waze calc_route_info -> calc_routes HA 2024.4 #16 --- .../person_location/reverse_geocode.py | 248 ++++++++++-------- 1 file changed, 141 insertions(+), 107 deletions(-) diff --git a/custom_components/person_location/reverse_geocode.py b/custom_components/person_location/reverse_geocode.py index e1bcda1..28947d5 100644 --- a/custom_components/person_location/reverse_geocode.py +++ b/custom_components/person_location/reverse_geocode.py @@ -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. @@ -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] @@ -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 = "" @@ -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 @@ -725,7 +857,7 @@ def handle_reverse_geocode(call): ] + '"' ) - attribution += mapquest_attribution + "; " + target.attributes[ATTR_ATTRIBUTION] += mapquest_attribution + "; " if ( ATTR_GEOCODED @@ -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()