Skip to content

Commit

Permalink
Add fingerprint to dict function (#481)
Browse files Browse the repository at this point in the history
* Add fingerprint to dict function

* Ignore codecov upload CI errors
  • Loading branch information
rikroe authored Aug 31, 2022
1 parent 0121990 commit 841f3aa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true
fail_ci_if_error: false
files: ./coverage.xml
name: "pytest-${{ matrix.python-version }}"
flags: "${{ matrix.python-version }}"
Expand Down
24 changes: 23 additions & 1 deletion bimmer_connected/account.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Access to a MyBMW account and all vehicles therein."""

import datetime
import json
import logging
import pathlib
from dataclasses import InitVar, dataclass, field
from typing import List, Optional
from tempfile import TemporaryDirectory
from typing import Any, Dict, List, Optional

import httpx

Expand Down Expand Up @@ -86,6 +88,26 @@ async def get_vehicles(self) -> None:

self.add_vehicle(vehicle_base, vehicle_state, fetched_at)

async def get_fingerprints(self) -> Dict[str, Any]:
"""Retrieve vehicle data from BMW servers and return original responses as JSON."""
original_log_response_path = self.config.log_response_path
fingerprints = {}

try:
# Use a temporary directory to just get the files from one call to get_vehicles()
with TemporaryDirectory() as tempdir:
tempdir_path = pathlib.Path(tempdir)
self.config.log_response_path = tempdir_path
await self.get_vehicles()
for logfile in tempdir_path.iterdir():
with open(logfile, "rb") as pointer:
fingerprints[logfile.name] = json.load(pointer)
finally:
# Make sure that log_response_path is always set to the original value afterwards
self.config.log_response_path = original_log_response_path

return fingerprints

def add_vehicle(self, vehicle_base: dict, vehicle_state: dict, fetched_at: datetime.datetime = None) -> None:
"""Add or update a vehicle from the API responses."""

Expand Down
14 changes: 14 additions & 0 deletions test/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,17 @@ async def test_client_async_only():
with httpx.Client(auth=MyBMWLoginRetry()) as client:
with pytest.raises(RuntimeError):
client.get("/eadrax-ucs/v1/presentation/oauth/config")


@pytest.mark.asyncio
async def test_get_fingerprints():
"""Test get_fingerprints to JSON."""
with account_mock():
account = MyBMWAccount(TEST_USERNAME, TEST_PASSWORD, TEST_REGION)

fingerprints = await account.get_fingerprints()

assert len([f for f in fingerprints if f.startswith("vehicles")]) == 2
assert len([f for f in fingerprints if f.startswith("state_bmw")]) == len(ALL_FINGERPRINTS.get("bmw", {}))
assert len([f for f in fingerprints if f.startswith("state_mini")]) == len(ALL_FINGERPRINTS.get("mini", {}))
assert len([f for f in fingerprints if f.startswith("state")]) == get_fingerprint_count()

0 comments on commit 841f3aa

Please sign in to comment.