Skip to content

Commit 597116d

Browse files
committed
Fix mypy errors for paho-mqtt v2 and scripts
- Remove loop_stop(force=) calls (parameter removed in paho v2) - Remove dead on_connack callback (on_connect handles CONNACK in v2) - Add missing return statement in stream_cardata.py main() - Fix Iterable→Sequence type for argparse.parse_args() compatibility
1 parent edefa2a commit 597116d

4 files changed

Lines changed: 7 additions & 21 deletions

File tree

custom_components/cardata/stream.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,25 +147,16 @@ def _done_callback(future: ConcurrentFuture[Any]) -> None:
147147
future = asyncio.run_coroutine_threadsafe(coro, self.hass.loop)
148148
future.add_done_callback(_done_callback)
149149

150-
def _safe_loop_stop(self, client: mqtt.Client, force: bool = False) -> None:
150+
def _safe_loop_stop(self, client: mqtt.Client) -> None:
151151
"""Safely stop the MQTT loop, handling any exceptions.
152152
153153
This ensures cleanup continues even if loop_stop() fails, preventing
154154
resource leaks from zombie MQTT threads.
155155
"""
156156
try:
157-
if force:
158-
client.loop_stop(force=True)
159-
else:
160-
client.loop_stop()
157+
client.loop_stop()
161158
except Exception as err:
162159
_LOGGER.warning("Error stopping MQTT loop: %s", err)
163-
# Try force stop as fallback
164-
if not force:
165-
try:
166-
client.loop_stop(force=True)
167-
except Exception:
168-
pass
169160

170161
async def async_start(self) -> None:
171162
# Acquire lock with timeout to prevent indefinite blocking
@@ -515,7 +506,7 @@ def _handle_connect(self, client: mqtt.Client, userdata, flags, rc) -> None:
515506
if rc == 5 and self._last_disconnect is not None and now - self._last_disconnect < 10:
516507
if debug_enabled():
517508
_LOGGER.debug("BMW MQTT connection refused shortly after disconnect; scheduling retry")
518-
self._safe_loop_stop(client, force=True)
509+
self._safe_loop_stop(client)
519510
self._client = None
520511
stream_reconnect.schedule_retry(self, 3)
521512
return

scripts/fetch_cardata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import json
3333
import sys
3434
from pathlib import Path
35-
from typing import Any, Dict, Iterable, Optional
35+
from typing import Any, Dict, Optional, Sequence
3636

3737
import requests
3838

@@ -134,7 +134,7 @@ def resolve_base_url(base_url: str, openapi_path: Optional[str], server_index: i
134134
return url
135135

136136

137-
def main(argv: Optional[Iterable[str]] = None) -> int:
137+
def main(argv: Optional[Sequence[str]] = None) -> int:
138138
parser = argparse.ArgumentParser(
139139
description=(
140140
"Fetch BMW CarData information using an access token."

scripts/stream_cardata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ def main() -> int:
141141
client.loop_forever()
142142
except KeyboardInterrupt:
143143
print("Disconnected by user")
144-
client.disconnect()
145-
return 0
144+
client.disconnect()
145+
return 0
146146

147147

148148
if __name__ == "__main__":

scripts/stream_loop.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ def build_client(
135135
client.reconnect_delay_set(min_delay=5, max_delay=60)
136136

137137
client.on_connect = on_connect
138-
client.on_connack = on_connack
139138
client.on_message = on_message
140139
client.on_disconnect = on_disconnect
141140
client.on_subscribe = on_subscribe
@@ -167,10 +166,6 @@ def on_message(client: mqtt.Client, userdata, msg: mqtt.MQTTMessage):
167166
print(f"[{timestamp}] {msg.topic}: {payload}")
168167

169168

170-
def on_connack(client, userdata, reason_code, flags, properties): # type: ignore[override]
171-
print(f"CONNACK received: reason_code={reason_code}, flags={flags}")
172-
173-
174169
def on_disconnect(client: mqtt.Client, userdata, disconnect_flags, reason_code, properties=None):
175170
print(f"Disconnected from stream (flags={disconnect_flags}, reason={reason_code}).")
176171
if isinstance(userdata, dict):

0 commit comments

Comments
 (0)