From 49a5c7b2cc13777667cd46aee95b904905a61414 Mon Sep 17 00:00:00 2001 From: Kevin Stillhammer Date: Fri, 10 Dec 2021 08:46:44 +0100 Subject: [PATCH] Use find_coordinates in waze_travel_time (#61400) --- .../waze_travel_time/config_flow.py | 1 - .../components/waze_travel_time/helpers.py | 63 ++----------------- .../components/waze_travel_time/sensor.py | 18 ++---- 3 files changed, 8 insertions(+), 74 deletions(-) diff --git a/homeassistant/components/waze_travel_time/config_flow.py b/homeassistant/components/waze_travel_time/config_flow.py index 54097ad37bd..d040f595fd6 100644 --- a/homeassistant/components/waze_travel_time/config_flow.py +++ b/homeassistant/components/waze_travel_time/config_flow.py @@ -172,7 +172,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): or await self.hass.async_add_executor_job( is_valid_config_entry, self.hass, - _LOGGER, user_input[CONF_ORIGIN], user_input[CONF_DESTINATION], user_input[CONF_REGION], diff --git a/homeassistant/components/waze_travel_time/helpers.py b/homeassistant/components/waze_travel_time/helpers.py index 26e529b8e93..67d8b5674b2 100644 --- a/homeassistant/components/waze_travel_time/helpers.py +++ b/homeassistant/components/waze_travel_time/helpers.py @@ -1,70 +1,15 @@ """Helpers for Waze Travel Time integration.""" -import re - from WazeRouteCalculator import WazeRouteCalculator, WRCError -from homeassistant.components.waze_travel_time.const import ENTITY_ID_PATTERN -from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE -from homeassistant.helpers import location +from homeassistant.helpers.location import find_coordinates -def is_valid_config_entry(hass, logger, origin, destination, region): +def is_valid_config_entry(hass, origin, destination, region): """Return whether the config entry data is valid.""" - origin = resolve_location(hass, logger, origin) - destination = resolve_location(hass, logger, destination) + origin = find_coordinates(hass, origin) + destination = find_coordinates(hass, destination) try: WazeRouteCalculator(origin, destination, region).calc_all_routes_info() except WRCError: return False return True - - -def resolve_location(hass, logger, loc): - """Resolve a location.""" - if re.fullmatch(ENTITY_ID_PATTERN, loc): - return get_location_from_entity(hass, logger, loc) - - return resolve_zone(hass, loc) - - -def get_location_from_entity(hass, logger, entity_id): - """Get the location from the entity_id.""" - if (state := hass.states.get(entity_id)) is None: - logger.error("Unable to find entity %s", entity_id) - return None - - # Check if the entity has location attributes. - if location.has_location(state): - logger.debug("Getting %s location", entity_id) - return _get_location_from_attributes(state) - - # Check if device is inside a zone. - zone_state = hass.states.get(f"zone.{state.state}") - if location.has_location(zone_state): - logger.debug( - "%s is in %s, getting zone location", entity_id, zone_state.entity_id - ) - return _get_location_from_attributes(zone_state) - - # If zone was not found in state then use the state as the location. - if entity_id.startswith("sensor."): - return state.state - - # When everything fails just return nothing. - return None - - -def resolve_zone(hass, friendly_name): - """Get a lat/long from a zones friendly_name.""" - states = hass.states.all() - for state in states: - if state.domain == "zone" and state.name == friendly_name: - return _get_location_from_attributes(state) - - return friendly_name - - -def _get_location_from_attributes(state): - """Get the lat/long string from an states attributes.""" - attr = state.attributes - return f"{attr.get(ATTR_LATITUDE)},{attr.get(ATTR_LONGITUDE)}" diff --git a/homeassistant/components/waze_travel_time/sensor.py b/homeassistant/components/waze_travel_time/sensor.py index 9df95daf9ee..3c6dba586d1 100644 --- a/homeassistant/components/waze_travel_time/sensor.py +++ b/homeassistant/components/waze_travel_time/sensor.py @@ -25,6 +25,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.location import find_coordinates from homeassistant.helpers.typing import DiscoveryInfoType from .const import ( @@ -50,7 +51,6 @@ from .const import ( UNITS, VEHICLE_TYPES, ) -from .helpers import get_location_from_entity, resolve_zone _LOGGER = logging.getLogger(__name__) @@ -237,24 +237,14 @@ class WazeTravelTime(SensorEntity): _LOGGER.debug("Fetching Route for %s", self._attr_name) # Get origin latitude and longitude from entity_id. if self._origin_entity_id is not None: - self._waze_data.origin = get_location_from_entity( - self.hass, _LOGGER, self._origin_entity_id - ) + self._waze_data.origin = find_coordinates(self.hass, self._origin_entity_id) # Get destination latitude and longitude from entity_id. if self._destination_entity_id is not None: - self._waze_data.destination = get_location_from_entity( - self.hass, _LOGGER, self._destination_entity_id + self._waze_data.destination = find_coordinates( + self.hass, self._destination_entity_id ) - # Get origin from zone name. - self._waze_data.origin = resolve_zone(self.hass, self._waze_data.origin) - - # Get destination from zone name. - self._waze_data.destination = resolve_zone( - self.hass, self._waze_data.destination - ) - self._waze_data.update()