Created
May 27, 2024 15:36
-
-
Save TheDelta/c11d16b23169481267f38da1779c4870 to your computer and use it in GitHub Desktop.
Usage example of https://github.com/cyr-ius/audiconnectpy
This file contains hidden or 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
""" | |
Example script | |
Usage | |
Create a .env file in the same folder as this example.py file (or update the vars blow) | |
Content of .env should be | |
``` | |
EMAIL=<your-email> | |
PASSWORD=<your-pw> | |
``` | |
You can also change COUNTRY & SPIN | |
``` | |
COUNTRY=EN | |
SPIN=1234 | |
``` | |
Execute and hopefully you get some information :) | |
to enable debug loggin, set `DEBUG_LOG=True` | |
to disable json nice print out, set `JSON_INDENT=None` | |
""" | |
import asyncio | |
import logging | |
from aiohttp import ClientSession | |
from audiconnectpy import AudiConnect, AudiException | |
from dotenv import dotenv_values | |
import json | |
config = dotenv_values(".env") | |
##################### | |
## ACCESS VARIABLES (use .env is recommended) | |
## | |
VW_USERNAME = config.get("EMAIL", "[email protected]") | |
VW_PASSWORD = config.get("PASSWORD", "top-secret") | |
COUNTRY = config.get("COUNTRY", "DE") | |
SPIN = config.get("SPIN") | |
#################### | |
JSON_INDENT=4 #None # None to disable pretty print or integer >= 1 for pretty print | |
DEBUG_LOG=True # Set to False to set logging to warn/error only | |
#################### | |
"""Helper class for some CLI colors""" | |
class tcol: | |
HEADER = '\033[95m' | |
BLUE = '\033[94m' | |
CYAN = '\033[96m' | |
SUCCESS = '\033[92m' | |
WARNING = '\033[93m' | |
DANGER = '\033[91m' | |
END = '\033[0m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
logger = logging.getLogger() | |
logger.setLevel(logging.DEBUG if DEBUG_LOG else logging.WARNING) | |
ch = logging.StreamHandler() | |
formatter = logging.Formatter("%(asctime)s %(name)s [" + tcol.BOLD + "%(levelname)s" + tcol.END + "] | %(message)s") | |
ch.setFormatter(formatter) | |
logger.addHandler(ch) | |
_LOGGER = logging.getLogger(__name__) | |
async def main() -> None: | |
"""Init method.""" | |
async with ClientSession() as session: | |
try: | |
api = AudiConnect(session, VW_USERNAME, VW_PASSWORD, COUNTRY, SPIN) | |
print(api.vehicles) | |
await api.async_login() | |
json_data = {} | |
for vehicle in api.vehicles: | |
try: | |
_LOGGER.debug(tcol.CYAN + vehicle.vin + tcol.END) | |
# pick whatever data you like | |
json_data[vehicle.vin] = { | |
'vin': vehicle.vin, | |
'infos': vehicle.infos.to_dict(), | |
'position': vehicle.position.to_dict(), | |
'measurements': vehicle.measurements.to_dict(), | |
'fuel': vehicle.fuel_status.to_dict(), | |
'last_access': vehicle.last_access, | |
'last_update': vehicle.last_update | |
} | |
# | |
# just log some examples data nicely | |
# | |
if vehicle.nickname: | |
_LOGGER.info("Nickname: " + vehicle.nickname) | |
_LOGGER.info("Media Name Long:" + vehicle.infos.media.long_name) | |
_LOGGER.info("Model Year" + str(vehicle.infos.core.model_year)) | |
_LOGGER.info(tcol.BOLD + "--- Supported Features ---" + tcol.END) | |
for cap in vehicle.capabilities: | |
_LOGGER.info(cap) | |
_LOGGER.info(tcol.BOLD + "--- Position ---" + tcol.END) | |
_LOGGER.info("%s: %s", tcol.BOLD + "Lat" + tcol.END, vehicle.position.latitude) | |
_LOGGER.info("%s: %s", tcol.BOLD + "Lon" + tcol.END, vehicle.position.longitude) | |
_LOGGER.info(tcol.BOLD + "--- Addresses & Proprietaries ---" + tcol.END) | |
_LOGGER.info(vehicle.location.addresses) | |
_LOGGER.info(vehicle.location.proprietaries) | |
_LOGGER.info(tcol.BOLD + "--- Measurements ---" + tcol.END) | |
_LOGGER.info("%s: %s", tcol.BOLD + "Odemeter" + tcol.END, vehicle.measurements.odometer_status.odometer) | |
_LOGGER.info("%s: %s", tcol.BOLD + "Odemeter" + tcol.END, vehicle.measurements.odometer_status.odometer) | |
_LOGGER.info("%s: %s", tcol.BOLD + "Total range" + tcol.END, vehicle.measurements.range_status.total_range_km) | |
if vehicle.measurements.range_status.gasoline_range: | |
_LOGGER.info("%s: %s", tcol.BOLD + "Gasoline range" + tcol.END, vehicle.measurements.range_status.gasoline_range) | |
if vehicle.measurements.range_status.electric_range: | |
_LOGGER.info("%s: %s", tcol.BOLD + "Electric range" + tcol.END, vehicle.measurements.range_status.electric_range) | |
if vehicle.fuel_status.range_status: | |
range_status = vehicle.fuel_status.range_status | |
_LOGGER.info(tcol.BOLD + "--- Fuel ---" + tcol.END) | |
_LOGGER.info("%s: %s", tcol.BOLD + "Car Type" + tcol.END, range_status.car_type) | |
_LOGGER.info("%s: %s", tcol.BOLD + "Total Range km" + tcol.END, range_status.total_range_km) | |
_LOGGER.info("%s: %s", tcol.BOLD + "Primary engine fuel level" + tcol.END, range_status.primary_engine.current_fuel_level_pct) | |
if range_status.secondary_engine: | |
_LOGGER.info("%s: %s", tcol.BOLD + "Secondary engine fuel level" + tcol.END, range_status.secondary_engine.current_fuel_level_pct) | |
_LOGGER.info("%s: %s", tcol.BOLD + "Last Access" + tcol.END, vehicle.last_access) | |
_LOGGER.info("%s: %s", tcol.BOLD + "Last Update" + tcol.END, vehicle.last_update) | |
# logout the whole object raw | |
_LOGGER.debug("Vehicle raw: %s", vehicle) | |
except Exception as e: | |
_LOGGER.error(e) | |
print(json.dumps(json_data, indent=JSON_INDENT, default=str)) | |
except AudiException as error: | |
_LOGGER.error(error) | |
print(json.dumps({ 'error': str(error) }, indent=JSON_INDENT, default=str)) | |
if __name__ == "__main__": | |
asyncio.run(main()) |
This file contains hidden or 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
python-dotenv>=1.0.1 | |
audiconnectpy>=2.2.6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment