Use find_coordinates in google_travel_time (#61423)

This commit is contained in:
Kevin Stillhammer 2022-01-07 17:45:29 +01:00 committed by GitHub
parent fc2025509e
commit 397538fd1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 68 deletions

View File

@ -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],

View File

@ -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

View File

@ -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(