Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a0995ed
replace gmqtt with aiomqtt
bj00rn Jul 14, 2025
7ce9d6c
Set default port for TLS to 8883, fallback to 1883 if no port and TCP…
bj00rn Jul 14, 2025
131bb22
fix keepalive
bj00rn Oct 7, 2025
619c4d6
fix client not reconnecting
bj00rn Nov 18, 2025
b033b55
retain birth/keepalive message
bj00rn Nov 25, 2025
5243c6c
fix(tests): update publish mock signatures for aiomqtt migration
nanomad Jun 24, 2026
85e6091
fix: restore *, retain=True defaults on all publish_* methods
nanomad Jun 24, 2026
2a87077
style: apply ruff formatting
nanomad Jun 24, 2026
9ee28e6
fix(log_publisher): forward retain flag to internal_publish
nanomad Jun 24, 2026
9113cfd
fix(mqtt_publisher): read message.retain directly in _on_message
nanomad Jun 24, 2026
30f7bc2
fix(mqtt_publisher): call on_mqtt_reconnected after broker reconnect
nanomad Jun 24, 2026
6310e7f
fix(mqtt_publisher): restore imported_energy_topic subscription
nanomad Jun 24, 2026
ec833b3
fix(mqtt_publisher): add missing f-prefix to subscribe error log
nanomad Jun 24, 2026
3fd5a96
fix(mqtt_publisher): guard connect() against unconfigured host
nanomad Jun 24, 2026
3f62666
fix(tests): correct misleading test name for clear_topic
nanomad Jun 24, 2026
105627d
fix(mqtt_publisher): remove spurious "a" suffix from client identifier
nanomad Jun 24, 2026
5cc59b0
fix(mqtt_publisher): exit immediately on permanent MQTT connect failures
nanomad Jun 24, 2026
dcddcca
refactor(mqtt_publisher): use paho CONNACK constants instead of magic…
nanomad Jun 24, 2026
a8bc3c6
refactor(mqtt_publisher): replace paho constants with local MQTT 3.1.…
nanomad Jun 24, 2026
7e55aee
fix(mqtt_publisher): exponential backoff on reconnect with 5 min cap
nanomad Jun 24, 2026
27677a6
fix(mqtt_publisher): use create_task instead of run_coroutine_threadsafe
nanomad Jun 24, 2026
4fecfcd
fix(mqtt_publisher): fix fatal CONNACK detection and connect() hang
nanomad Jun 24, 2026
8968d78
Merge pull request #457 from SAIC-iSmart-API/chore/aiomqtt
nanomad Jun 24, 2026
17bd181
fix(ha): emit retain as JSON boolean in discovery payloads
nanomad Jun 24, 2026
11af448
style: apply ruff formatting
nanomad Jun 24, 2026
cdeb882
Merge pull request #459 from SAIC-iSmart-API/fix/ha-discovery-retain-…
nanomad Jun 24, 2026
77c4581
fix(extractors): suppress SoC kWh warning for non-EV vehicles
nanomad Jun 24, 2026
5acfbd4
fix(command): publish command results with retain=False
nanomad Jun 24, 2026
923038a
Merge pull request #461 from SAIC-iSmart-API/fix/command-result-retai…
nanomad Jun 24, 2026
2070279
fix(tls): only apply tls_insecure when a custom CA cert is configured
nanomad Jun 24, 2026
c16515e
fix(tls): warn on hostname-check bypass regardless of custom CA cert
nanomad Jun 24, 2026
d806387
Merge pull request #462 from SAIC-iSmart-API/fix/tls-insecure-require…
nanomad Jun 24, 2026
d202027
Merge pull request #460 from SAIC-iSmart-API/fix/extract-soc-kwh-warn…
nanomad Jun 24, 2026
953a24b
chore: add 0.13.0 release notes to CHANGELOG
nanomad Jun 24, 2026
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
36 changes: 36 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
# Change Log

## 0.13.0

### Changed

* Replace gmqtt with aiomqtt for MQTT connectivity. The new client uses a
pure-asyncio architecture — no more `run_coroutine_threadsafe` cross-thread
calls. Reconnect now uses exponential backoff (5 s → 10 s → … → 5 min)
instead of a fixed interval (#457).

### Fixed

* Exit immediately on permanent MQTT connection failures (bad credentials,
unknown protocol version, identifier rejected, not authorised) instead of
looping forever (#457).

* Restore MQTT subscriptions and trigger HA re-discovery on broker reconnect
after a transient disconnect (#457).

* Suppress spurious `WARNING: Could not extract a valid SoC kWh` log on every
poll for non-EV vehicles. The warning is preserved for EVs where the BMS
returns invalid data (#460).

* Publish command result topics (`Success` / `Failed: …`) with `retain=False`
so a stale result from a previous session does not persist on the broker
across restarts (#461).

* Emit `retain` as a JSON boolean (`true`/`false`) in Home Assistant MQTT
discovery payloads instead of the string `"true"`/`"false"` that the HA
schema requires (#459).

* Warn when TLS hostname verification is disabled regardless of whether a
custom CA certificate is configured. Self-signed cert users (who typically
have no CA file) now see the warning too (#462).

**Full Changelog**: https://github.com/SAIC-iSmart-API/saic-python-mqtt-gateway/compare/0.12.0...0.13.0

## 0.12.0

### Added
Expand Down
47 changes: 31 additions & 16 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ requires-python = '>=3.12,<4.0'
dependencies = [
"saic-ismart-client-ng (>=0.9.3,<0.10.0)",
'httpx (>=0.28.1,<0.29.0)',
'gmqtt (>=0.7.0,<0.8.0)',
'inflection (>=0.5.1,<0.6.0)',
'apscheduler (>=3.11.0,<4.0.0)',
'python-dotenv (>=1.1.1,<2.0.0)',
"aiomqtt (>=2.4.0,<3.0.0)",
]

[project.urls]
Expand Down
7 changes: 5 additions & 2 deletions src/configuration/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from __future__ import annotations

from enum import Enum
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Literal

if TYPE_CHECKING:
from zoneinfo import ZoneInfo

from integrations.openwb.charging_station import ChargingStation


Transport = Literal["tcp", "websockets"]


class TransportProtocol(Enum):
def __init__(self, transport_mechanism: str, with_tls: bool) -> None:
def __init__(self, transport_mechanism: Transport, with_tls: bool) -> None:
self.transport_mechanism = transport_mechanism
self.with_tls = with_tls

Expand Down
11 changes: 7 additions & 4 deletions src/configuration/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,18 @@ def __parse_mqtt_transport(args: Namespace, config: Configuration) -> None:
args.tls_server_cert_check_hostname
)
else:
msg = f"Invalid MQTT URI scheme: {parse_result.scheme}, use tcp or ws"
msg = f"Invalid MQTT URI scheme: {parse_result.scheme}, use tls, tcp or ws"
raise SystemExit(msg)

if parse_result.port:
config.mqtt_port = parse_result.port
elif config.mqtt_transport_protocol == TransportProtocol.TCP:
config.mqtt_port = 1883
else:
elif config.mqtt_transport_protocol == TransportProtocol.TLS:
config.mqtt_port = 8883
elif config.mqtt_transport_protocol == TransportProtocol.WS:
config.mqtt_port = 9001
else:
# fallback to default mqtt port
config.mqtt_port = 1883
config.mqtt_host = str(parse_result.hostname)


Expand Down
12 changes: 6 additions & 6 deletions src/extractors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ def extract_soc_kwh(
charge_status: ChrgMgmtDataRespProcessingResult | None,
soc: float | None,
) -> float | None:
if (
charge_status is not None
and (raw_soc_kwh := charge_status.soc_kwh) is not None
and (soc_kwh := __validate_and_convert_soc_kwh(raw_soc_kwh)) is not None
):
if charge_status is None:
return None

if (raw_soc_kwh := charge_status.soc_kwh) is not None and (
soc_kwh := __validate_and_convert_soc_kwh(raw_soc_kwh)
) is not None:
LOG.debug("SoC kWh derived from realtimePower")
return soc_kwh

if (
soc is not None
and charge_status is not None
and (
capacity := __validate_and_convert_soc_kwh(
charge_status.real_total_battery_capacity
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/vehicle_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __report_command_failure(
else:
LOG.error("Command %s failed: %s", command, detail)
try:
self.publisher.publish_str(result_topic, f"Failed: {detail}")
self.publisher.publish_str(result_topic, f"Failed: {detail}", retain=False)
except Exception:
LOG.warning(
"Failed to publish failure result for command %s",
Expand Down Expand Up @@ -138,7 +138,7 @@ async def __execute_mqtt_command_handler(

try:
execution_result = await handler.handle(payload, retained=retained)
self.publisher.publish_str(result_topic, "Success")
self.publisher.publish_str(result_topic, "Success", retain=False)
if execution_result.force_refresh:
self.vehicle_state.set_refresh_mode(
RefreshMode.FORCE, f"after command execution on topic {topic}"
Expand All @@ -165,7 +165,7 @@ async def __execute_mqtt_command_handler(
return
try:
execution_result = await handler.handle(payload, retained=retained)
self.publisher.publish_str(result_topic, "Success")
self.publisher.publish_str(result_topic, "Success", retain=False)
if execution_result.force_refresh:
self.vehicle_state.set_refresh_mode(
RefreshMode.FORCE,
Expand Down
6 changes: 3 additions & 3 deletions src/integrations/home_assistant/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _publish_select(
"command_topic": self._get_command_topic(topic),
"value_template": value_template,
"command_template": command_template,
"retain": str(retain).lower(),
"retain": retain,
"options": options,
"enabled_by_default": enabled,
}
Expand Down Expand Up @@ -87,7 +87,7 @@ def _publish_text(
"command_topic": self._get_command_topic(topic),
"value_template": value_template,
"command_template": command_template,
"retain": str(retain).lower(),
"retain": retain,
"enabled_by_default": enabled,
}
if min_value is not None:
Expand Down Expand Up @@ -153,7 +153,7 @@ def _publish_number(
"state_topic": self._get_state_topic(topic),
"command_topic": self._get_command_topic(topic),
"value_template": value_template,
"retain": str(retain).lower(),
"retain": retain,
"mode": mode,
"min": min_value,
"max": max_value,
Expand Down
2 changes: 1 addition & 1 deletion src/log_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

MODULES_DEFAULT_LOG_LEVEL = {
"asyncio": "WARNING",
"gmqtt": "WARNING",
"aiomqtt": "WARNING",
"httpcore": "WARNING",
"httpx": "WARNING",
"saic_ismart_client_ng": "WARNING",
Expand Down
1 change: 1 addition & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
configuration = process_command_line()

mqtt_gateway = MqttGateway(configuration)

asyncio.run(mqtt_gateway.run(), debug=debug_log_enabled())
39 changes: 33 additions & 6 deletions src/publisher/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,55 @@ def publish_json(
no_prefix: bool = False,
*,
retain: bool = True,
qos: int = 0,
) -> None:
raise NotImplementedError

@abstractmethod
def publish_str(
self, key: str, value: str, no_prefix: bool = False, *, retain: bool = True
self,
key: str,
value: str,
no_prefix: bool = False,
*,
retain: bool = True,
qos: int = 0,
) -> None:
raise NotImplementedError

@abstractmethod
def publish_int(
self, key: str, value: int, no_prefix: bool = False, *, retain: bool = True
self,
key: str,
value: int,
no_prefix: bool = False,
*,
retain: bool = True,
qos: int = 0,
) -> None:
raise NotImplementedError

@abstractmethod
def publish_bool(
self, key: str, value: bool, no_prefix: bool = False, *, retain: bool = True
self,
key: str,
value: bool,
no_prefix: bool = False,
*,
retain: bool = True,
qos: int = 0,
) -> None:
raise NotImplementedError

@abstractmethod
def publish_float(
self, key: str, value: float, no_prefix: bool = False, *, retain: bool = True
self,
key: str,
value: float,
no_prefix: bool = False,
*,
retain: bool = True,
qos: int = 0,
) -> None:
raise NotImplementedError

Expand Down Expand Up @@ -173,7 +198,7 @@ def publish(
raise TypeError(msg)

@abstractmethod
def clear_topic(self, key: str, no_prefix: bool = False) -> None:
def clear_topic(self, key: str, no_prefix: bool = False, qos: int = 0) -> None:
raise NotImplementedError

def get_mqtt_account_prefix(self) -> str:
Expand Down Expand Up @@ -249,7 +274,9 @@ def __anonymize(self, data: T) -> T:
return data

def keepalive(self) -> None:
self.publish_str(mqtt_topics.INTERNAL_LWT, "online", False)
self.publish_str(
mqtt_topics.INTERNAL_LWT, "online", no_prefix=False, retain=True, qos=1
)

@staticmethod
def anonymize_str(value: str) -> str:
Expand Down
Loading
Loading