if name.lower().find(b'respeaker') >= 0 and dev['maxInputChannels'] > 0:
Never satisfies and device_index doesn't get set. Later logic continues to create a stream object using None as device index, instead setting it to default device returned from pyaudio_instance.get_default_input_device_info() to handle things correctly.
When using independently the if condition:
if name.lower().find(b'respeaker') >= 0 and dev['maxInputChannels'] > 0:
Never satisfies and device_index doesn't get set. Later logic continues to create a stream object using None as device index, instead setting it to default device returned from
pyaudio_instance.get_default_input_device_info()
to handle things correctly.