mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Use find_coordinates in waze_travel_time (#61400)
This commit is contained in:
parent
9c28727aa0
commit
49a5c7b2cc
@ -172,7 +172,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
or await self.hass.async_add_executor_job(
|
or await self.hass.async_add_executor_job(
|
||||||
is_valid_config_entry,
|
is_valid_config_entry,
|
||||||
self.hass,
|
self.hass,
|
||||||
_LOGGER,
|
|
||||||
user_input[CONF_ORIGIN],
|
user_input[CONF_ORIGIN],
|
||||||
user_input[CONF_DESTINATION],
|
user_input[CONF_DESTINATION],
|
||||||
user_input[CONF_REGION],
|
user_input[CONF_REGION],
|
||||||
|
@ -1,70 +1,15 @@
|
|||||||
"""Helpers for Waze Travel Time integration."""
|
"""Helpers for Waze Travel Time integration."""
|
||||||
import re
|
|
||||||
|
|
||||||
from WazeRouteCalculator import WazeRouteCalculator, WRCError
|
from WazeRouteCalculator import WazeRouteCalculator, WRCError
|
||||||
|
|
||||||
from homeassistant.components.waze_travel_time.const import ENTITY_ID_PATTERN
|
from homeassistant.helpers.location import find_coordinates
|
||||||
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE
|
|
||||||
from homeassistant.helpers import location
|
|
||||||
|
|
||||||
|
|
||||||
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."""
|
"""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)
|
||||||
try:
|
try:
|
||||||
WazeRouteCalculator(origin, destination, region).calc_all_routes_info()
|
WazeRouteCalculator(origin, destination, region).calc_all_routes_info()
|
||||||
except WRCError:
|
except WRCError:
|
||||||
return False
|
return False
|
||||||
return True
|
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)}"
|
|
||||||
|
@ -25,6 +25,7 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
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
|
||||||
from homeassistant.helpers.typing import DiscoveryInfoType
|
from homeassistant.helpers.typing import DiscoveryInfoType
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -50,7 +51,6 @@ from .const import (
|
|||||||
UNITS,
|
UNITS,
|
||||||
VEHICLE_TYPES,
|
VEHICLE_TYPES,
|
||||||
)
|
)
|
||||||
from .helpers import get_location_from_entity, resolve_zone
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -237,24 +237,14 @@ class WazeTravelTime(SensorEntity):
|
|||||||
_LOGGER.debug("Fetching Route for %s", self._attr_name)
|
_LOGGER.debug("Fetching Route for %s", self._attr_name)
|
||||||
# Get origin latitude and longitude from entity_id.
|
# Get origin latitude and longitude from entity_id.
|
||||||
if self._origin_entity_id is not None:
|
if self._origin_entity_id is not None:
|
||||||
self._waze_data.origin = get_location_from_entity(
|
self._waze_data.origin = find_coordinates(self.hass, self._origin_entity_id)
|
||||||
self.hass, _LOGGER, self._origin_entity_id
|
|
||||||
)
|
|
||||||
|
|
||||||
# Get destination latitude and longitude from entity_id.
|
# Get destination latitude and longitude from entity_id.
|
||||||
if self._destination_entity_id is not None:
|
if self._destination_entity_id is not None:
|
||||||
self._waze_data.destination = get_location_from_entity(
|
self._waze_data.destination = find_coordinates(
|
||||||
self.hass, _LOGGER, self._destination_entity_id
|
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()
|
self._waze_data.update()
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user