From d7634b6113046c9cff1cb1e792503168b1e21fef Mon Sep 17 00:00:00 2001 From: asychow Date: Mon, 11 Nov 2024 02:28:22 +0000 Subject: [PATCH] feat(EU): support valet mode (#672) --- hyundai_kia_connect_api/ApiImpl.py | 22 ++++++++++++++++++++-- hyundai_kia_connect_api/KiaUvoApiEU.py | 16 ++++++++++++++++ hyundai_kia_connect_api/VehicleManager.py | 15 +++++++++++++-- hyundai_kia_connect_api/const.py | 5 +++++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/hyundai_kia_connect_api/ApiImpl.py b/hyundai_kia_connect_api/ApiImpl.py index d9927ce9..37829729 100644 --- a/hyundai_kia_connect_api/ApiImpl.py +++ b/hyundai_kia_connect_api/ApiImpl.py @@ -10,7 +10,14 @@ from requests.exceptions import JSONDecodeError from .Token import Token from .Vehicle import Vehicle -from .const import WINDOW_STATE, CHARGE_PORT_ACTION, OrderStatus, DOMAIN +from .const import ( + WINDOW_STATE, + CHARGE_PORT_ACTION, + OrderStatus, + DOMAIN, + VALET_MODE_ACTION, + VEHICLE_LOCK_ACTION, +) from .utils import get_child_value _LOGGER = logging.getLogger(__name__) @@ -128,7 +135,9 @@ def update_geocoded_location( get_child_value(response, "address"), ) - def lock_action(self, token: Token, vehicle: Vehicle, action: str) -> str: + def lock_action( + self, token: Token, vehicle: Vehicle, action: VEHICLE_LOCK_ACTION + ) -> str: """Lock or unlocks a vehicle. Returns the tracking ID""" pass @@ -220,3 +229,12 @@ def start_hazard_lights(self, token: Token, vehicle: Vehicle) -> str: def start_hazard_lights_and_horn(self, token: Token, vehicle: Vehicle) -> str: """Turns on the hazard lights and horn for 30 seconds""" pass + + def valet_mode_action( + self, token: Token, vehicle: Vehicle, action: VALET_MODE_ACTION + ) -> str: + """ + feature only available for some regions. + Activate or Deactivate valet mode. Returns the tracking ID + """ + pass diff --git a/hyundai_kia_connect_api/KiaUvoApiEU.py b/hyundai_kia_connect_api/KiaUvoApiEU.py index df1354cd..9da0e574 100644 --- a/hyundai_kia_connect_api/KiaUvoApiEU.py +++ b/hyundai_kia_connect_api/KiaUvoApiEU.py @@ -45,6 +45,7 @@ SEAT_STATUS, TEMPERATURE_UNITS, VEHICLE_LOCK_ACTION, + VALET_MODE_ACTION, ) from .exceptions import ( AuthenticationError, @@ -1435,6 +1436,21 @@ def set_default_departure_options( token.device_id = self._get_device_id(self._get_stamp()) return response["msgId"] + def valet_mode_action( + self, token: Token, vehicle: Vehicle, action: VALET_MODE_ACTION + ) -> str: + url = self.SPA_API_URL_V2 + "vehicles/" + vehicle.id + "/control/valet" + + payload = {"action": action.value} + _LOGGER.debug(f"{DOMAIN} - Valet Mode Action Request: {payload}") + response = requests.post( + url, json=payload, headers=self._get_control_headers(token, vehicle) + ).json() + _LOGGER.debug(f"{DOMAIN} - Valet Mode Action Response: {response}") + _check_response_for_errors(response) + token.device_id = self._get_device_id(self._get_stamp()) + return response["msgId"] + def _get_stamp(self) -> str: raw_data = f"{self.APP_ID}:{int(dt.datetime.now().timestamp())}".encode() result = bytes(b1 ^ b2 for b1, b2 in zip(self.CFB, raw_data)) diff --git a/hyundai_kia_connect_api/VehicleManager.py b/hyundai_kia_connect_api/VehicleManager.py index 92710f7d..77b9b1ac 100644 --- a/hyundai_kia_connect_api/VehicleManager.py +++ b/hyundai_kia_connect_api/VehicleManager.py @@ -37,6 +37,7 @@ VEHICLE_LOCK_ACTION, CHARGE_PORT_ACTION, OrderStatus, + VALET_MODE_ACTION, ) _LOGGER = logging.getLogger(__name__) @@ -257,11 +258,21 @@ def enable_vehicle(self, vehicle_id: str) -> None: def schedule_charging_and_climate( self, vehicle_id: str, options: ScheduleChargingClimateRequestOptions - ) -> None: - self.api.schedule_charging_and_climate( + ) -> str: + return self.api.schedule_charging_and_climate( self.token, self.get_vehicle(vehicle_id), options ) + def start_valet_mode(self, vehicle_id: str) -> str: + return self.api.valet_mode_action( + self.token, self.get_vehicle(vehicle_id), VALET_MODE_ACTION.ACTIVATE + ) + + def stop_valet_mode(self, vehicle_id: str) -> str: + return self.api.valet_mode_action( + self.token, self.get_vehicle(vehicle_id), VALET_MODE_ACTION.DEACTIVATE + ) + @staticmethod def get_implementation_by_region_brand( region: int, brand: int, language: str diff --git a/hyundai_kia_connect_api/const.py b/hyundai_kia_connect_api/const.py index 25fb55cd..a691e881 100644 --- a/hyundai_kia_connect_api/const.py +++ b/hyundai_kia_connect_api/const.py @@ -98,3 +98,8 @@ class WINDOW_STATE(IntEnum): CLOSED = 0 OPEN = 1 VENTILATION = 2 + + +class VALET_MODE_ACTION(Enum): + ACTIVATE = "activate" + DEACTIVATE = "deactivate"