From 6167e4178b9a04d6d63a6ceaadba101fd2cc8062 Mon Sep 17 00:00:00 2001 From: Kevin Stillhammer Date: Mon, 29 Nov 2021 16:54:03 +0100 Subject: [PATCH] Use find_coordinates in here_travel_time (#59938) --- .../components/here_travel_time/sensor.py | 60 ++----------------- .../here_travel_time/test_sensor.py | 5 +- 2 files changed, 9 insertions(+), 56 deletions(-) diff --git a/homeassistant/components/here_travel_time/sensor.py b/homeassistant/components/here_travel_time/sensor.py index 1d47bbaf89f..3277d737a8e 100644 --- a/homeassistant/components/here_travel_time/sensor.py +++ b/homeassistant/components/here_travel_time/sensor.py @@ -10,8 +10,6 @@ import voluptuous as vol 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, @@ -22,10 +20,10 @@ from homeassistant.const import ( 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 @@ -313,63 +311,15 @@ class HERETravelTimeSensor(SensorEntity): """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.""" diff --git a/tests/components/here_travel_time/test_sensor.py b/tests/components/here_travel_time/test_sensor.py index ce0c2d9ca6d..b67e24dfc18 100644 --- a/tests/components/here_travel_time/test_sensor.py +++ b/tests/components/here_travel_time/test_sensor.py @@ -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):