Skip to content

Commit

Permalink
Deprecate short endpoints for vehicle actions (#504)
Browse files Browse the repository at this point in the history
* Require full endpoint for vehicle actions

* Cleanup

* Move comment

Co-authored-by: epenet <[email protected]>
  • Loading branch information
epenet and epenet authored Feb 1, 2022
1 parent 42d977b commit 680cd5a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
43 changes: 27 additions & 16 deletions src/renault_api/kamereon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
_LOGGER = logging.getLogger(__name__)


DATA_ENDPOINTS: Dict[str, Any] = {
_KCA_GET_ENDPOINTS: Dict[str, Any] = {
"": {"version": 2},
"battery-status": {"version": 2},
"charge-history": {"version": 1},
Expand All @@ -34,14 +34,18 @@
"lock-status": {"version": 1},
"notification-settings": {"version": 1},
}
ACTION_ENDPOINTS: Dict[str, Any] = {
"charge-mode": {"version": 1, "type": "ChargeMode"},
"charge-schedule": {"version": 2, "type": "ChargeSchedule"},
"charging-start": {"version": 1, "type": "ChargingStart"},
"hvac-schedule": {"version": 2, "type": "HvacSchedule"},
"hvac-start": {"version": 1, "type": "HvacStart"},
_KCA_POST_ENDPOINTS: Dict[str, Any] = {
"actions/charge-mode": {"version": 1, "type": "ChargeMode"},
"actions/charge-schedule": {"version": 2, "type": "ChargeSchedule"},
"actions/charging-start": {"version": 1, "type": "ChargingStart"},
"actions/hvac-schedule": {"version": 2, "type": "HvacSchedule"},
"actions/hvac-start": {"version": 1, "type": "HvacStart"},
}

# Deprecated from 0.1.8 - kept for compatibility
DATA_ENDPOINTS = _KCA_GET_ENDPOINTS
ACTION_ENDPOINTS = _KCA_POST_ENDPOINTS


def get_commerce_url(root_url: str) -> str:
"""Get the Kamereon base commerce url."""
Expand Down Expand Up @@ -255,10 +259,6 @@ async def get_vehicle_details(
)


def _get_endpoint_version(endpoint_details: Dict[str, Any]) -> int:
return int(endpoint_details["version"])


async def get_vehicle_data(
websession: aiohttp.ClientSession,
root_url: str,
Expand All @@ -272,10 +272,11 @@ async def get_vehicle_data(
params: Optional[Dict[str, str]] = None,
) -> models.KamereonVehicleDataResponse:
"""GET to /v{endpoint_version}/cars/{vin}/{endpoint}."""
endpoint_details = _KCA_GET_ENDPOINTS[endpoint]
car_adapter_url = get_car_adapter_url(
root_url=root_url,
account_id=account_id,
version=endpoint_version or _get_endpoint_version(DATA_ENDPOINTS[endpoint]),
version=endpoint_version or int(endpoint_details["version"]),
vin=vin,
)
url = f"{car_adapter_url}/{endpoint}" if endpoint else car_adapter_url
Expand Down Expand Up @@ -308,18 +309,28 @@ async def set_vehicle_action(
endpoint_version: Optional[int] = None,
data_type: Optional[Dict[str, Any]] = None,
) -> models.KamereonVehicleDataResponse:
"""POST to /v{endpoint_version}/cars/{vin}/actions/{endpoint}."""
"""POST to /v{endpoint_version}/cars/{vin}/{endpoint}."""
if "/" not in endpoint:
# Deprecated in 0.1.8
warn(
f"You should use the full endpoint: actions/{endpoint}.",
DeprecationWarning,
stacklevel=2,
)
endpoint = f"actions/{endpoint}"

endpoint_details = _KCA_POST_ENDPOINTS[endpoint]
car_adapter_url = get_car_adapter_url(
root_url=root_url,
account_id=account_id,
version=endpoint_version or _get_endpoint_version(ACTION_ENDPOINTS[endpoint]),
version=endpoint_version or int(endpoint_details["version"]),
vin=vin,
)
url = f"{car_adapter_url}/actions/{endpoint}"
url = f"{car_adapter_url}/{endpoint}"
params = {"country": country}
json = {
"data": {
"type": data_type or ACTION_ENDPOINTS[endpoint]["type"],
"type": data_type or endpoint_details["type"],
"attributes": attributes,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/renault_api/renault_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ async def set_vehicle_action(
endpoint: str,
attributes: Dict[str, Any],
) -> models.KamereonVehicleDataResponse:
"""POST to /v{endpoint_version}/cars/{vin}/actions/{endpoint}."""
"""POST to /v{endpoint_version}/cars/{vin}/{endpoint}."""
return await kamereon.set_vehicle_action(
websession=self._websession,
root_url=await self._get_kamereon_root_url(),
Expand Down
14 changes: 7 additions & 7 deletions src/renault_api/renault_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ async def set_ac_start(
response = await self.session.set_vehicle_action(
account_id=self.account_id,
vin=self.vin,
endpoint="hvac-start",
endpoint="actions/hvac-start",
attributes=attributes,
)
return cast(
Expand All @@ -423,7 +423,7 @@ async def set_ac_stop(self) -> models.KamereonVehicleHvacStartActionData:
response = await self.session.set_vehicle_action(
account_id=self.account_id,
vin=self.vin,
endpoint="hvac-start",
endpoint="actions/hvac-start",
attributes=attributes,
)
return cast(
Expand All @@ -448,7 +448,7 @@ async def set_hvac_schedules(
response = await self.session.set_vehicle_action(
account_id=self.account_id,
vin=self.vin,
endpoint="hvac-schedule",
endpoint="actions/hvac-schedule",
attributes=attributes,
)
return cast(
Expand All @@ -475,7 +475,7 @@ async def set_charge_schedules(
response = await self.session.set_vehicle_action(
account_id=self.account_id,
vin=self.vin,
endpoint="charge-schedule",
endpoint="actions/charge-schedule",
attributes=attributes,
)
return cast(
Expand All @@ -495,7 +495,7 @@ async def set_charge_mode(
response = await self.session.set_vehicle_action(
account_id=self.account_id,
vin=self.vin,
endpoint="charge-mode",
endpoint="actions/charge-mode",
attributes=attributes,
)
return cast(
Expand All @@ -511,7 +511,7 @@ async def set_charge_start(self) -> models.KamereonVehicleChargingStartActionDat
response = await self.session.set_vehicle_action(
account_id=self.account_id,
vin=self.vin,
endpoint="charging-start",
endpoint="actions/charging-start",
attributes=attributes,
)
return cast(
Expand All @@ -529,7 +529,7 @@ async def set_charge_stop(self) -> models.KamereonVehicleChargingStartActionData
response = await self.session.set_vehicle_action(
account_id=self.account_id,
vin=self.vin,
endpoint="charging-start",
endpoint="actions/charging-start",
attributes=attributes,
)
return cast(
Expand Down

0 comments on commit 680cd5a

Please sign in to comment.