Migrate to api_key (#29966)

pull/29989/head
Kevin Eifinger 2019-12-16 08:20:41 +01:00 committed by Paulus Schoutsen
parent 039cc98278
commit fc01da8933
5 changed files with 48 additions and 82 deletions

View File

@ -3,7 +3,7 @@
"name": "HERE travel time",
"documentation": "https://www.home-assistant.io/integrations/here_travel_time",
"requirements": [
"herepy==0.6.3.3"
"herepy==2.0.0"
],
"dependencies": [],
"codeowners": [

View File

@ -32,8 +32,7 @@ CONF_DESTINATION_ENTITY_ID = "destination_entity_id"
CONF_ORIGIN_LATITUDE = "origin_latitude"
CONF_ORIGIN_LONGITUDE = "origin_longitude"
CONF_ORIGIN_ENTITY_ID = "origin_entity_id"
CONF_APP_ID = "app_id"
CONF_APP_CODE = "app_code"
CONF_API_KEY = "api_key"
CONF_TRAFFIC_MODE = "traffic_mode"
CONF_ROUTE_MODE = "route_mode"
@ -97,8 +96,7 @@ PLATFORM_SCHEMA = vol.All(
cv.has_at_least_one_key(CONF_ORIGIN_LATITUDE, CONF_ORIGIN_ENTITY_ID),
PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_APP_ID): cv.string,
vol.Required(CONF_APP_CODE): cv.string,
vol.Required(CONF_API_KEY): cv.string,
vol.Inclusive(
CONF_DESTINATION_LATITUDE, "destination_coordinates"
): cv.latitude,
@ -131,9 +129,8 @@ async def async_setup_platform(
) -> None:
"""Set up the HERE travel time platform."""
app_id = config[CONF_APP_ID]
app_code = config[CONF_APP_CODE]
here_client = herepy.RoutingApi(app_id, app_code)
api_key = config[CONF_API_KEY]
here_client = herepy.RoutingApi(api_key)
if not await hass.async_add_executor_job(
_are_valid_client_credentials, here_client

View File

@ -648,7 +648,7 @@ hdate==0.9.3
heatmiserV3==1.1.18
# homeassistant.components.here_travel_time
herepy==0.6.3.3
herepy==2.0.0
# homeassistant.components.hikvisioncam
hikvision==0.4

View File

@ -222,7 +222,7 @@ hbmqtt==0.9.5
hdate==0.9.3
# homeassistant.components.here_travel_time
herepy==0.6.3.3
herepy==2.0.0
# homeassistant.components.pi_hole
hole==0.5.0

View File

@ -48,8 +48,7 @@ DOMAIN = "sensor"
PLATFORM = "here_travel_time"
APP_ID = "test"
APP_CODE = "test"
API_KEY = "test"
TRUCK_ORIGIN_LATITUDE = "41.9798"
TRUCK_ORIGIN_LONGITUDE = "-87.8801"
@ -67,15 +66,14 @@ CAR_DESTINATION_LATITUDE = "39.0"
CAR_DESTINATION_LONGITUDE = "-77.1"
def _build_mock_url(origin, destination, modes, app_id, app_code, departure):
def _build_mock_url(origin, destination, modes, api_key, departure):
"""Construct a url for HERE."""
base_url = "https://route.cit.api.here.com/routing/7.2/calculateroute.json?"
base_url = "https://route.ls.hereapi.com/routing/7.2/calculateroute.json?"
parameters = {
"waypoint0": f"geo!{origin}",
"waypoint1": f"geo!{destination}",
"mode": ";".join(str(herepy.RouteMode[mode]) for mode in modes),
"app_id": app_id,
"app_code": app_code,
"apikey": api_key,
"departure": departure,
}
url = base_url + urllib.parse.urlencode(parameters)
@ -118,8 +116,7 @@ def requests_mock_credentials_check(requests_mock):
",".join([CAR_ORIGIN_LATITUDE, CAR_ORIGIN_LONGITUDE]),
",".join([CAR_DESTINATION_LATITUDE, CAR_DESTINATION_LONGITUDE]),
modes,
APP_ID,
APP_CODE,
API_KEY,
"now",
)
requests_mock.get(
@ -136,8 +133,7 @@ def requests_mock_truck_response(requests_mock_credentials_check):
",".join([TRUCK_ORIGIN_LATITUDE, TRUCK_ORIGIN_LONGITUDE]),
",".join([TRUCK_DESTINATION_LATITUDE, TRUCK_DESTINATION_LONGITUDE]),
modes,
APP_ID,
APP_CODE,
API_KEY,
"now",
)
requests_mock_credentials_check.get(
@ -153,8 +149,7 @@ def requests_mock_car_disabled_response(requests_mock_credentials_check):
",".join([CAR_ORIGIN_LATITUDE, CAR_ORIGIN_LONGITUDE]),
",".join([CAR_DESTINATION_LATITUDE, CAR_DESTINATION_LONGITUDE]),
modes,
APP_ID,
APP_CODE,
API_KEY,
"now",
)
requests_mock_credentials_check.get(
@ -172,8 +167,7 @@ async def test_car(hass, requests_mock_car_disabled_response):
"origin_longitude": CAR_ORIGIN_LONGITUDE,
"destination_latitude": CAR_DESTINATION_LATITUDE,
"destination_longitude": CAR_DESTINATION_LONGITUDE,
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
}
}
assert await async_setup_component(hass, DOMAIN, config)
@ -219,8 +213,7 @@ async def test_traffic_mode_enabled(hass, requests_mock_credentials_check):
",".join([CAR_ORIGIN_LATITUDE, CAR_ORIGIN_LONGITUDE]),
",".join([CAR_DESTINATION_LATITUDE, CAR_DESTINATION_LONGITUDE]),
modes,
APP_ID,
APP_CODE,
API_KEY,
"now",
)
requests_mock_credentials_check.get(
@ -235,8 +228,7 @@ async def test_traffic_mode_enabled(hass, requests_mock_credentials_check):
"origin_longitude": CAR_ORIGIN_LONGITUDE,
"destination_latitude": CAR_DESTINATION_LATITUDE,
"destination_longitude": CAR_DESTINATION_LONGITUDE,
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"traffic_mode": True,
}
}
@ -262,8 +254,7 @@ async def test_imperial(hass, requests_mock_car_disabled_response):
"origin_longitude": CAR_ORIGIN_LONGITUDE,
"destination_latitude": CAR_DESTINATION_LATITUDE,
"destination_longitude": CAR_DESTINATION_LONGITUDE,
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"unit_system": "imperial",
}
}
@ -281,7 +272,7 @@ async def test_route_mode_shortest(hass, requests_mock_credentials_check):
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")
response_url = _build_mock_url(origin, destination, modes, API_KEY, "now")
requests_mock_credentials_check.get(
response_url, text=load_fixture("here_travel_time/car_shortest_response.json")
)
@ -294,8 +285,7 @@ async def test_route_mode_shortest(hass, requests_mock_credentials_check):
"origin_longitude": origin.split(",")[1],
"destination_latitude": destination.split(",")[0],
"destination_longitude": destination.split(",")[1],
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"route_mode": ROUTE_MODE_SHORTEST,
}
}
@ -313,7 +303,7 @@ async def test_route_mode_fastest(hass, requests_mock_credentials_check):
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")
response_url = _build_mock_url(origin, destination, modes, API_KEY, "now")
requests_mock_credentials_check.get(
response_url, text=load_fixture("here_travel_time/car_enabled_response.json")
)
@ -326,8 +316,7 @@ async def test_route_mode_fastest(hass, requests_mock_credentials_check):
"origin_longitude": origin.split(",")[1],
"destination_latitude": destination.split(",")[0],
"destination_longitude": destination.split(",")[1],
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"traffic_mode": True,
}
}
@ -350,8 +339,7 @@ async def test_truck(hass, requests_mock_truck_response):
"origin_longitude": TRUCK_ORIGIN_LONGITUDE,
"destination_latitude": TRUCK_DESTINATION_LATITUDE,
"destination_longitude": TRUCK_DESTINATION_LONGITUDE,
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_TRUCK,
}
}
@ -369,7 +357,7 @@ async def test_public_transport(hass, requests_mock_credentials_check):
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")
response_url = _build_mock_url(origin, destination, modes, API_KEY, "now")
requests_mock_credentials_check.get(
response_url, text=load_fixture("here_travel_time/public_response.json")
)
@ -382,8 +370,7 @@ async def test_public_transport(hass, requests_mock_credentials_check):
"origin_longitude": origin.split(",")[1],
"destination_latitude": destination.split(",")[0],
"destination_longitude": destination.split(",")[1],
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_PUBLIC,
}
}
@ -419,7 +406,7 @@ async def test_public_transport_time_table(hass, requests_mock_credentials_check
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")
response_url = _build_mock_url(origin, destination, modes, API_KEY, "now")
requests_mock_credentials_check.get(
response_url,
text=load_fixture("here_travel_time/public_time_table_response.json"),
@ -433,8 +420,7 @@ async def test_public_transport_time_table(hass, requests_mock_credentials_check
"origin_longitude": origin.split(",")[1],
"destination_latitude": destination.split(",")[0],
"destination_longitude": destination.split(",")[1],
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_PUBLIC_TIME_TABLE,
}
}
@ -470,7 +456,7 @@ async def test_pedestrian(hass, requests_mock_credentials_check):
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")
response_url = _build_mock_url(origin, destination, modes, API_KEY, "now")
requests_mock_credentials_check.get(
response_url, text=load_fixture("here_travel_time/pedestrian_response.json")
)
@ -483,8 +469,7 @@ async def test_pedestrian(hass, requests_mock_credentials_check):
"origin_longitude": origin.split(",")[1],
"destination_latitude": destination.split(",")[0],
"destination_longitude": destination.split(",")[1],
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_PEDESTRIAN,
}
}
@ -523,7 +508,7 @@ async def test_bicycle(hass, requests_mock_credentials_check):
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")
response_url = _build_mock_url(origin, destination, modes, API_KEY, "now")
requests_mock_credentials_check.get(
response_url, text=load_fixture("here_travel_time/bike_response.json")
)
@ -536,8 +521,7 @@ async def test_bicycle(hass, requests_mock_credentials_check):
"origin_longitude": origin.split(",")[1],
"destination_latitude": destination.split(",")[0],
"destination_longitude": destination.split(",")[1],
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_BICYCLE,
}
}
@ -599,8 +583,7 @@ async def test_location_zone(hass, requests_mock_truck_response):
"name": "test",
"origin_entity_id": "zone.origin",
"destination_entity_id": "zone.destination",
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_TRUCK,
}
}
@ -640,8 +623,7 @@ async def test_location_sensor(hass, requests_mock_truck_response):
"name": "test",
"origin_entity_id": "sensor.origin",
"destination_entity_id": "sensor.destination",
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_TRUCK,
}
}
@ -689,8 +671,7 @@ async def test_location_person(hass, requests_mock_truck_response):
"name": "test",
"origin_entity_id": "person.origin",
"destination_entity_id": "person.destination",
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_TRUCK,
}
}
@ -738,8 +719,7 @@ async def test_location_device_tracker(hass, requests_mock_truck_response):
"name": "test",
"origin_entity_id": "device_tracker.origin",
"destination_entity_id": "device_tracker.destination",
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_TRUCK,
}
}
@ -773,8 +753,7 @@ async def test_location_device_tracker_added_after_update(
"name": "test",
"origin_entity_id": "device_tracker.origin",
"destination_entity_id": "device_tracker.destination",
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_TRUCK,
}
}
@ -842,8 +821,7 @@ async def test_location_device_tracker_in_zone(
"origin_entity_id": "device_tracker.origin",
"destination_latitude": TRUCK_DESTINATION_LATITUDE,
"destination_longitude": TRUCK_DESTINATION_LONGITUDE,
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_TRUCK,
}
}
@ -863,7 +841,7 @@ async def test_route_not_found(hass, requests_mock_credentials_check, caplog):
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")
response_url = _build_mock_url(origin, destination, modes, API_KEY, "now")
requests_mock_credentials_check.get(
response_url,
text=load_fixture("here_travel_time/routing_error_no_route_found.json"),
@ -877,8 +855,7 @@ async def test_route_not_found(hass, requests_mock_credentials_check, caplog):
"origin_longitude": origin.split(",")[1],
"destination_latitude": destination.split(",")[0],
"destination_longitude": destination.split(",")[1],
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
}
}
assert await async_setup_component(hass, DOMAIN, config)
@ -901,8 +878,7 @@ async def test_pattern_origin(hass, caplog):
"origin_longitude": "-77.04833",
"destination_latitude": CAR_DESTINATION_LATITUDE,
"destination_longitude": CAR_DESTINATION_LONGITUDE,
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
}
}
assert await async_setup_component(hass, DOMAIN, config)
@ -921,8 +897,7 @@ async def test_pattern_destination(hass, caplog):
"origin_longitude": CAR_ORIGIN_LONGITUDE,
"destination_latitude": "139.0",
"destination_longitude": "-77.1",
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
}
}
assert await async_setup_component(hass, DOMAIN, config)
@ -938,8 +913,7 @@ async def test_invalid_credentials(hass, requests_mock, caplog):
",".join([CAR_ORIGIN_LATITUDE, CAR_ORIGIN_LONGITUDE]),
",".join([CAR_DESTINATION_LATITUDE, CAR_DESTINATION_LONGITUDE]),
modes,
APP_ID,
APP_CODE,
API_KEY,
"now",
)
requests_mock.get(
@ -955,8 +929,7 @@ async def test_invalid_credentials(hass, requests_mock, caplog):
"origin_longitude": CAR_ORIGIN_LONGITUDE,
"destination_latitude": CAR_DESTINATION_LATITUDE,
"destination_longitude": CAR_DESTINATION_LONGITUDE,
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
}
}
assert await async_setup_component(hass, DOMAIN, config)
@ -969,7 +942,7 @@ async def test_attribution(hass, requests_mock_credentials_check):
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")
response_url = _build_mock_url(origin, destination, modes, API_KEY, "now")
requests_mock_credentials_check.get(
response_url, text=load_fixture("here_travel_time/attribution_response.json")
)
@ -982,8 +955,7 @@ async def test_attribution(hass, requests_mock_credentials_check):
"origin_longitude": origin.split(",")[1],
"destination_latitude": destination.split(",")[0],
"destination_longitude": destination.split(",")[1],
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"traffic_mode": True,
"route_mode": ROUTE_MODE_SHORTEST,
"mode": TRAVEL_MODE_PUBLIC_TIME_TABLE,
@ -1013,8 +985,7 @@ async def test_pattern_entity_state(hass, requests_mock_truck_response, caplog):
"origin_entity_id": "sensor.origin",
"destination_latitude": TRUCK_DESTINATION_LATITUDE,
"destination_longitude": TRUCK_DESTINATION_LONGITUDE,
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_TRUCK,
}
}
@ -1040,8 +1011,7 @@ async def test_pattern_entity_state_with_space(hass, requests_mock_truck_respons
"origin_entity_id": "sensor.origin",
"destination_latitude": TRUCK_DESTINATION_LATITUDE,
"destination_longitude": TRUCK_DESTINATION_LONGITUDE,
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_TRUCK,
}
}
@ -1059,8 +1029,7 @@ async def test_delayed_update(hass, requests_mock_truck_response, caplog):
"origin_entity_id": "sensor.origin",
"destination_latitude": TRUCK_DESTINATION_LATITUDE,
"destination_longitude": TRUCK_DESTINATION_LONGITUDE,
"app_id": APP_ID,
"app_code": APP_CODE,
"api_key": API_KEY,
"mode": TRAVEL_MODE_TRUCK,
}
}