From 562ab09bcec79390fc4b97487d7a5e3aead11d9b Mon Sep 17 00:00:00 2001 From: Giovanni Condello Date: Sat, 14 Nov 2026 14:49:37 +0100 Subject: [PATCH] fix: treat the ZS32 hybrid as a non-EV and extend its A/C range ZS32 (MG ZS Hybrid+) has no plug, so skip charge polling and disable the battery and charging entities by default. Set its A/C range to 17-33 as the iSmart app does, with the app's temperature - 14 wire index. --- src/integrations/home_assistant/base.py | 26 ++++++++----- src/integrations/home_assistant/discovery.py | 40 ++++++++++++++++++++ src/vehicle_info.py | 17 ++++++--- tests/test_ha_discovery_feature_gating.py | 20 +++++++++- tests/test_vehicle_info.py | 32 ++++++++++++++++ 5 files changed, 118 insertions(+), 17 deletions(-) diff --git a/src/integrations/home_assistant/base.py b/src/integrations/home_assistant/base.py index 942533c..3268cf4 100644 --- a/src/integrations/home_assistant/base.py +++ b/src/integrations/home_assistant/base.py @@ -36,6 +36,14 @@ def _publish_ha_discovery_message( ) -> str: raise NotImplementedError + def _is_enabled_by_default( + self, + topic: str, # noqa: ARG002 + enabled: bool, + ) -> bool: + """Lets a subclass turn an entity off, e.g. for cars that lack it.""" + return enabled + def _publish_select( self, topic: str, @@ -57,7 +65,7 @@ def _publish_select( "command_template": command_template, "retain": retain, "options": options, - "enabled_by_default": enabled, + "enabled_by_default": self._is_enabled_by_default(topic, enabled), } if entity_category is not None: payload["entity_category"] = entity_category @@ -88,7 +96,7 @@ def _publish_text( "value_template": value_template, "command_template": command_template, "retain": retain, - "enabled_by_default": enabled, + "enabled_by_default": self._is_enabled_by_default(topic, enabled), } if min_value is not None: payload["min"] = min_value @@ -120,7 +128,7 @@ def _publish_binary_sensor( "value_template": value_template, "payload_on": payload_on, "payload_off": payload_off, - "enabled_by_default": enabled, + "enabled_by_default": self._is_enabled_by_default(topic, enabled), } if device_class is not None: payload["device_class"] = device_class @@ -158,7 +166,7 @@ def _publish_number( "min": min_value, "max": max_value, "step": step, - "enabled_by_default": enabled, + "enabled_by_default": self._is_enabled_by_default(topic, enabled), } if entity_category is not None: payload["entity_category"] = entity_category @@ -195,7 +203,7 @@ def _publish_switch( "payload_off": payload_off, "optimistic": False, "qos": 0, - "enabled_by_default": enabled, + "enabled_by_default": self._is_enabled_by_default(topic, enabled), } if icon is not None: payload["icon"] = icon @@ -224,7 +232,7 @@ def _publish_lock( "state_unlocked": state_unlocked, "optimistic": False, "qos": 0, - "enabled_by_default": enabled, + "enabled_by_default": self._is_enabled_by_default(topic, enabled), } if icon is not None: payload["icon"] = icon @@ -260,7 +268,7 @@ def _publish_cover( "state_closed": state_closed, "optimistic": False, "qos": 0, - "enabled_by_default": enabled, + "enabled_by_default": self._is_enabled_by_default(state_topic, enabled), } if device_class is not None: payload["device_class"] = device_class @@ -285,7 +293,7 @@ def _publish_event( payload: dict[str, Any] = { "state_topic": self._get_state_topic(topic), "event_types": event_types, - "enabled_by_default": enabled, + "enabled_by_default": self._is_enabled_by_default(topic, enabled), } if entity_category is not None: payload["entity_category"] = entity_category @@ -315,7 +323,7 @@ def _publish_sensor( payload = { "state_topic": self._get_state_topic(topic), "value_template": value_template, - "enabled_by_default": enabled, + "enabled_by_default": self._is_enabled_by_default(topic, enabled), } if entity_category is not None: payload["entity_category"] = entity_category diff --git a/src/integrations/home_assistant/discovery.py b/src/integrations/home_assistant/discovery.py index 7076fe5..f0ac1b4 100644 --- a/src/integrations/home_assistant/discovery.py +++ b/src/integrations/home_assistant/discovery.py @@ -30,6 +30,40 @@ LOG = logging.getLogger(__name__) +# Entities that only make sense on a car with a plug and a traction battery. +# They stay published for the other cars but are disabled by default. +EV_ONLY_TOPICS: frozenset[str] = frozenset( + { + mqtt_topics.DRIVETRAIN_CHARGER_CONNECTED, + mqtt_topics.DRIVETRAIN_CHARGING, + mqtt_topics.DRIVETRAIN_CHARGING_STOP_REASON, + mqtt_topics.DRIVETRAIN_CHARGING_LAST_START, + mqtt_topics.DRIVETRAIN_CHARGING_LAST_END, + mqtt_topics.DRIVETRAIN_CHARGING_SCHEDULE, + mqtt_topics.DRIVETRAIN_CHARGING_TYPE, + mqtt_topics.DRIVETRAIN_CHARGING_CABLE_LOCK, + mqtt_topics.DRIVETRAIN_CHARGECURRENT_LIMIT, + mqtt_topics.DRIVETRAIN_BATTERY_HEATING, + mqtt_topics.DRIVETRAIN_BATTERY_HEATING_STOP_REASON, + mqtt_topics.DRIVETRAIN_BATTERY_HEATING_SCHEDULE, + mqtt_topics.DRIVETRAIN_HV_BATTERY_ACTIVE, + mqtt_topics.DRIVETRAIN_CURRENT, + mqtt_topics.DRIVETRAIN_VOLTAGE, + mqtt_topics.DRIVETRAIN_POWER, + mqtt_topics.DRIVETRAIN_POWER_USAGE_OF_DAY, + mqtt_topics.DRIVETRAIN_POWER_USAGE_SINCE_LAST_CHARGE, + mqtt_topics.DRIVETRAIN_MILEAGE_SINCE_LAST_CHARGE, + mqtt_topics.DRIVETRAIN_REMAINING_CHARGING_TIME, + mqtt_topics.DRIVETRAIN_LAST_CHARGE_ENDING_POWER, + mqtt_topics.DRIVETRAIN_RANGE, + mqtt_topics.DRIVETRAIN_SOC, + mqtt_topics.DRIVETRAIN_SOC_KWH, + mqtt_topics.DRIVETRAIN_SOC_TARGET, + mqtt_topics.DRIVETRAIN_TOTAL_BATTERY_CAPACITY, + } +) + + class HomeAssistantDiscovery(HomeAssistantDiscoveryBase): def __init__( self, @@ -1000,6 +1034,12 @@ def __get_system_topic(self, topic: str) -> str: return publisher.get_topic(topic, no_prefix=False) return topic + @override + def _is_enabled_by_default(self, topic: str, enabled: bool) -> bool: + if topic in EV_ONLY_TOPICS and not self.__vin_info.is_ev: + return False + return enabled + @override def _get_state_topic(self, raw_topic: str) -> str: return self.__get_vehicle_topic(raw_topic) diff --git a/src/vehicle_info.py b/src/vehicle_info.py index e9b23c3..77833dd 100644 --- a/src/vehicle_info.py +++ b/src/vehicle_info.py @@ -57,21 +57,25 @@ def __properties_from_configuration( return properties def get_ac_temperature_idx(self, remote_ac_temperature: int) -> int: - if self.series.startswith("EH32"): + # The app sends temperature - 14. The offset below matches that for the + # minimum temperature of each series. + if self.series.startswith(("EH32", "ZS32")): return 3 + remote_ac_temperature - self.min_ac_temperature return 2 + remote_ac_temperature - self.min_ac_temperature @property def min_ac_temperature(self) -> int: - if self.series.startswith("EH32"): + if self.series.startswith(("EH32", "ZS32")): return 17 - if self.series.startswith("MZS3E"): - return 16 return 16 @property def max_ac_temperature(self) -> int: - if self.series.startswith("EH32") or self.series.startswith("MZS3E"): + if self.series.startswith("ZS32"): + # The app caps this series at 31 or 33 depending on its region. The car + # panel of the reported ZS Hybrid+ goes up to 32 and then "HI". + return 33 + if self.series.startswith(("EH32", "MZS3E")): return 31 return 28 @@ -102,7 +106,8 @@ def __get_mask_by_code(self, property_name: str, index: int) -> bool: @property def is_ev(self) -> bool: - return not self.series.startswith("ZP22") + # Hybrids without a plug: the charge management data is not available + return not self.series.startswith(("ZP22", "ZS32")) @property def has_fossil_fuel(self) -> bool: diff --git a/tests/test_ha_discovery_feature_gating.py b/tests/test_ha_discovery_feature_gating.py index b56a23f..0da172f 100644 --- a/tests/test_ha_discovery_feature_gating.py +++ b/tests/test_ha_discovery_feature_gating.py @@ -49,14 +49,14 @@ ] -def _publish_discovery(codes: dict[str, str]) -> dict[str, str]: +def _publish_discovery(codes: dict[str, str], series: str = "EH32 S") -> dict[str, str]: config = Configuration() config.anonymized_publishing = False config.ha_discovery_prefix = "homeassistant" publisher = MessageCapturingConsolePublisher(config) vin_info = VinInfo() vin_info.vin = VIN - vin_info.series = "EH32 S" + vin_info.series = series vin_info.vehicleModelConfiguration = [ VehicleModelConfiguration(itemCode=code, itemName=code, itemValue=value) for code, value in codes.items() @@ -122,3 +122,19 @@ def test_door_mask_gates_single_entities() -> None: assert _enabled(published, "binary_sensor", "door_passenger") assert not _enabled(published, "binary_sensor", "door_rear_left") assert not _enabled(published, "binary_sensor", "door_rear_right") + + +def _soc_enabled(series: str) -> bool: + published = _publish_discovery({}, series=series) + payload = json.loads(published[_topic("sensor", "soc")]) + enabled: bool = payload["enabled_by_default"] + return enabled + + +@pytest.mark.parametrize("series", ["ZP22 EU", "ZS32 EU"]) +def test_battery_entities_disabled_by_default_on_hybrids(series: str) -> None: + assert not _soc_enabled(series) + + +def test_battery_entities_enabled_on_ev() -> None: + assert _soc_enabled("EH32 S") diff --git a/tests/test_vehicle_info.py b/tests/test_vehicle_info.py index 3e5cb59..9fec61a 100644 --- a/tests/test_vehicle_info.py +++ b/tests/test_vehicle_info.py @@ -126,3 +126,35 @@ def test_no_hint_with_custom_capacity( assert not [ r for r in caplog.records if "BATTERY_CAPACITY_MAPPING" in r.message ] + + +class TestZs32Hybrid: + @staticmethod + def _info(series: str) -> VehicleInfo: + vin_info = VinInfo() + vin_info.vin = VIN + vin_info.series = series + vin_info.modelName = "ZS" + vin_info.modelYear = "2024" + return VehicleInfo(vin_info, None) + + def test_zs32_is_not_an_ev(self) -> None: + assert not self._info("ZS32 EU").is_ev + assert self._info("ZS32 EU").has_fossil_fuel + + def test_zs32_ac_range_and_index_follow_the_app(self) -> None: + info = self._info("ZS32 EU") + assert (info.min_ac_temperature, info.max_ac_temperature) == (17, 33) + # The app sends temperature - 14 + assert info.get_ac_temperature_idx(22) == 22 - 14 + assert info.get_ac_temperature_idx(33) == 33 - 14 + + def test_other_series_ac_values_unchanged(self) -> None: + for series, low, high in ( + ("EH32 S", 17, 31), + ("MZS3E", 16, 31), + ("AH4EM L", 16, 28), + ): + info = self._info(series) + assert (info.min_ac_temperature, info.max_ac_temperature) == (low, high) + assert info.get_ac_temperature_idx(22) == 22 - 14