Skip to content

Commit e9efbdb

Browse files
committed
Rework command for Home Asssistant service
1 parent d13f5d2 commit e9efbdb

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

teslemetry_stream/vehicle.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from typing import TYPE_CHECKING
22
import asyncio
33
import logging
4+
5+
from attr import dataclass
46
from .const import Signal
57

68
if TYPE_CHECKING:
@@ -57,13 +59,11 @@ async def update_config(self, config: dict) -> None:
5759
if not self._config:
5860
return
5961

60-
resp = await self.stream._session.patch(
61-
f"https://api.teslemetry.com/api/config/{self.vin}",
62-
headers=self.stream._headers,
63-
json=self._config,
64-
raise_for_status=False,
65-
)
66-
if resp.ok:
62+
resp = await self.patch_config(config)
63+
if error := resp.get("error"):
64+
LOGGER.error("Error updating streaming config for %s: %s", self.vin, error)
65+
return
66+
elif resp.get("response",{}).get("updated_vehicles"):
6767
LOGGER.info("Updated vehicle streaming config for %s", self.vin)
6868
if fields := self._config.get("fields"):
6969
LOGGER.debug("Configured streaming fields %s", ", ".join(fields.keys()))
@@ -73,6 +73,27 @@ async def update_config(self, config: dict) -> None:
7373
self.preferTyped = prefer_typed
7474
self._config.clear()
7575

76+
77+
async def patch_config(self, config: dict) -> dict[str, str|dict]:
78+
"""Modify the configuration for the vehicle."""
79+
resp = await self.stream._session.patch(
80+
f"https://api.teslemetry.com/api/config/{self.vin}",
81+
headers=self.stream._headers,
82+
json=config,
83+
raise_for_status=False,
84+
)
85+
return await resp.json()
86+
87+
async def post_config(self, config: dict) -> dict[str, str|dict]:
88+
"""Overwrite the configuration for the vehicle."""
89+
resp = await self.stream._session.post(
90+
f"https://api.teslemetry.com/api/config/{self.vin}",
91+
headers=self.stream._headers,
92+
json=config,
93+
raise_for_status=False,
94+
)
95+
return await resp.json()
96+
7697
async def add_field(self, field: Signal | str, interval: int | None = None) -> None:
7798
"""Handle vehicle data from the stream."""
7899
if isinstance(field, Signal):

0 commit comments

Comments
 (0)