Use find_coordinates in waze_travel_time (#61400)

This commit is contained in:
Kevin Stillhammer 2021-12-10 08:46:44 +01:00 committed by GitHub
parent 9c28727aa0
commit 49a5c7b2cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 74 deletions

View File

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

View File

@ -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)}"

View File

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