diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e8a1c96..ba28cbe 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,9 @@ Changelog Unreleased ========== +- Construct the paho-mqtt client with ``CallbackAPIVersion.VERSION2`` and + update ``on_connect`` / ``on_disconnect`` to the v2 callback signatures. + Version 3.0.2 ============= diff --git a/src/libdeye/mqtt_client.py b/src/libdeye/mqtt_client.py index ba9da1a..c7b6455 100644 --- a/src/libdeye/mqtt_client.py +++ b/src/libdeye/mqtt_client.py @@ -391,7 +391,7 @@ def __init__( self._cloud_api = cloud_api self._endpoint = "" self._topic = "" - self._mqtt = mqtt.Client() + self._mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) if tls_context is not None: self._mqtt.tls_set_context(tls_context) else: @@ -420,7 +420,11 @@ def disconnect(self) -> None: def _mqtt_on_connect( self, - *args: Any, + _mqtt: mqtt.Client, + _userdata: Any, + _connect_flags: mqtt.ConnectFlags, + _reason_code: mqtt.ReasonCode, + _properties: mqtt.Properties | None, ) -> None: for topic, callbacks in self._subscribers.items(): if len(callbacks) > 0: @@ -433,10 +437,12 @@ def _mqtt_on_connect( def _mqtt_on_disconnect( self, _mqtt: mqtt.Client, - _userdata: None, - result_code: int, + _userdata: Any, + _disconnect_flags: mqtt.DisconnectFlags, + reason_code: mqtt.ReasonCode, + _properties: mqtt.Properties | None, ) -> None: - if result_code == 0: # User initiated disconnect + if reason_code == 0: # User initiated disconnect return # Update MQTT info and wait for it to complete before reconnecting @@ -449,7 +455,7 @@ def _process_message_payload(self, msg: mqtt.MQTTMessage) -> Any: raise NotImplementedError def _mqtt_on_message( - self, _mqtt: mqtt.Client, _userdata: None, msg: mqtt.MQTTMessage + self, _mqtt: mqtt.Client, _userdata: Any, msg: mqtt.MQTTMessage ) -> None: if msg.topic not in self._subscribers: return diff --git a/tests/test_mqtt_client.py b/tests/test_mqtt_client.py index d87990f..0447ab8 100644 --- a/tests/test_mqtt_client.py +++ b/tests/test_mqtt_client.py @@ -153,7 +153,13 @@ def test_mqtt_on_connect(self, base_client: MockBaseDeyeMqttClient) -> None: # Call _mqtt_on_connect with patch.object(base_client._mqtt, "subscribe") as mock_subscribe: with patch.object(base_client._mqtt, "publish") as mock_publish: - base_client._mqtt_on_connect(base_client._mqtt, None, {}, 0, {}) + base_client._mqtt_on_connect( + base_client._mqtt, + None, + mqtt.ConnectFlags(False), + mqtt.ReasonCode(mqtt.PacketTypes.CONNACK), + None, + ) mock_subscribe.assert_called_once_with(topic1) mock_publish.assert_called_once_with(pending_topic, pending_command) assert len(base_client._pending_commands) == 0 @@ -163,7 +169,13 @@ def test_mqtt_on_disconnect_user_initiated( ) -> None: """Test _mqtt_on_disconnect method with user initiated disconnect.""" with patch("asyncio.run_coroutine_threadsafe") as mock_run_coroutine_threadsafe: - base_client._mqtt_on_disconnect(base_client._mqtt, None, 0) + base_client._mqtt_on_disconnect( + base_client._mqtt, + None, + mqtt.DisconnectFlags(False), + mqtt.ReasonCode(mqtt.PacketTypes.DISCONNECT), + None, + ) mock_run_coroutine_threadsafe.assert_not_called() def test_mqtt_on_disconnect_unexpected( @@ -171,7 +183,13 @@ def test_mqtt_on_disconnect_unexpected( ) -> None: """Test _mqtt_on_disconnect method with unexpected disconnect.""" with patch("asyncio.run_coroutine_threadsafe") as mock_run_coroutine_threadsafe: - base_client._mqtt_on_disconnect(base_client._mqtt, None, 1) + base_client._mqtt_on_disconnect( + base_client._mqtt, + None, + mqtt.DisconnectFlags(False), + mqtt.ReasonCode(mqtt.PacketTypes.DISCONNECT, "Unspecified error"), + None, + ) mock_run_coroutine_threadsafe.assert_called_once() def test_mqtt_on_message(self, base_client: MockBaseDeyeMqttClient) -> None: