1 """Teslemetry helper functions."""
6 from tesla_fleet_api.exceptions
import TeslaFleetError
10 from .const
import DOMAIN, LOGGER, TeslemetryState
13 def flatten(data: dict[str, Any], parent: str |
None =
None) -> dict[str, Any]:
14 """Flatten the data structure."""
16 for key, value
in data.items():
18 key = f
"{parent}_{key}"
19 if isinstance(value, dict):
20 result.update(
flatten(value, key))
27 """Wake up a vehicle."""
28 async
with vehicle.wakelock:
30 while vehicle.coordinator.data[
"state"] != TeslemetryState.ONLINE:
33 cmd = await vehicle.api.wake_up()
35 cmd = await vehicle.api.vehicle()
36 state = cmd[
"response"][
"state"]
37 except TeslaFleetError
as e:
39 translation_domain=DOMAIN,
40 translation_key=
"wake_up_failed",
41 translation_placeholders={
"message": e.message},
43 vehicle.coordinator.data[
"state"] = state
44 if state != TeslemetryState.ONLINE:
48 translation_domain=DOMAIN,
49 translation_key=
"wake_up_timeout",
51 await asyncio.sleep(times * 5)
55 """Handle a command."""
57 result = await command
58 except TeslaFleetError
as e:
60 translation_domain=DOMAIN,
61 translation_key=
"command_exception",
62 translation_placeholders={
"message": e.message},
64 LOGGER.debug(
"Command result: %s", result)
69 """Handle a vehicle command."""
71 if (response := result.get(
"response"))
is None:
72 if error := result.get(
"error"):
75 translation_domain=DOMAIN,
76 translation_key=
"command_error",
77 translation_placeholders={
"error": error},
81 if (result := response.get(
"result"))
is not True:
82 if reason := response.get(
"reason"):
83 if reason
in (
"already_set",
"not_charging",
"requested"):
88 translation_domain=DOMAIN,
89 translation_key=
"command_reason",
90 translation_placeholders={
"reason": reason},
94 translation_domain=DOMAIN, translation_key=
"command_no_result"
None wake_up_vehicle(vehicle)
Any handle_vehicle_command(command)
dict[str, Any] handle_command(command)
dict[str, Any] flatten(dict[str, Any] data, str|None parent=None)