Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/integrations/home_assistant/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions src/integrations/home_assistant/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions src/vehicle_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
20 changes: 18 additions & 2 deletions tests/test_ha_discovery_feature_gating.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
32 changes: 32 additions & 0 deletions tests/test_vehicle_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading