From 6a65428216200131c17be7adfb371f35575a1e2f Mon Sep 17 00:00:00 2001 From: Giovanni Condello Date: Sat, 14 Nov 2026 11:54:06 +0100 Subject: [PATCH] feat: hint at BATTERY_CAPACITY_MAPPING for the ambiguous MG4 Urban pack size The AH4EM series is sold with 43 kWh and 54 kWh packs and the series does not tell them apart. Keep the defaults, log a one-time hint and document the override. --- README.md | 2 +- src/vehicle_info.py | 15 +++++++++++++-- tests/test_vehicle_info.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7f828a4..db3e8eb 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ When using combinations of configuration methods, the order of precedence is as | --saic-device-app-id | SAIC_DEVICE_APP_ID | SAIC app ID sent as the deviceId suffix at login. Default comes from the preset (com.saicmotor.europecar for MG, com.saicmaxus.ismarteu for MAXUS). | | --saic-relogin-delay | SAIC_RELOGIN_DELAY | The gateway detects logins from other devices (e.g. the iSMART app). It then pauses it's activity for 900 seconds (default value). The delay can be configured with this parameter. | | --messages-request-interval | MESSAGES_REQUEST_INTERVAL | The interval for retrieving messages in seconds. Default is 60 seconds. | -| --battery-capacity-mapping | BATTERY_CAPACITY_MAPPING | Mapping of VIN to full battery capacity. Multiple mappings can be provided separated by ',' Example: LSJXXXX=54.0,LSJYYYY=64.0 | +| --battery-capacity-mapping | BATTERY_CAPACITY_MAPPING | Mapping of VIN to full battery capacity. Multiple mappings can be provided separated by ',' Example: LSJXXXX=54.0,LSJYYYY=64.0. Use it when the detected capacity is wrong, e.g. the MG4 Urban, which is sold with 43 kWh and 54 kWh packs under the same series. | | --charge-min-percentage | CHARGE_MIN_PERCENTAGE | How many % points we should try to refresh the charge state. 1.0 by default | | --account-refresh-interval | ACCOUNT_REFRESH_INTERVAL | Interval in seconds for refreshing account-level data (vehicle list, timezone). Default is 86400 (24 hours). | | --saic-user-timezone | SAIC_USER_TIMEZONE | Force the account timezone instead of trusting the SAIC API value. Accepts an IANA name (e.g. `Australia/Sydney`) or `GMT+HH:MM`. Mismatches with the API offset are logged. | diff --git a/src/vehicle_info.py b/src/vehicle_info.py index e98ffbb..eaa994f 100644 --- a/src/vehicle_info.py +++ b/src/vehicle_info.py @@ -32,6 +32,7 @@ def __init__( self.__properties_from_configuration(self.configuration) ) self.__custom_battery_capacity: float | None = custom_battery_capacity + self.__urban_capacity_hint_logged = False @property def custom_battery_capacity(self) -> float | None: @@ -149,8 +150,18 @@ def __mg4_real_battery_capacity(self) -> float | None: @property def __mg4_urban_real_battery_capacity(self) -> float | None: - # MG4 Urban: "L" suffix = low/standard trim (43kWh LFP, series AH4EM L) - # All other variants (e.g. AH4EM S) default to 54kWh LFP + # MG4 Urban is sold with a 43kWh and a 54kWh LFP pack, in several trims. + # The series alone does not tell them apart: "AH4EM L" was reported as + # 43kWh, while "AH4EM S" was reported both as 54kWh and as 43kWh. + # Keep the historical guess, and tell the owner how to override it. + if not self.__urban_capacity_hint_logged: + self.__urban_capacity_hint_logged = True + LOG.info( + f"MG4 Urban series='{self.series}' is sold with a 43kWh and a 54kWh " + "battery, and the series does not tell which one this car has. " + "If the capacity is wrong, set BATTERY_CAPACITY_MAPPING " + f"({self.vin}=43.0 or {self.vin}=54.0)" + ) if self.series == "AH4EM L": return 43.0 return 54.0 diff --git a/tests/test_vehicle_info.py b/tests/test_vehicle_info.py index d1d4121..ef3653e 100644 --- a/tests/test_vehicle_info.py +++ b/tests/test_vehicle_info.py @@ -1,10 +1,16 @@ from __future__ import annotations +import logging +from typing import TYPE_CHECKING + from saic_ismart_client_ng.api.vehicle.schema import VehicleModelConfiguration, VinInfo from tests.common_mocks import VIN from vehicle_info import VehicleInfo +if TYPE_CHECKING: + import pytest + def _make_vehicle_info( series: str, @@ -55,3 +61,32 @@ def test_custom_capacity_overrides_lookup(self) -> None: "AH4EM L", model="MG4 EV URBAN", custom_battery_capacity=54.0 ) assert vi.real_battery_capacity == 54.0 + + +class TestMg4UrbanCapacityHint: + @staticmethod + def _info(series: str, custom: float | None = None) -> VehicleInfo: + vin_info = VinInfo() + vin_info.vin = VIN + vin_info.series = series + vin_info.modelName = "MG4 EV URBAN" + vin_info.modelYear = "2025" + return VehicleInfo(vin_info, custom) + + def test_hint_logged_once(self, caplog: pytest.LogCaptureFixture) -> None: + info = self._info("AH4EM S") + with caplog.at_level(logging.INFO, logger="vehicle_info"): + assert info.real_battery_capacity == 54.0 + assert info.real_battery_capacity == 54.0 + hints = [r for r in caplog.records if "BATTERY_CAPACITY_MAPPING" in r.message] + assert len(hints) == 1 + + def test_no_hint_with_custom_capacity( + self, caplog: pytest.LogCaptureFixture + ) -> None: + info = self._info("AH4EM S", 43.0) + with caplog.at_level(logging.INFO, logger="vehicle_info"): + assert info.real_battery_capacity == 43.0 + assert not [ + r for r in caplog.records if "BATTERY_CAPACITY_MAPPING" in r.message + ]