-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Zegorax/master
[CORE] Add Teslamate integration
- Loading branch information
Showing
10 changed files
with
190 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
service.db | ||
.venv | ||
.DS_Store | ||
dev/ | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from interfaces.backendinterface import IBackendProvider | ||
import requests | ||
|
||
class TeslaloggerBackendProvider(IBackendProvider): | ||
def refresh_data(self): | ||
data = requests.get(f"{self.base_url}/currentjson/{self.car_id}/").json() | ||
|
||
self.latitude = data["latitude"] | ||
self.longitude = data["longitude"] | ||
self.odometer = data["odometer"] | ||
self.is_driving = data["driving"] | ||
self.is_charging = data["charging"] | ||
self.battery_level = data["battery_level"] | ||
|
||
self.active_route_latitude = data["active_route_latitude"] | ||
self.active_route_longitude = data["active_route_longitude"] | ||
|
||
self.active_route_destination = data["active_route_destination"] | ||
self.active_route_minutes_to_arrival = data["active_route_minutes_to_arrival"] | ||
self.active_route_energy_at_arrival = data["active_route_energy_at_arrival"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from interfaces.backendinterface import IBackendProvider | ||
import requests | ||
|
||
class TeslamateBackendProvider(IBackendProvider): | ||
def refresh_data(self): | ||
|
||
data = requests.get(f"{self.base_url}/api/v1/cars/{self.car_id}/status").json()["data"]["status"] | ||
|
||
self.latitude = data["car_geodata"]["latitude"] | ||
self.longitude = data["car_geodata"]["longitude"] | ||
self.odometer = data["odometer"] | ||
self.is_driving = data["driving_details"]["shift_state"] is not "P" | ||
self.is_charging = data["charging_details"]["time_to_full_charge"] > 0 | ||
self.battery_level = data["battery_details"]["battery_level"] | ||
|
||
|
||
self.active_route_latitude = data["driving_details"]["active_route"]["location"]["latitude"] | ||
self.active_route_longitude = data["driving_details"]["active_route"]["location"]["longitude"] | ||
|
||
self.active_route_destination = data["driving_details"]["active_route"]["destination"] | ||
self.active_route_minutes_to_arrival = data["driving_details"]["active_route"]["minutes_to_arrival"] | ||
self.active_route_energy_at_arrival = data["driving_details"]["active_route"]["energy_at_arrival"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
services: | ||
teslamate: | ||
image: teslamate/teslamate:latest | ||
restart: always | ||
environment: | ||
- ENCRYPTION_KEY=secretkey #replace with a secure key to encrypt your Tesla API tokens | ||
- DATABASE_USER=teslamate | ||
- DATABASE_PASS=password #insert your secure database password! | ||
- DATABASE_NAME=teslamate | ||
- DATABASE_HOST=database | ||
- MQTT_HOST=mosquitto | ||
ports: | ||
- 4000:4000 | ||
volumes: | ||
- ./dev/import:/opt/app/import | ||
cap_drop: | ||
- all | ||
|
||
database: | ||
image: postgres:16 | ||
restart: always | ||
environment: | ||
- POSTGRES_USER=teslamate | ||
- POSTGRES_PASSWORD=password #insert your secure database password! | ||
- POSTGRES_DB=teslamate | ||
volumes: | ||
- ./dev/teslamate-db:/var/lib/postgresql/data | ||
|
||
mosquitto: | ||
image: eclipse-mosquitto:2 | ||
restart: always | ||
command: mosquitto -c /mosquitto-no-auth.conf | ||
# ports: | ||
# - 1883:1883 | ||
volumes: | ||
- ./dev/mosquitto-conf:/mosquitto/config | ||
- ./dev/mosquitto-data:/mosquitto/data | ||
|
||
networks: | ||
internal: |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from enum import Enum | ||
import importlib | ||
from backendproviders import teslalogger, teslamate | ||
from interfaces.backendinterface import IBackendProvider | ||
|
||
class BackendProviderFactory: | ||
# static to have a singleton-like | ||
provider: IBackendProvider | ||
|
||
def _load_provider(self, name, base_url, car_id): | ||
if name == "teslalogger": | ||
return teslalogger.TeslaloggerBackendProvider(base_url, car_id) | ||
elif name == "teslamate": | ||
return teslamate.TeslamateBackendProvider(base_url, car_id) | ||
else: | ||
raise Exception(f"Unknown backend provider : {name}. Available choices are 'teslalogger' or 'teslamagte'.") | ||
|
||
|
||
def __init__(self, provider_name, base_url, car_id): | ||
self.provider_name = provider_name | ||
|
||
BackendProviderFactory.provider = self._load_provider(provider_name, base_url, car_id) | ||
|
||
@staticmethod | ||
def get_instance() -> IBackendProvider: | ||
return BackendProviderFactory.provider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
class IBackendProvider(ABC): | ||
base_url: str = None | ||
car_id: int = None | ||
|
||
latitude: float = None | ||
longitude: float = None | ||
odometer: float = None | ||
is_driving: bool = False | ||
is_charging: bool = False | ||
battery_level: int = 0 | ||
|
||
active_route_destination: str = None | ||
active_route_latitude: float = None | ||
active_route_longitude: float = None | ||
active_route_minutes_to_arrival: float = None | ||
active_route_energy_at_arrival: int = None | ||
|
||
|
||
def __init__(self, base_url, car_id): | ||
self.base_url = base_url | ||
self.car_id = car_id | ||
|
||
print(f"Starting provider. BASE_URL : {base_url}, CAR_ID : {car_id}") | ||
|
||
@abstractmethod | ||
def refresh_data(self): | ||
pass | ||
|
||
@property | ||
def active_route_seconds_to_arrival(self) -> float: | ||
return self.active_route_minutes_to_arrival * 60 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ Werkzeug==3.0.3 | |
wheel==0.38.4 | ||
zipp==3.19.1 | ||
flask-login==0.6.3 | ||
geopy==2.3.0 | ||
geopy==2.3.0 |