Add here_travel_time (#24603)
* Add here_travel_time * Bump herepy version to 0.6.2 * Update requirements_all.txt * Disable pylint and catch errors * Add herepy to requirements_test_all * Correctly place test req for herepy * use homeassistant.const.LENGTH_METERS * Implemented Requested Changes * Better error message for cryptic error code * add requested changes * add_entities instead of async * Add route attr and distance in km instead of m * fix linting errors * attribute duration in minutes instead of seconds * Correct pattern for longitude * dont split attribute but rather local var * move strings to const and use travelTime * Add tests * Add route for pedestrian and public * fix public transport route generation * remove print statement * Standalone pytest * Use hass fixture and increase test cov _resolve_zone is redundant * Clean up redundant code * Add type annotations * Readd _resolve_zone and add a test for it * Full test cov * use caplog * Add origin/destination attributes According to https://github.com/home-assistant/home-assistant/pull/24956 * Add mode: bicycle * black * Add mode: publicTransportTimeTable * Fix error for publicTransportTimeTable Switch route_mode and travel_mode in api request. * split up config options * More type hints * implement *_entity_id * align attributes with google_travel_time * route in lib apply requested changes * Update requirements_all.txt * remove DATA_KEY * Use ATTR_MODE * add attribution * Only add attribution if not none * Add debug log for raw response * Add _build_hass_attribution * clearer var names in credentials check * async _are_valid_client_credentials
This commit is contained in:
1 parent
5c7f869f9b
commit
5c0fa35d4a
20 files changed
+3911
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Tests for here_travel_time component."""
|
||||
@@ -0,0 +1,947 @@
|
||||
"""The test for the here_travel_time sensor platform."""
|
||||
import logging
|
||||
from unittest.mock import patch
|
||||
import urllib
|
||||
|
||||
import herepy
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.here_travel_time.sensor import (
|
||||
ATTR_ATTRIBUTION,
|
||||
ATTR_DESTINATION,
|
||||
ATTR_DESTINATION_NAME,
|
||||
ATTR_DISTANCE,
|
||||
ATTR_DURATION,
|
||||
ATTR_DURATION_IN_TRAFFIC,
|
||||
ATTR_ORIGIN,
|
||||
ATTR_ORIGIN_NAME,
|
||||
ATTR_ROUTE,
|
||||
CONF_MODE,
|
||||
CONF_TRAFFIC_MODE,
|
||||
CONF_UNIT_SYSTEM,
|
||||
ICON_BICYCLE,
|
||||
ICON_CAR,
|
||||
ICON_PEDESTRIAN,
|
||||
ICON_PUBLIC,
|
||||
ICON_TRUCK,
|
||||
NO_ROUTE_ERROR_MESSAGE,
|
||||
ROUTE_MODE_FASTEST,
|
||||
ROUTE_MODE_SHORTEST,
|
||||
SCAN_INTERVAL,
|
||||
TRAFFIC_MODE_DISABLED,
|
||||
TRAFFIC_MODE_ENABLED,
|
||||
TRAVEL_MODE_BICYCLE,
|
||||
TRAVEL_MODE_CAR,
|
||||
TRAVEL_MODE_PEDESTRIAN,
|
||||
TRAVEL_MODE_PUBLIC,
|
||||
TRAVEL_MODE_PUBLIC_TIME_TABLE,
|
||||
TRAVEL_MODE_TRUCK,
|
||||
UNIT_OF_MEASUREMENT,
|
||||
)
|
||||
from homeassistant.const import ATTR_ICON
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import async_fire_time_changed, load_fixture
|
||||
|
||||
DOMAIN = "sensor"
|
||||
|
||||
PLATFORM = "here_travel_time"
|
||||
|
||||
APP_ID = "test"
|
||||
APP_CODE = "test"
|
||||
|
||||
TRUCK_ORIGIN_LATITUDE = "41.9798"
|
||||
TRUCK_ORIGIN_LONGITUDE = "-87.8801"
|
||||
TRUCK_DESTINATION_LATITUDE = "41.9043"
|
||||
TRUCK_DESTINATION_LONGITUDE = "-87.9216"
|
||||
|
||||
BIKE_ORIGIN_LATITUDE = "41.9798"
|
||||
BIKE_ORIGIN_LONGITUDE = "-87.8801"
|
||||
BIKE_DESTINATION_LATITUDE = "41.9043"
|
||||
BIKE_DESTINATION_LONGITUDE = "-87.9216"
|
||||
|
||||
CAR_ORIGIN_LATITUDE = "38.9"
|
||||
CAR_ORIGIN_LONGITUDE = "-77.04833"
|
||||
CAR_DESTINATION_LATITUDE = "39.0"
|
||||
CAR_DESTINATION_LONGITUDE = "-77.1"
|
||||
|
||||
|
||||
def _build_mock_url(origin, destination, modes, app_id, app_code, departure):
|
||||
"""Construct a url for HERE."""
|
||||
base_url = "https://route.cit.api.here.com/routing/7.2/calculateroute.json?"
|
||||
parameters = {
|
||||
"waypoint0": origin,
|
||||
"waypoint1": destination,
|
||||
"mode": ";".join(str(herepy.RouteMode[mode]) for mode in modes),
|
||||
"app_id": app_id,
|
||||
"app_code": app_code,
|
||||
"departure": departure,
|
||||
}
|
||||
url = base_url + urllib.parse.urlencode(parameters)
|
||||
return url
|
||||
|
||||
|
||||
def _assert_truck_sensor(sensor):
|
||||
"""Assert that states and attributes are correct for truck_response."""
|
||||
assert sensor.state == "14"
|
||||
assert sensor.attributes.get("unit_of_measurement") == UNIT_OF_MEASUREMENT
|
||||
|
||||
assert sensor.attributes.get(ATTR_ATTRIBUTION) is None
|
||||
assert sensor.attributes.get(ATTR_DURATION) == 13.533333333333333
|
||||
assert sensor.attributes.get(ATTR_DISTANCE) == 13.049
|
||||
assert sensor.attributes.get(ATTR_ROUTE) == (
|
||||
"I-190; I-294 S - Tri-State Tollway; I-290 W - Eisenhower Expy W; "
|
||||
"IL-64 W - E North Ave; I-290 E - Eisenhower Expy E; I-290"
|
||||
)
|
||||
assert sensor.attributes.get(CONF_UNIT_SYSTEM) == "metric"
|
||||
assert sensor.attributes.get(ATTR_DURATION_IN_TRAFFIC) == 13.533333333333333
|
||||
assert sensor.attributes.get(ATTR_ORIGIN) == ",".join(
|
||||
[TRUCK_ORIGIN_LATITUDE, TRUCK_ORIGIN_LONGITUDE]
|
||||
)
|
||||
assert sensor.attributes.get(ATTR_DESTINATION) == ",".join(
|
||||
[TRUCK_DESTINATION_LATITUDE, TRUCK_DESTINATION_LONGITUDE]
|
||||
)
|
||||
assert sensor.attributes.get(ATTR_ORIGIN_NAME) == ""
|
||||
assert sensor.attributes.get(ATTR_DESTINATION_NAME) == "Eisenhower Expy E"
|
||||
assert sensor.attributes.get(CONF_MODE) == TRAVEL_MODE_TRUCK
|
||||
assert sensor.attributes.get(CONF_TRAFFIC_MODE) is False
|
||||
|
||||
assert sensor.attributes.get(ATTR_ICON) == ICON_TRUCK
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def requests_mock_credentials_check(requests_mock):
|
||||
"""Add the url used in the api validation to all requests mock."""
|
||||
modes = [ROUTE_MODE_FASTEST, TRAVEL_MODE_CAR, TRAFFIC_MODE_DISABLED]
|
||||
response_url = _build_mock_url(
|
||||
",".join([CAR_ORIGIN_LATITUDE, CAR_ORIGIN_LONGITUDE]),
|
||||
",".join([CAR_DESTINATION_LATITUDE, CAR_DESTINATION_LONGITUDE]),
|
||||
modes,
|
||||
APP_ID,
|
||||
APP_CODE,
|
||||
"now",
|
||||
)
|
||||
requests_mock.get(
|
||||
response_url, text=load_fixture("here_travel_time/car_response.json")
|
||||
)
|
||||
return requests_mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def requests_mock_truck_response(requests_mock_credentials_check):
|
||||
"""Return a requests_mock for truck respones."""
|
||||
modes = [ROUTE_MODE_FASTEST, TRAVEL_MODE_TRUCK, TRAFFIC_MODE_DISABLED]
|
||||
response_url = _build_mock_url(
|
||||
",".join([TRUCK_ORIGIN_LATITUDE, TRUCK_ORIGIN_LONGITUDE]),
|
||||
",".join([TRUCK_DESTINATION_LATITUDE, TRUCK_DESTINATION_LONGITUDE]),
|
||||
modes,
|
||||
APP_ID,
|
||||
APP_CODE,
|
||||
"now",
|
||||
)
|
||||
requests_mock_credentials_check.get(
|
||||
response_url, text=load_fixture("here_travel_time/truck_response.json")
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def requests_mock_car_disabled_response(requests_mock_credentials_check):
|
||||
"""Return a requests_mock for truck respones."""
|
||||
modes = [ROUTE_MODE_FASTEST, TRAVEL_MODE_CAR, TRAFFIC_MODE_DISABLED]
|
||||
response_url = _build_mock_url(
|
||||
",".join([CAR_ORIGIN_LATITUDE, CAR_ORIGIN_LONGITUDE]),
|
||||
",".join([CAR_DESTINATION_LATITUDE, CAR_DESTINATION_LONGITUDE]),
|
||||
modes,
|
||||
APP_ID,
|
||||
APP_CODE,
|
||||
"now",
|
||||
)
|
||||
requests_mock_credentials_check.get(
|
||||
response_url, text=load_fixture("here_travel_time/car_response.json")
|
||||
)
|
||||
|
||||
|
||||
async def test_car(hass, requests_mock_car_disabled_response):
|
||||
"""Test that car works."""
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": CAR_ORIGIN_LATITUDE,
|
||||
"origin_longitude": CAR_ORIGIN_LONGITUDE,
|
||||
"destination_latitude": CAR_DESTINATION_LATITUDE,
|
||||
"destination_longitude": CAR_DESTINATION_LONGITUDE,
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
}
|
||||
}
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
assert sensor.state == "30"
|
||||
assert sensor.attributes.get("unit_of_measurement") == UNIT_OF_MEASUREMENT
|
||||
|
||||
assert sensor.attributes.get(ATTR_ATTRIBUTION) is None
|
||||
assert sensor.attributes.get(ATTR_DURATION) == 30.05
|
||||
assert sensor.attributes.get(ATTR_DISTANCE) == 23.903
|
||||
assert sensor.attributes.get(ATTR_ROUTE) == (
|
||||
"US-29 - K St NW; US-29 - Whitehurst Fwy; "
|
||||
"I-495 N - Capital Beltway; MD-187 S - Old Georgetown Rd"
|
||||
)
|
||||
assert sensor.attributes.get(CONF_UNIT_SYSTEM) == "metric"
|
||||
assert sensor.attributes.get(ATTR_DURATION_IN_TRAFFIC) == 31.016666666666666
|
||||
assert sensor.attributes.get(ATTR_ORIGIN) == ",".join(
|
||||
[CAR_ORIGIN_LATITUDE, CAR_ORIGIN_LONGITUDE]
|
||||
)
|
||||
assert sensor.attributes.get(ATTR_DESTINATION) == ",".join(
|
||||
[CAR_DESTINATION_LATITUDE, CAR_DESTINATION_LONGITUDE]
|
||||
)
|
||||
assert sensor.attributes.get(ATTR_ORIGIN_NAME) == "22nd St NW"
|
||||
assert sensor.attributes.get(ATTR_DESTINATION_NAME) == "Service Rd S"
|
||||
assert sensor.attributes.get(CONF_MODE) == TRAVEL_MODE_CAR
|
||||
assert sensor.attributes.get(CONF_TRAFFIC_MODE) is False
|
||||
|
||||
assert sensor.attributes.get(ATTR_ICON) == ICON_CAR
|
||||
|
||||
# Test traffic mode disabled
|
||||
assert sensor.attributes.get(ATTR_DURATION) != sensor.attributes.get(
|
||||
ATTR_DURATION_IN_TRAFFIC
|
||||
)
|
||||
|
||||
|
||||
async def test_traffic_mode_enabled(hass, requests_mock_credentials_check):
|
||||
"""Test that traffic mode enabled works."""
|
||||
modes = [ROUTE_MODE_FASTEST, TRAVEL_MODE_CAR, TRAFFIC_MODE_ENABLED]
|
||||
response_url = _build_mock_url(
|
||||
",".join([CAR_ORIGIN_LATITUDE, CAR_ORIGIN_LONGITUDE]),
|
||||
",".join([CAR_DESTINATION_LATITUDE, CAR_DESTINATION_LONGITUDE]),
|
||||
modes,
|
||||
APP_ID,
|
||||
APP_CODE,
|
||||
"now",
|
||||
)
|
||||
requests_mock_credentials_check.get(
|
||||
response_url, text=load_fixture("here_travel_time/car_enabled_response.json")
|
||||
)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": CAR_ORIGIN_LATITUDE,
|
||||
"origin_longitude": CAR_ORIGIN_LONGITUDE,
|
||||
"destination_latitude": CAR_DESTINATION_LATITUDE,
|
||||
"destination_longitude": CAR_DESTINATION_LONGITUDE,
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"traffic_mode": True,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
|
||||
# Test traffic mode enabled
|
||||
assert sensor.attributes.get(ATTR_DURATION) != sensor.attributes.get(
|
||||
ATTR_DURATION_IN_TRAFFIC
|
||||
)
|
||||
|
||||
|
||||
async def test_imperial(hass, requests_mock_car_disabled_response):
|
||||
"""Test that imperial units work."""
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": CAR_ORIGIN_LATITUDE,
|
||||
"origin_longitude": CAR_ORIGIN_LONGITUDE,
|
||||
"destination_latitude": CAR_DESTINATION_LATITUDE,
|
||||
"destination_longitude": CAR_DESTINATION_LONGITUDE,
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"unit_system": "imperial",
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
assert sensor.attributes.get(ATTR_DISTANCE) == 14.852635608048994
|
||||
|
||||
|
||||
async def test_route_mode_shortest(hass, requests_mock_credentials_check):
|
||||
"""Test that route mode shortest works."""
|
||||
origin = "38.902981,-77.048338"
|
||||
destination = "39.042158,-77.119116"
|
||||
modes = [ROUTE_MODE_SHORTEST, TRAVEL_MODE_CAR, TRAFFIC_MODE_DISABLED]
|
||||
response_url = _build_mock_url(origin, destination, modes, APP_ID, APP_CODE, "now")
|
||||
requests_mock_credentials_check.get(
|
||||
response_url, text=load_fixture("here_travel_time/car_shortest_response.json")
|
||||
)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": origin.split(",")[0],
|
||||
"origin_longitude": origin.split(",")[1],
|
||||
"destination_latitude": destination.split(",")[0],
|
||||
"destination_longitude": destination.split(",")[1],
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"route_mode": ROUTE_MODE_SHORTEST,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
assert sensor.attributes.get(ATTR_DISTANCE) == 18.388
|
||||
|
||||
|
||||
async def test_route_mode_fastest(hass, requests_mock_credentials_check):
|
||||
"""Test that route mode fastest works."""
|
||||
origin = "38.902981,-77.048338"
|
||||
destination = "39.042158,-77.119116"
|
||||
modes = [ROUTE_MODE_FASTEST, TRAVEL_MODE_CAR, TRAFFIC_MODE_ENABLED]
|
||||
response_url = _build_mock_url(origin, destination, modes, APP_ID, APP_CODE, "now")
|
||||
requests_mock_credentials_check.get(
|
||||
response_url, text=load_fixture("here_travel_time/car_enabled_response.json")
|
||||
)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": origin.split(",")[0],
|
||||
"origin_longitude": origin.split(",")[1],
|
||||
"destination_latitude": destination.split(",")[0],
|
||||
"destination_longitude": destination.split(",")[1],
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"traffic_mode": True,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
assert sensor.attributes.get(ATTR_DISTANCE) == 23.381
|
||||
|
||||
|
||||
async def test_truck(hass, requests_mock_truck_response):
|
||||
"""Test that truck works."""
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": TRUCK_ORIGIN_LATITUDE,
|
||||
"origin_longitude": TRUCK_ORIGIN_LONGITUDE,
|
||||
"destination_latitude": TRUCK_DESTINATION_LATITUDE,
|
||||
"destination_longitude": TRUCK_DESTINATION_LONGITUDE,
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"mode": TRAVEL_MODE_TRUCK,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
sensor = hass.states.get("sensor.test")
|
||||
_assert_truck_sensor(sensor)
|
||||
|
||||
|
||||
async def test_public_transport(hass, requests_mock_credentials_check):
|
||||
"""Test that publicTransport works."""
|
||||
origin = "41.9798,-87.8801"
|
||||
destination = "41.9043,-87.9216"
|
||||
modes = [ROUTE_MODE_FASTEST, TRAVEL_MODE_PUBLIC, TRAFFIC_MODE_DISABLED]
|
||||
response_url = _build_mock_url(origin, destination, modes, APP_ID, APP_CODE, "now")
|
||||
requests_mock_credentials_check.get(
|
||||
response_url, text=load_fixture("here_travel_time/public_response.json")
|
||||
)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": origin.split(",")[0],
|
||||
"origin_longitude": origin.split(",")[1],
|
||||
"destination_latitude": destination.split(",")[0],
|
||||
"destination_longitude": destination.split(",")[1],
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"mode": TRAVEL_MODE_PUBLIC,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
assert sensor.state == "89"
|
||||
assert sensor.attributes.get("unit_of_measurement") == UNIT_OF_MEASUREMENT
|
||||
|
||||
assert sensor.attributes.get(ATTR_ATTRIBUTION) is None
|
||||
assert sensor.attributes.get(ATTR_DURATION) == 89.16666666666667
|
||||
assert sensor.attributes.get(ATTR_DISTANCE) == 22.325
|
||||
assert sensor.attributes.get(ATTR_ROUTE) == (
|
||||
"332 - Palmer/Schiller; 332 - Cargo Rd./Delta Cargo; " "332 - Palmer/Schiller"
|
||||
)
|
||||
assert sensor.attributes.get(CONF_UNIT_SYSTEM) == "metric"
|
||||
assert sensor.attributes.get(ATTR_DURATION_IN_TRAFFIC) == 89.16666666666667
|
||||
assert sensor.attributes.get(ATTR_ORIGIN) == origin
|
||||
assert sensor.attributes.get(ATTR_DESTINATION) == destination
|
||||
assert sensor.attributes.get(ATTR_ORIGIN_NAME) == "Mannheim Rd"
|
||||
assert sensor.attributes.get(ATTR_DESTINATION_NAME) == ""
|
||||
assert sensor.attributes.get(CONF_MODE) == TRAVEL_MODE_PUBLIC
|
||||
assert sensor.attributes.get(CONF_TRAFFIC_MODE) is False
|
||||
|
||||
assert sensor.attributes.get(ATTR_ICON) == ICON_PUBLIC
|
||||
|
||||
|
||||
async def test_public_transport_time_table(hass, requests_mock_credentials_check):
|
||||
"""Test that publicTransportTimeTable works."""
|
||||
origin = "41.9798,-87.8801"
|
||||
destination = "41.9043,-87.9216"
|
||||
modes = [ROUTE_MODE_FASTEST, TRAVEL_MODE_PUBLIC_TIME_TABLE, TRAFFIC_MODE_DISABLED]
|
||||
response_url = _build_mock_url(origin, destination, modes, APP_ID, APP_CODE, "now")
|
||||
requests_mock_credentials_check.get(
|
||||
response_url,
|
||||
text=load_fixture("here_travel_time/public_time_table_response.json"),
|
||||
)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": origin.split(",")[0],
|
||||
"origin_longitude": origin.split(",")[1],
|
||||
"destination_latitude": destination.split(",")[0],
|
||||
"destination_longitude": destination.split(",")[1],
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"mode": TRAVEL_MODE_PUBLIC_TIME_TABLE,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
assert sensor.state == "80"
|
||||
assert sensor.attributes.get("unit_of_measurement") == UNIT_OF_MEASUREMENT
|
||||
|
||||
assert sensor.attributes.get(ATTR_ATTRIBUTION) is None
|
||||
assert sensor.attributes.get(ATTR_DURATION) == 79.73333333333333
|
||||
assert sensor.attributes.get(ATTR_DISTANCE) == 14.775
|
||||
assert sensor.attributes.get(ATTR_ROUTE) == (
|
||||
"330 - Archer/Harlem (Terminal); 309 - Elmhurst Metra Station"
|
||||
)
|
||||
assert sensor.attributes.get(CONF_UNIT_SYSTEM) == "metric"
|
||||
assert sensor.attributes.get(ATTR_DURATION_IN_TRAFFIC) == 79.73333333333333
|
||||
assert sensor.attributes.get(ATTR_ORIGIN) == origin
|
||||
assert sensor.attributes.get(ATTR_DESTINATION) == destination
|
||||
assert sensor.attributes.get(ATTR_ORIGIN_NAME) == "Mannheim Rd"
|
||||
assert sensor.attributes.get(ATTR_DESTINATION_NAME) == ""
|
||||
assert sensor.attributes.get(CONF_MODE) == TRAVEL_MODE_PUBLIC_TIME_TABLE
|
||||
assert sensor.attributes.get(CONF_TRAFFIC_MODE) is False
|
||||
|
||||
assert sensor.attributes.get(ATTR_ICON) == ICON_PUBLIC
|
||||
|
||||
|
||||
async def test_pedestrian(hass, requests_mock_credentials_check):
|
||||
"""Test that pedestrian works."""
|
||||
origin = "41.9798,-87.8801"
|
||||
destination = "41.9043,-87.9216"
|
||||
modes = [ROUTE_MODE_FASTEST, TRAVEL_MODE_PEDESTRIAN, TRAFFIC_MODE_DISABLED]
|
||||
response_url = _build_mock_url(origin, destination, modes, APP_ID, APP_CODE, "now")
|
||||
requests_mock_credentials_check.get(
|
||||
response_url, text=load_fixture("here_travel_time/pedestrian_response.json")
|
||||
)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": origin.split(",")[0],
|
||||
"origin_longitude": origin.split(",")[1],
|
||||
"destination_latitude": destination.split(",")[0],
|
||||
"destination_longitude": destination.split(",")[1],
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"mode": TRAVEL_MODE_PEDESTRIAN,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
assert sensor.state == "211"
|
||||
assert sensor.attributes.get("unit_of_measurement") == UNIT_OF_MEASUREMENT
|
||||
|
||||
assert sensor.attributes.get(ATTR_ATTRIBUTION) is None
|
||||
assert sensor.attributes.get(ATTR_DURATION) == 210.51666666666668
|
||||
assert sensor.attributes.get(ATTR_DISTANCE) == 12.533
|
||||
assert sensor.attributes.get(ATTR_ROUTE) == (
|
||||
"Mannheim Rd; W Belmont Ave; Cullerton St; E Fullerton Ave; "
|
||||
"La Porte Ave; E Palmer Ave; N Railroad Ave; W North Ave; "
|
||||
"E North Ave; E Third St"
|
||||
)
|
||||
assert sensor.attributes.get(CONF_UNIT_SYSTEM) == "metric"
|
||||
assert sensor.attributes.get(ATTR_DURATION_IN_TRAFFIC) == 210.51666666666668
|
||||
assert sensor.attributes.get(ATTR_ORIGIN) == origin
|
||||
assert sensor.attributes.get(ATTR_DESTINATION) == destination
|
||||
assert sensor.attributes.get(ATTR_ORIGIN_NAME) == "Mannheim Rd"
|
||||
assert sensor.attributes.get(ATTR_DESTINATION_NAME) == ""
|
||||
assert sensor.attributes.get(CONF_MODE) == TRAVEL_MODE_PEDESTRIAN
|
||||
assert sensor.attributes.get(CONF_TRAFFIC_MODE) is False
|
||||
|
||||
assert sensor.attributes.get(ATTR_ICON) == ICON_PEDESTRIAN
|
||||
|
||||
|
||||
async def test_bicycle(hass, requests_mock_credentials_check):
|
||||
"""Test that bicycle works."""
|
||||
origin = "41.9798,-87.8801"
|
||||
destination = "41.9043,-87.9216"
|
||||
modes = [ROUTE_MODE_FASTEST, TRAVEL_MODE_BICYCLE, TRAFFIC_MODE_DISABLED]
|
||||
response_url = _build_mock_url(origin, destination, modes, APP_ID, APP_CODE, "now")
|
||||
requests_mock_credentials_check.get(
|
||||
response_url, text=load_fixture("here_travel_time/bike_response.json")
|
||||
)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": origin.split(",")[0],
|
||||
"origin_longitude": origin.split(",")[1],
|
||||
"destination_latitude": destination.split(",")[0],
|
||||
"destination_longitude": destination.split(",")[1],
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"mode": TRAVEL_MODE_BICYCLE,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
assert sensor.state == "55"
|
||||
assert sensor.attributes.get("unit_of_measurement") == UNIT_OF_MEASUREMENT
|
||||
|
||||
assert sensor.attributes.get(ATTR_ATTRIBUTION) is None
|
||||
assert sensor.attributes.get(ATTR_DURATION) == 54.86666666666667
|
||||
assert sensor.attributes.get(ATTR_DISTANCE) == 12.613
|
||||
assert sensor.attributes.get(ATTR_ROUTE) == (
|
||||
"Mannheim Rd; W Belmont Ave; Cullerton St; N Landen Dr; "
|
||||
"E Fullerton Ave; N Wolf Rd; W North Ave; N Clinton Ave; "
|
||||
"E Third St; N Caroline Ave"
|
||||
)
|
||||
assert sensor.attributes.get(CONF_UNIT_SYSTEM) == "metric"
|
||||
assert sensor.attributes.get(ATTR_DURATION_IN_TRAFFIC) == 54.86666666666667
|
||||
assert sensor.attributes.get(ATTR_ORIGIN) == origin
|
||||
assert sensor.attributes.get(ATTR_DESTINATION) == destination
|
||||
assert sensor.attributes.get(ATTR_ORIGIN_NAME) == "Mannheim Rd"
|
||||
assert sensor.attributes.get(ATTR_DESTINATION_NAME) == ""
|
||||
assert sensor.attributes.get(CONF_MODE) == TRAVEL_MODE_BICYCLE
|
||||
assert sensor.attributes.get(CONF_TRAFFIC_MODE) is False
|
||||
|
||||
assert sensor.attributes.get(ATTR_ICON) == ICON_BICYCLE
|
||||
|
||||
|
||||
async def test_location_zone(hass, requests_mock_truck_response):
|
||||
"""Test that origin/destination supplied by a zone works."""
|
||||
utcnow = dt_util.utcnow()
|
||||
# Patching 'utcnow' to gain more control over the timed update.
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=utcnow):
|
||||
zone_config = {
|
||||
"zone": [
|
||||
{
|
||||
"name": "Destination",
|
||||
"latitude": TRUCK_DESTINATION_LATITUDE,
|
||||
"longitude": TRUCK_DESTINATION_LONGITUDE,
|
||||
"radius": 250,
|
||||
"passive": False,
|
||||
},
|
||||
{
|
||||
"name": "Origin",
|
||||
"latitude": TRUCK_ORIGIN_LATITUDE,
|
||||
"longitude": TRUCK_ORIGIN_LONGITUDE,
|
||||
"radius": 250,
|
||||
"passive": False,
|
||||
},
|
||||
]
|
||||
}
|
||||
assert await async_setup_component(hass, "zone", zone_config)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_entity_id": "zone.origin",
|
||||
"destination_entity_id": "zone.destination",
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"mode": TRAVEL_MODE_TRUCK,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
_assert_truck_sensor(sensor)
|
||||
|
||||
# Test that update works more than once
|
||||
async_fire_time_changed(hass, utcnow + SCAN_INTERVAL)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
_assert_truck_sensor(sensor)
|
||||
|
||||
|
||||
async def test_location_sensor(hass, requests_mock_truck_response):
|
||||
"""Test that origin/destination supplied by a sensor works."""
|
||||
utcnow = dt_util.utcnow()
|
||||
# Patching 'utcnow' to gain more control over the timed update.
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=utcnow):
|
||||
hass.states.async_set(
|
||||
"sensor.origin", ",".join([TRUCK_ORIGIN_LATITUDE, TRUCK_ORIGIN_LONGITUDE])
|
||||
)
|
||||
hass.states.async_set(
|
||||
"sensor.destination",
|
||||
",".join([TRUCK_DESTINATION_LATITUDE, TRUCK_DESTINATION_LONGITUDE]),
|
||||
)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_entity_id": "sensor.origin",
|
||||
"destination_entity_id": "sensor.destination",
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"mode": TRAVEL_MODE_TRUCK,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
_assert_truck_sensor(sensor)
|
||||
|
||||
# Test that update works more than once
|
||||
async_fire_time_changed(hass, utcnow + SCAN_INTERVAL)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
_assert_truck_sensor(sensor)
|
||||
|
||||
|
||||
async def test_location_person(hass, requests_mock_truck_response):
|
||||
"""Test that origin/destination supplied by a person works."""
|
||||
utcnow = dt_util.utcnow()
|
||||
# Patching 'utcnow' to gain more control over the timed update.
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=utcnow):
|
||||
hass.states.async_set(
|
||||
"person.origin",
|
||||
"unknown",
|
||||
{
|
||||
"latitude": float(TRUCK_ORIGIN_LATITUDE),
|
||||
"longitude": float(TRUCK_ORIGIN_LONGITUDE),
|
||||
},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"person.destination",
|
||||
"unknown",
|
||||
{
|
||||
"latitude": float(TRUCK_DESTINATION_LATITUDE),
|
||||
"longitude": float(TRUCK_DESTINATION_LONGITUDE),
|
||||
},
|
||||
)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_entity_id": "person.origin",
|
||||
"destination_entity_id": "person.destination",
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"mode": TRAVEL_MODE_TRUCK,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
_assert_truck_sensor(sensor)
|
||||
|
||||
# Test that update works more than once
|
||||
async_fire_time_changed(hass, utcnow + SCAN_INTERVAL)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
_assert_truck_sensor(sensor)
|
||||
|
||||
|
||||
async def test_location_device_tracker(hass, requests_mock_truck_response):
|
||||
"""Test that origin/destination supplied by a device_tracker works."""
|
||||
utcnow = dt_util.utcnow()
|
||||
# Patching 'utcnow' to gain more control over the timed update.
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=utcnow):
|
||||
hass.states.async_set(
|
||||
"device_tracker.origin",
|
||||
"unknown",
|
||||
{
|
||||
"latitude": float(TRUCK_ORIGIN_LATITUDE),
|
||||
"longitude": float(TRUCK_ORIGIN_LONGITUDE),
|
||||
},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"device_tracker.destination",
|
||||
"unknown",
|
||||
{
|
||||
"latitude": float(TRUCK_DESTINATION_LATITUDE),
|
||||
"longitude": float(TRUCK_DESTINATION_LONGITUDE),
|
||||
},
|
||||
)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_entity_id": "device_tracker.origin",
|
||||
"destination_entity_id": "device_tracker.destination",
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"mode": TRAVEL_MODE_TRUCK,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
_assert_truck_sensor(sensor)
|
||||
|
||||
# Test that update works more than once
|
||||
async_fire_time_changed(hass, utcnow + SCAN_INTERVAL)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
_assert_truck_sensor(sensor)
|
||||
|
||||
|
||||
async def test_location_device_tracker_added_after_update(
|
||||
hass, requests_mock_truck_response, caplog
|
||||
):
|
||||
"""Test that device_tracker added after first update works."""
|
||||
caplog.set_level(logging.ERROR)
|
||||
utcnow = dt_util.utcnow()
|
||||
# Patching 'utcnow' to gain more control over the timed update.
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=utcnow):
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_entity_id": "device_tracker.origin",
|
||||
"destination_entity_id": "device_tracker.destination",
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"mode": TRAVEL_MODE_TRUCK,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
assert len(caplog.records) == 2
|
||||
assert "Unable to find entity" in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
# Device tracker appear after first update
|
||||
hass.states.async_set(
|
||||
"device_tracker.origin",
|
||||
"unknown",
|
||||
{
|
||||
"latitude": float(TRUCK_ORIGIN_LATITUDE),
|
||||
"longitude": float(TRUCK_ORIGIN_LONGITUDE),
|
||||
},
|
||||
)
|
||||
hass.states.async_set(
|
||||
"device_tracker.destination",
|
||||
"unknown",
|
||||
{
|
||||
"latitude": float(TRUCK_DESTINATION_LATITUDE),
|
||||
"longitude": float(TRUCK_DESTINATION_LONGITUDE),
|
||||
},
|
||||
)
|
||||
|
||||
# Test that update works more than once
|
||||
async_fire_time_changed(hass, utcnow + SCAN_INTERVAL)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
_assert_truck_sensor(sensor)
|
||||
assert len(caplog.records) == 0
|
||||
|
||||
|
||||
async def test_location_device_tracker_in_zone(
|
||||
hass, requests_mock_truck_response, caplog
|
||||
):
|
||||
"""Test that device_tracker in zone uses device_tracker state works."""
|
||||
caplog.set_level(logging.DEBUG)
|
||||
zone_config = {
|
||||
"zone": [
|
||||
{
|
||||
"name": "Origin",
|
||||
"latitude": TRUCK_ORIGIN_LATITUDE,
|
||||
"longitude": TRUCK_ORIGIN_LONGITUDE,
|
||||
"radius": 250,
|
||||
"passive": False,
|
||||
}
|
||||
]
|
||||
}
|
||||
assert await async_setup_component(hass, "zone", zone_config)
|
||||
hass.states.async_set(
|
||||
"device_tracker.origin", "origin", {"latitude": None, "longitude": None}
|
||||
)
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_entity_id": "device_tracker.origin",
|
||||
"destination_latitude": TRUCK_DESTINATION_LATITUDE,
|
||||
"destination_longitude": TRUCK_DESTINATION_LONGITUDE,
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"mode": TRAVEL_MODE_TRUCK,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
sensor = hass.states.get("sensor.test")
|
||||
_assert_truck_sensor(sensor)
|
||||
assert ", getting zone location" in caplog.text
|
||||
|
||||
|
||||
async def test_route_not_found(hass, requests_mock_credentials_check, caplog):
|
||||
"""Test that route not found error is correctly handled."""
|
||||
caplog.set_level(logging.ERROR)
|
||||
origin = "52.516,13.3779"
|
||||
destination = "47.013399,-10.171986"
|
||||
modes = [ROUTE_MODE_FASTEST, TRAVEL_MODE_CAR, TRAFFIC_MODE_DISABLED]
|
||||
response_url = _build_mock_url(origin, destination, modes, APP_ID, APP_CODE, "now")
|
||||
requests_mock_credentials_check.get(
|
||||
response_url,
|
||||
text=load_fixture("here_travel_time/routing_error_no_route_found.json"),
|
||||
)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": origin.split(",")[0],
|
||||
"origin_longitude": origin.split(",")[1],
|
||||
"destination_latitude": destination.split(",")[0],
|
||||
"destination_longitude": destination.split(",")[1],
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
assert len(caplog.records) == 1
|
||||
assert NO_ROUTE_ERROR_MESSAGE in caplog.text
|
||||
|
||||
|
||||
async def test_pattern_origin(hass, caplog):
|
||||
"""Test that pattern matching the origin works."""
|
||||
caplog.set_level(logging.ERROR)
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": "138.90",
|
||||
"origin_longitude": "-77.04833",
|
||||
"destination_latitude": CAR_DESTINATION_LATITUDE,
|
||||
"destination_longitude": CAR_DESTINATION_LONGITUDE,
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
assert len(caplog.records) == 1
|
||||
assert "invalid latitude" in caplog.text
|
||||
|
||||
|
||||
async def test_pattern_destination(hass, caplog):
|
||||
"""Test that pattern matching the destination works."""
|
||||
caplog.set_level(logging.ERROR)
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": CAR_ORIGIN_LATITUDE,
|
||||
"origin_longitude": CAR_ORIGIN_LONGITUDE,
|
||||
"destination_latitude": "139.0",
|
||||
"destination_longitude": "-77.1",
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
assert len(caplog.records) == 1
|
||||
assert "invalid latitude" in caplog.text
|
||||
|
||||
|
||||
async def test_invalid_credentials(hass, requests_mock, caplog):
|
||||
"""Test that invalid credentials error is correctly handled."""
|
||||
caplog.set_level(logging.ERROR)
|
||||
modes = [ROUTE_MODE_FASTEST, TRAVEL_MODE_CAR, TRAFFIC_MODE_DISABLED]
|
||||
response_url = _build_mock_url(
|
||||
",".join([CAR_ORIGIN_LATITUDE, CAR_ORIGIN_LONGITUDE]),
|
||||
",".join([CAR_DESTINATION_LATITUDE, CAR_DESTINATION_LONGITUDE]),
|
||||
modes,
|
||||
APP_ID,
|
||||
APP_CODE,
|
||||
"now",
|
||||
)
|
||||
requests_mock.get(
|
||||
response_url,
|
||||
text=load_fixture("here_travel_time/routing_error_invalid_credentials.json"),
|
||||
)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": CAR_ORIGIN_LATITUDE,
|
||||
"origin_longitude": CAR_ORIGIN_LONGITUDE,
|
||||
"destination_latitude": CAR_DESTINATION_LATITUDE,
|
||||
"destination_longitude": CAR_DESTINATION_LONGITUDE,
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
assert len(caplog.records) == 1
|
||||
assert "Invalid credentials" in caplog.text
|
||||
|
||||
|
||||
async def test_attribution(hass, requests_mock_credentials_check):
|
||||
"""Test that attributions are correctly displayed."""
|
||||
origin = "50.037751372637686,14.39233448220898"
|
||||
destination = "50.07993838201255,14.42582157361062"
|
||||
modes = [ROUTE_MODE_SHORTEST, TRAVEL_MODE_PUBLIC_TIME_TABLE, TRAFFIC_MODE_ENABLED]
|
||||
response_url = _build_mock_url(origin, destination, modes, APP_ID, APP_CODE, "now")
|
||||
requests_mock_credentials_check.get(
|
||||
response_url, text=load_fixture("here_travel_time/attribution_response.json")
|
||||
)
|
||||
|
||||
config = {
|
||||
DOMAIN: {
|
||||
"platform": PLATFORM,
|
||||
"name": "test",
|
||||
"origin_latitude": origin.split(",")[0],
|
||||
"origin_longitude": origin.split(",")[1],
|
||||
"destination_latitude": destination.split(",")[0],
|
||||
"destination_longitude": destination.split(",")[1],
|
||||
"app_id": APP_ID,
|
||||
"app_code": APP_CODE,
|
||||
"traffic_mode": True,
|
||||
"route_mode": ROUTE_MODE_SHORTEST,
|
||||
"mode": TRAVEL_MODE_PUBLIC_TIME_TABLE,
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
sensor = hass.states.get("sensor.test")
|
||||
assert (
|
||||
sensor.attributes.get(ATTR_ATTRIBUTION)
|
||||
== "With the support of HERE Technologies. All information is provided without warranty of any kind."
|
||||
)
|
||||
Reference in new issue
Block a user