Skip to content

Commit bacacaa

Browse files
committed
fix: pass an explicit TLS version to the MQTT client
paho-mqtt 2.1.0 reads ssl.PROTOCOL_TLSv1_2 when no TLS version is given. On a Python whose ssl module does not define that constant, the MQTT session failed to start with "module 'ssl' has no attribute 'PROTOCOL_TLSv1_2'". Pass ssl.PROTOCOL_TLS_CLIENT, which paho selected anyway, so certificate and hostname checks are unchanged.
1 parent 321c6d5 commit bacacaa

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎roborock/mqtt/roborock_session.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,12 @@ async def _mqtt_client(self, params: MqttParams) -> AsyncIterator[aiomqtt.Client
254254
_LOGGER.debug("Connecting to %s:%s for %s", params.host, params.port, params.username)
255255
tls_params = None
256256
if params.tls:
257-
tls_params = TLSParameters(cert_reqs=ssl.CERT_REQUIRED if params.verify_tls else ssl.CERT_NONE)
257+
# An explicit version avoids paho-mqtt < 2.2 reading ssl.PROTOCOL_TLSv1_2,
258+
# which some Python builds do not define.
259+
tls_params = TLSParameters(
260+
cert_reqs=ssl.CERT_REQUIRED if params.verify_tls else ssl.CERT_NONE,
261+
tls_version=ssl.PROTOCOL_TLS_CLIENT,
262+
)
258263
try:
259264
async with aiomqtt.Client(
260265
hostname=params.host,

‎tests/mqtt/test_roborock_session.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import asyncio
44
import copy
55
import datetime
6+
import ssl
67
from collections.abc import Callable, Generator
78
from typing import Any
89
from unittest.mock import AsyncMock, Mock, patch
910

1011
import aiomqtt
1112
import pytest
13+
from aiomqtt import TLSParameters
1214

1315
from roborock.diagnostics import Diagnostics
1416
from roborock.mqtt.roborock_session import RoborockMqttSession, create_mqtt_session
@@ -559,3 +561,22 @@ async def test_session_defers_reconnect_when_idle(
559561
assert params.diagnostics.as_dict().get("reconnect_deferred", 0) >= 1
560562
finally:
561563
await session.close()
564+
565+
566+
@pytest.mark.parametrize(("verify_tls", "cert_reqs"), [(True, ssl.CERT_REQUIRED), (False, ssl.CERT_NONE)])
567+
async def test_tls_parameters(mock_aenter_client: AsyncMock, verify_tls: bool, cert_reqs: ssl.VerifyMode) -> None:
568+
"""TLS uses an explicit protocol, so paho-mqtt never reads ssl.PROTOCOL_TLSv1_2."""
569+
client_factory = Mock()
570+
client_factory.return_value.__aenter__ = mock_aenter_client
571+
client_factory.return_value.__aexit__ = AsyncMock()
572+
params = copy.deepcopy(FAKE_PARAMS)
573+
params.tls = True
574+
params.verify_tls = verify_tls
575+
576+
with patch("roborock.mqtt.roborock_session.aiomqtt.Client", client_factory):
577+
session = await create_mqtt_session(params)
578+
await session.close()
579+
580+
assert client_factory.call_args.kwargs["tls_params"] == TLSParameters(
581+
cert_reqs=cert_reqs, tls_version=ssl.PROTOCOL_TLS_CLIENT
582+
)

0 commit comments

Comments
 (0)