mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Use find_coordinates in google_travel_time (#61423)
This commit is contained in:
parent
fc2025509e
commit
397538fd1a
@ -130,7 +130,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
if await self.hass.async_add_executor_job(
|
if await self.hass.async_add_executor_job(
|
||||||
is_valid_config_entry,
|
is_valid_config_entry,
|
||||||
self.hass,
|
self.hass,
|
||||||
_LOGGER,
|
|
||||||
user_input[CONF_API_KEY],
|
user_input[CONF_API_KEY],
|
||||||
user_input[CONF_ORIGIN],
|
user_input[CONF_ORIGIN],
|
||||||
user_input[CONF_DESTINATION],
|
user_input[CONF_DESTINATION],
|
||||||
|
@ -3,69 +3,16 @@ from googlemaps import Client
|
|||||||
from googlemaps.distance_matrix import distance_matrix
|
from googlemaps.distance_matrix import distance_matrix
|
||||||
from googlemaps.exceptions import ApiError
|
from googlemaps.exceptions import ApiError
|
||||||
|
|
||||||
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE
|
from homeassistant.helpers.location import find_coordinates
|
||||||
from homeassistant.helpers import location
|
|
||||||
|
|
||||||
from .const import TRACKABLE_DOMAINS
|
|
||||||
|
|
||||||
|
|
||||||
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."""
|
"""Return whether the config entry data is valid."""
|
||||||
origin = resolve_location(hass, logger, origin)
|
origin = find_coordinates(hass, origin)
|
||||||
destination = resolve_location(hass, logger, destination)
|
destination = find_coordinates(hass, destination)
|
||||||
client = Client(api_key, timeout=10)
|
client = Client(api_key, timeout=10)
|
||||||
try:
|
try:
|
||||||
distance_matrix(client, origin, destination, mode="driving")
|
distance_matrix(client, origin, destination, mode="driving")
|
||||||
except ApiError:
|
except ApiError:
|
||||||
return False
|
return False
|
||||||
return True
|
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
|
|
||||||
|
@ -21,6 +21,7 @@ from homeassistant.core import CoreState, HomeAssistant
|
|||||||
from homeassistant.helpers.device_registry import DeviceEntryType
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.location import find_coordinates
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -36,7 +37,6 @@ from .const import (
|
|||||||
DOMAIN,
|
DOMAIN,
|
||||||
TRACKABLE_DOMAINS,
|
TRACKABLE_DOMAINS,
|
||||||
)
|
)
|
||||||
from .helpers import get_location_from_entity, resolve_zone
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -213,17 +213,10 @@ class GoogleTravelTimeSensor(SensorEntity):
|
|||||||
|
|
||||||
# Convert device_trackers to google friendly location
|
# Convert device_trackers to google friendly location
|
||||||
if hasattr(self, "_origin_entity_id"):
|
if hasattr(self, "_origin_entity_id"):
|
||||||
self._origin = get_location_from_entity(
|
self._origin = find_coordinates(self.hass, self._origin_entity_id)
|
||||||
self.hass, _LOGGER, self._origin_entity_id
|
|
||||||
)
|
|
||||||
|
|
||||||
if hasattr(self, "_destination_entity_id"):
|
if hasattr(self, "_destination_entity_id"):
|
||||||
self._destination = get_location_from_entity(
|
self._destination = find_coordinates(self.hass, self._destination_entity_id)
|
||||||
self.hass, _LOGGER, self._destination_entity_id
|
|
||||||
)
|
|
||||||
|
|
||||||
self._destination = resolve_zone(self.hass, self._destination)
|
|
||||||
self._origin = resolve_zone(self.hass, self._origin)
|
|
||||||
|
|
||||||
if self._destination is not None and self._origin is not None:
|
if self._destination is not None and self._origin is not None:
|
||||||
self._matrix = distance_matrix(
|
self._matrix = distance_matrix(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user