Skip to content

Commit

Permalink
Enable Electric Climatisation
Browse files Browse the repository at this point in the history
  • Loading branch information
stickpin committed Dec 21, 2023
1 parent 82be1ad commit ca7767d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 61 deletions.
35 changes: 10 additions & 25 deletions volkswagencarnet/vw_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,34 +1032,19 @@ async def setCharger(self, vin, data) -> dict[str, str | int | None]:
except:
raise

async def setClimater(self, vin, data, spin):
async def setClimater(self, vin, data, action):
"""Execute climatisation actions."""

action = "start" if action else "stop"

try:
# Only get security token if auxiliary heater is to be started
if data.get("action", {}).get("settings", {}).get("heaterSource", None) == "auxiliary":
self._session_headers["X-securityToken"] = await self.get_sec_token(vin=vin, spin=spin, action="rclima")
response = await self.dataCall(
f"fs-car/bs/climatisation/v1/{BRAND}/{self._session_country}/vehicles/$vin/climater/actions",
vin,
json=data,
response_raw = await self.post(
f"{BASE_API}/vehicle/v1/vehicles/{vin}/climatisation/{action}", json=data, return_raw=True
)
self._session_headers.pop("X-securityToken", None)
if not response:
raise Exception("Invalid or no response")
elif response == 429:
return dict({"id": None, "state": "Throttled", "rate_limit_remaining": 0})
else:
request_id = response.get("action", {}).get("actionId", 0)
request_state = response.get("action", {}).get("actionState", "unknown")
remaining = response.get("rate_limit_remaining", -1)
_LOGGER.debug(
f'Request for climater action returned with state "{request_state}", request id: {request_id},'
f" remaining requests: {remaining}"
)
return dict({"id": str(request_id), "state": request_state, "rate_limit_remaining": remaining})
except:
self._session_headers.pop("X-securityToken", None)
raise
return await self._handle_action_result(response_raw)

except Exception as e:
raise Exception("Unknown error during setClimater") from e

async def setWindowHeater(self, vin, action):
"""Execute window heating actions."""
Expand Down
4 changes: 2 additions & 2 deletions volkswagencarnet/vw_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,11 @@ def state(self):
return self.vehicle.electric_climatisation

async def turn_on(self):
await self.vehicle.set_climatisation("electric")
await self.vehicle.set_climatisation("start")
await self.vehicle.update()

async def turn_off(self):
await self.vehicle.set_climatisation("off")
await self.vehicle.set_climatisation("stop")
await self.vehicle.update()

@property
Expand Down
56 changes: 22 additions & 34 deletions volkswagencarnet/vw_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,28 +466,24 @@ async def set_battery_climatisation(self, mode=False):
_LOGGER.error("No climatisation support.")
raise Exception("No climatisation support.")

async def set_climatisation(self, mode="off", spin=False):
async def set_climatisation(self, action="stop"):
"""Turn on/off climatisation with electric/auxiliary heater."""
if self.is_electric_climatisation_supported:
if mode in ["electric", "auxiliary"]:
target_temp = int((self.climatisation_target_temperature + 273) * 10)
without_hv_power = self.climatisation_without_external_power
if action == "start":
data = {
"action": {
"settings": {
"climatisationWithoutHVpower": without_hv_power,
"targetTemperature": target_temp,
"heaterSource": mode,
},
"type": "startClimatisation",
}
"climatisationWithoutExternalPower": self.climatisation_without_external_power,
"targetTemperature": self.climatisation_target_temperature,
"targetTemperatureUnit": "celsius"
}
elif mode == "off":
data = {"action": {"type": "stopClimatisation"}}
elif action == "stop":
data = {}
else:
_LOGGER.error(f"Invalid climatisation type: {mode}")
raise Exception(f"Invalid climatisation type: {mode}")
return await self.set_climater(data, spin)
_LOGGER.error(f"Invalid climatisation action: {action}")
raise Exception(f"Invalid climatisation action: {action}")
response = await self._connection.setClimater(self.vin, data, (action == "start"))
return await self._handle_response(
response=response, topic="climatisation", error_msg=f"Failed to {action} climatisation with electric/auxiliary heater."
)
else:
_LOGGER.error("No climatisation support.")
raise Exception("No climatisation support.")
Expand Down Expand Up @@ -1314,35 +1310,27 @@ def is_outside_temperature_supported(self) -> bool:
@property
def electric_climatisation(self) -> bool:
"""Return status of climatisation."""
# TODO not found yet
climatisation_type = (
self.attrs.get("climater", {}).get("settings", {}).get("heaterSource", {}).get("content", "")
)
status = (
self.attrs.get("climater", {})
.get("status", {})
.get("climatisationStatusData", {})
.get("climatisationState", {})
.get("content", "")
self.attrs.get("climatisation", {})
.get("climatisationStatus", {})
.get("value", {})
.get("climatisationState", "")
)
return status in ["heating", "on"] and climatisation_type == "electric"
return status in ["heating", "on"]

@property
def electric_climatisation_last_updated(self) -> datetime:
"""Return status of climatisation last updated."""
# TODO not found yet
return (
self.attrs.get("climater", {})
.get("status", {})
.get("climatisationStatusData", {})
.get("climatisationState", {})
.get(BACKEND_RECEIVED_TIMESTAMP)
self.attrs.get("climatisation", {})
.get("climatisationStatus", {})
.get("value", {})
.get("carCapturedTimestamp")
)

@property
def is_electric_climatisation_supported(self) -> bool:
"""Return true if vehicle has climater."""
# TODO not found yet
return self.is_climatisation_supported

@property
Expand Down

0 comments on commit ca7767d

Please sign in to comment.