From 397538fd1a1cd1b9ec3596324c29c39742187573 Mon Sep 17 00:00:00 2001 From: Kevin Stillhammer Date: Fri, 7 Jan 2022 17:45:29 +0100 Subject: [PATCH] Use find_coordinates in google_travel_time (#61423) --- .../google_travel_time/config_flow.py | 1 - .../components/google_travel_time/helpers.py | 61 ++----------------- .../components/google_travel_time/sensor.py | 13 +--- 3 files changed, 7 insertions(+), 68 deletions(-) diff --git a/homeassistant/components/google_travel_time/config_flow.py b/homeassistant/components/google_travel_time/config_flow.py index 39425f01d84..f2e23b02cc0 100644 --- a/homeassistant/components/google_travel_time/config_flow.py +++ b/homeassistant/components/google_travel_time/config_flow.py @@ -130,7 +130,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): if await self.hass.async_add_executor_job( is_valid_config_entry, self.hass, - _LOGGER, user_input[CONF_API_KEY], user_input[CONF_ORIGIN], user_input[CONF_DESTINATION], diff --git a/homeassistant/components/google_travel_time/helpers.py b/homeassistant/components/google_travel_time/helpers.py index f295bceb65d..dc8fc32af7a 100644 --- a/homeassistant/components/google_travel_time/helpers.py +++ b/homeassistant/components/google_travel_time/helpers.py @@ -3,69 +3,16 @@ from googlemaps import Client from googlemaps.distance_matrix import distance_matrix from googlemaps.exceptions import ApiError -from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE -from homeassistant.helpers import location - -from .const import TRACKABLE_DOMAINS +from homeassistant.helpers.location import find_coordinates -def is_valid_config_entry(hass, logger, api_key, origin, destination): +def is_valid_config_entry(hass, api_key, origin, destination): """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) client = Client(api_key, timeout=10) try: distance_matrix(client, origin, destination, mode="driving") except ApiError: return False return True - - -def resolve_location(hass, logger, loc): - """Resolve a location.""" - if loc.split(".", 1)[0] in TRACKABLE_DOMAINS: - 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 state or attributes.""" - if (entity := 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(entity): - return get_location_from_attributes(entity) - - # Check if device is in a zone - zone_entity = hass.states.get(f"zone.{entity.state}") - if location.has_location(zone_entity): - logger.debug( - "%s is in %s, getting zone location", entity_id, zone_entity.entity_id - ) - return get_location_from_attributes(zone_entity) - - # If zone was not found in state then use the state as the location - if entity_id.startswith("sensor."): - return entity.state - - # When everything fails just return nothing - return None - - -def get_location_from_attributes(entity): - """Get the lat/long string from an entities attributes.""" - attr = entity.attributes - return f"{attr.get(ATTR_LATITUDE)},{attr.get(ATTR_LONGITUDE)}" - - -def resolve_zone(hass, friendly_name): - """Resolve a location from a zone's friendly name.""" - entities = hass.states.all() - for entity in entities: - if entity.domain == "zone" and entity.name == friendly_name: - return get_location_from_attributes(entity) - - return friendly_name diff --git a/homeassistant/components/google_travel_time/sensor.py b/homeassistant/components/google_travel_time/sensor.py index 08104619bb9..6960361d9a1 100644 --- a/homeassistant/components/google_travel_time/sensor.py +++ b/homeassistant/components/google_travel_time/sensor.py @@ -21,6 +21,7 @@ from homeassistant.core import CoreState, HomeAssistant 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 import homeassistant.util.dt as dt_util from .const import ( @@ -36,7 +37,6 @@ from .const import ( DOMAIN, TRACKABLE_DOMAINS, ) -from .helpers import get_location_from_entity, resolve_zone _LOGGER = logging.getLogger(__name__) @@ -213,17 +213,10 @@ class GoogleTravelTimeSensor(SensorEntity): # Convert device_trackers to google friendly location if hasattr(self, "_origin_entity_id"): - self._origin = get_location_from_entity( - self.hass, _LOGGER, self._origin_entity_id - ) + self._origin = find_coordinates(self.hass, self._origin_entity_id) if hasattr(self, "_destination_entity_id"): - self._destination = get_location_from_entity( - self.hass, _LOGGER, self._destination_entity_id - ) - - self._destination = resolve_zone(self.hass, self._destination) - self._origin = resolve_zone(self.hass, self._origin) + self._destination = find_coordinates(self.hass, self._destination_entity_id) if self._destination is not None and self._origin is not None: self._matrix = distance_matrix(