Skip to content

Commit

Permalink
Use find_coordinates in here_travel_time (home-assistant#59938)
Browse files Browse the repository at this point in the history
  • Loading branch information
eifinger authored Nov 29, 2021
1 parent 2be7773 commit 6167e41
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 56 deletions.
60 changes: 5 additions & 55 deletions homeassistant/components/here_travel_time/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import (
ATTR_ATTRIBUTION,
ATTR_LATITUDE,
ATTR_LONGITUDE,
ATTR_MODE,
CONF_API_KEY,
CONF_MODE,
Expand All @@ -22,10 +20,10 @@
EVENT_HOMEASSISTANT_START,
TIME_MINUTES,
)
from homeassistant.core import HomeAssistant, State, callback
from homeassistant.helpers import location
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.location import find_coordinates
from homeassistant.helpers.typing import DiscoveryInfoType
from homeassistant.util import dt

Expand Down Expand Up @@ -313,63 +311,15 @@ async def async_update(self) -> None:
"""Update Sensor Information."""
# Convert device_trackers to HERE friendly location
if self._origin_entity_id is not None:
self._here_data.origin = await self._get_location_from_entity(
self._origin_entity_id
)
self._here_data.origin = find_coordinates(self.hass, self._origin_entity_id)

if self._destination_entity_id is not None:
self._here_data.destination = await self._get_location_from_entity(
self._destination_entity_id
self._here_data.destination = find_coordinates(
self.hass, self._destination_entity_id
)

await self.hass.async_add_executor_job(self._here_data.update)

async def _get_location_from_entity(self, entity_id: str) -> str | None:
"""Get the location from the entity state or attributes."""
if (entity := self.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 self._get_location_from_attributes(entity)

# Check if device is in a zone
zone_entity = self.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 self._get_location_from_attributes(zone_entity)

# Check if state is valid coordinate set
if self._entity_state_is_valid_coordinate_set(entity.state):
return entity.state

_LOGGER.error(
"The state of %s is not a valid set of coordinates: %s",
entity_id,
entity.state,
)
return None

@staticmethod
def _entity_state_is_valid_coordinate_set(state: str) -> bool:
"""Check that the given string is a valid set of coordinates."""
schema = vol.Schema(cv.gps)
try:
coordinates = state.split(",")
schema(coordinates)
return True
except (vol.MultipleInvalid):
return False

@staticmethod
def _get_location_from_attributes(entity: State) -> str:
"""Get the lat/long string from an entities attributes."""
attr = entity.attributes
return f"{attr.get(ATTR_LATITUDE)},{attr.get(ATTR_LONGITUDE)}"


class HERETravelTimeData:
"""HERETravelTime data object."""
Expand Down
5 changes: 4 additions & 1 deletion tests/components/here_travel_time/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,10 @@ async def test_pattern_entity_state(hass, requests_mock_truck_response, caplog):
await hass.async_block_till_done()

assert len(caplog.records) == 1
assert "is not a valid set of coordinates" in caplog.text
assert (
"Entity sensor.origin does not contain a location and does not point at an entity that does: invalid"
in caplog.text
)


async def test_pattern_entity_state_with_space(hass, requests_mock_truck_response):
Expand Down

0 comments on commit 6167e41

Please sign in to comment.