-
Notifications
You must be signed in to change notification settings - Fork 115
Fix odometer issue in cupra and skoda SoC modules #3253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
716bb11
Add odometer data retrieval to Skoda API integration
vuffiraa72 d02d21b
Improve error handling in Skoda API vehicle status retrieval and add …
vuffiraa72 eace507
Update fetch_soc to include odometer data retrieval from server
vuffiraa72 f6370a4
Add odometer data retrieval to Cupra API integration
vuffiraa72 f71c907
Add unit tests for Cupra API odometer data retrieval and error handling
vuffiraa72 8f0ef19
Update fetch_soc comment to clarify data retrieval including odometer
vuffiraa72 ba892ed
Refactor Cupra and Skoda API to return structured SoCResult type and …
vuffiraa72 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| from unittest.mock import Mock, MagicMock | ||
| import pytest | ||
| import asyncio | ||
| from modules.common import store | ||
| from modules.common.abstract_vehicle import VehicleUpdateData | ||
| from modules.common.component_context import SingleComponentUpdateContext | ||
| from modules.vehicles.cupra import api | ||
| from modules.vehicles.cupra.soc import create_vehicle | ||
| from modules.vehicles.cupra.config import Cupra, CupraConfiguration | ||
| from modules.vehicles.cupra.libcupra import cupra as CupraApi | ||
|
|
||
|
|
||
| class TestCupra: | ||
| @pytest.fixture(autouse=True) | ||
| def set_up(self, monkeypatch): | ||
| self.mock_context_exit = Mock(return_value=True) | ||
| self.mock_fetch_soc = Mock( | ||
| name="fetch_soc", | ||
| return_value=(60, 320, "2026-03-29T11:00:00Z", 1774782000.0, 45678), | ||
| ) | ||
| self.mock_value_store = Mock(name="value_store") | ||
| monkeypatch.setattr(api, "fetch_soc", self.mock_fetch_soc) | ||
| monkeypatch.setattr(store, "get_car_value_store", Mock(return_value=self.mock_value_store)) | ||
| monkeypatch.setattr(SingleComponentUpdateContext, '__exit__', self.mock_context_exit) | ||
|
|
||
| def test_update_updates_value_store(self): | ||
| # setup | ||
| config = Cupra(configuration=CupraConfiguration(user_id="test_user", password="test_password", vin="test_vin")) | ||
|
|
||
| # execution | ||
| create_vehicle(config, 1).update(VehicleUpdateData()) | ||
|
|
||
| # evaluation | ||
| self.assert_context_manager_called_with(None) | ||
| self.mock_fetch_soc.assert_called_once_with(config, 1) | ||
| assert self.mock_value_store.set.call_count == 1 | ||
| call_args = self.mock_value_store.set.call_args[0][0] | ||
| assert call_args.soc == 60 | ||
| assert call_args.range == 320 | ||
| assert call_args.soc_timestamp == 1774782000.0 | ||
| assert call_args.odometer == 45678 | ||
|
|
||
| def test_update_passes_errors_to_context(self): | ||
| # setup | ||
| dummy_error = Exception("Der SoC kann nicht ausgelesen werden") | ||
| self.mock_fetch_soc.side_effect = dummy_error | ||
| config = Cupra(configuration=CupraConfiguration(user_id="test_user", password="test_password", vin="test_vin")) | ||
|
|
||
| # execution | ||
| create_vehicle(config, 1).update(VehicleUpdateData()) | ||
|
|
||
| # evaluation | ||
| self.assert_context_manager_called_with_substr(dummy_error) | ||
|
|
||
| def assert_context_manager_called_with(self, error): | ||
| assert self.mock_context_exit.call_count == 1 | ||
| assert self.mock_context_exit.call_args[0][1] is error | ||
|
|
||
| def assert_context_manager_called_with_substr(self, error): | ||
| assert self.mock_context_exit.call_count == 1 | ||
| assert str(error) in str(self.mock_context_exit.call_args[0][1]) | ||
|
|
||
|
|
||
| class MockAiohttpResponse: | ||
| def __init__(self, json_data, status_code): | ||
| self._json_data = json_data | ||
| self.status = status_code | ||
|
|
||
| async def json(self): | ||
| return self._json_data | ||
|
|
||
| def release(self): | ||
| pass | ||
|
|
||
| async def __aenter__(self): | ||
| return self | ||
|
|
||
| async def __aexit__(self, exc_type, exc_val, exc_tb): | ||
| pass | ||
|
|
||
|
|
||
| class TestCupraGetStatus: | ||
| @pytest.fixture | ||
| def mock_session(self): | ||
| session = MagicMock() | ||
|
|
||
| async def get_side_effect(*args, **kwargs): | ||
| return session.get.return_value | ||
| session.get = MagicMock(side_effect=get_side_effect) | ||
| return session | ||
|
|
||
| @pytest.fixture | ||
| def cupra_instance(self, mock_session): | ||
| instance = CupraApi(mock_session) | ||
| instance.set_vin("test_vin") | ||
| instance.headers = {"Authorization": "Bearer test_token"} | ||
| return instance | ||
|
|
||
| def test_get_status_success(self, cupra_instance, mock_session): | ||
| # setup | ||
| status_response_data = { | ||
| "status": { | ||
| "battery": { | ||
| "currentSOC_pct": 67, | ||
| "cruisingRangeElectric_km": 305, | ||
| "carCapturedTimestamp": "2026-03-29T11:20:00Z" | ||
| } | ||
| } | ||
| } | ||
| mileage_response_data = { | ||
| "mileageKm": 77889 | ||
| } | ||
| responses = [ | ||
| MockAiohttpResponse(status_response_data, 200), | ||
| MockAiohttpResponse(mileage_response_data, 200) | ||
| ] | ||
|
|
||
| async def side_effect_func(*args, **kwargs): | ||
| return responses.pop(0) | ||
| mock_session.get.side_effect = side_effect_func | ||
|
|
||
| # execution | ||
| status = asyncio.run(cupra_instance.get_status()) | ||
|
|
||
| # evaluation | ||
| assert status['charging']['batteryStatus']['value']['currentSOC_pct'] == 67 | ||
| assert status['charging']['batteryStatus']['value']['cruisingRangeElectric_km'] == 305 | ||
| assert status['charging']['batteryStatus']['value']['carCapturedTimestamp'] == "2026-03-29T11:20:00Z" | ||
| assert status['charging']['batteryStatus']['value']['odometer'] == 77889 | ||
| assert mock_session.get.call_count == 2 | ||
| mock_session.get.assert_any_call( | ||
| "https://ola.prod.code.seat.cloud.vwgroup.com/vehicles/test_vin/charging/status", | ||
| headers=cupra_instance.headers | ||
| ) | ||
| mock_session.get.assert_any_call( | ||
| "https://ola.prod.code.seat.cloud.vwgroup.com/v1/vehicles/test_vin/mileage", | ||
| headers=cupra_instance.headers | ||
| ) | ||
|
|
||
| def test_get_status_mileage_error_sets_odometer_none(self, cupra_instance, mock_session): | ||
| # setup | ||
| status_response_data = { | ||
| "status": { | ||
| "battery": { | ||
| "currentSOC_pct": 67, | ||
| "cruisingRangeElectric_km": 305, | ||
| "carCapturedTimestamp": "2026-03-29T11:20:00Z" | ||
| } | ||
| } | ||
| } | ||
| mileage_error_response = { | ||
| "error": "service_unavailable" | ||
| } | ||
| responses = [ | ||
| MockAiohttpResponse(status_response_data, 200), | ||
| MockAiohttpResponse(mileage_error_response, 503) | ||
| ] | ||
|
|
||
| async def side_effect_func(*args, **kwargs): | ||
| return responses.pop(0) | ||
| mock_session.get.side_effect = side_effect_func | ||
|
|
||
| # execution | ||
| status = asyncio.run(cupra_instance.get_status()) | ||
|
|
||
| # evaluation | ||
| assert status['charging']['batteryStatus']['value']['currentSOC_pct'] == 67 | ||
| assert status['charging']['batteryStatus']['value']['cruisingRangeElectric_km'] == 305 | ||
| assert status['charging']['batteryStatus']['value']['carCapturedTimestamp'] == "2026-03-29T11:20:00Z" | ||
| assert status['charging']['batteryStatus']['value']['odometer'] is None |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.