# Buggy code
def perform_hw1_preflight(self):
    try:
        firmwareInfo = self.dongleObject.getFirmwareVersion()
        firmware = firmwareInfo["version"]
        self.multiOutputSupported = versiontuple(firmware) >= versiontuple(
            MULTI_OUTPUT_SUPPORT
        )
        self.nativeSegwitSupported = versiontuple(firmware) >= versiontuple(
            SEGWIT_SUPPORT
        )
        self.segwitSupported = self.nativeSegwitSupported or (
            firmwareInfo["specialVersion"] == 0x20
            and versiontuple(firmware) >= versiontuple(SEGWIT_SUPPORT_SPECIAL)
        )

        if not checkFirmware(firmwareInfo):
            self.dongleObject.dongle.close()
            raise UserFacingException(MSG_NEEDS_FW_UPDATE_GENERIC)
        try:
            self.dongleObject.getOperationMode()
        except BTChipException as e:
            if e.sw == 0x6985:
                self.dongleObject.dongle.close()
                self.handler.get_setup()
                # Acquire the new client on the next run
            else:
                raise e
        if (
            self.has_detached_pin_support(self.dongleObject)
            and not self.is_pin_validated(self.dongleObject)
            and (self.handler is not None)
        ):
            remaining_attempts = self.dongleObject.getVerifyPinRemainingAttempts()
            if remaining_attempts != 1:
                msg = "Enter your Ledger PIN - remaining attempts : " + str(
                    remaining_attempts
                )
            else:
                msg = "Enter your Ledger PIN - WARNING : LAST ATTEMPT. If the PIN is not correct, the dongle will be wiped."
            confirmed, p, pin = self.password_dialog(msg)
            if not confirmed:
                raise UserFacingException(
                    "Aborted by user - please unplug the dongle and plug it again before retrying"
                )
            pin = pin.encode()
            self.dongleObject.verifyPin(pin)
    except BTChipException as e:
        if e.sw == 0x6FAA:
            raise UserFacingException(
                "Dongle is temporarily locked - please unplug it and replug it again"
            )
        if (e.sw & 0xFFF0) == 0x63C0:
            raise UserFacingException(
                "Invalid PIN - please unplug the dongle and plug it again before retrying"
            )
        if e.sw == 0x6F00 and e.message == "Invalid channel":
            # based on docs 0x6f00 might be a more general error, hence we also compare message to be sure
            raise UserFacingException(
                "Invalid channel.\n"
                "Please make sure that 'Browser support' is disabled on your device."
            )
        raise e


# Traceback (electrum.plugin.DeviceUnpairableError)
Traceback (most recent call last):
File "/home/matejcik/projekty/prace/electrum/electrum/gui/qt/util.py", line 718, in run
result = task.task()
File "/home/matejcik/projekty/prace/electrum/electrum/plugins/trezor/trezor.py", line 179, in get_client
client = devmgr.client_for_keystore(self, handler, keystore, force_pair)
File "/home/matejcik/projekty/prace/electrum/electrum/plugin.py", line 457, in client_for_keystore
client = self.force_pair_xpub(plugin, handler, info, xpub, derivation, devices)
File "/home/matejcik/projekty/prace/electrum/electrum/plugin.py", line 505, in force_pair_xpub
'receive will be unspendable.').format(plugin.device))
electrum.plugin.DeviceUnpairableError: Electrum cannot pair with your Trezor.

# Fixed code
def perform_hw1_preflight(self):
    try:
        firmwareInfo = self.dongleObject.getFirmwareVersion()
        firmware = firmwareInfo["version"]
        self.multiOutputSupported = versiontuple(firmware) >= versiontuple(
            MULTI_OUTPUT_SUPPORT
        )
        self.nativeSegwitSupported = versiontuple(firmware) >= versiontuple(
            SEGWIT_SUPPORT
        )
        self.segwitSupported = self.nativeSegwitSupported or (
            firmwareInfo["specialVersion"] == 0x20
            and versiontuple(firmware) >= versiontuple(SEGWIT_SUPPORT_SPECIAL)
        )

        if not checkFirmware(firmwareInfo):
            self.dongleObject.dongle.close()
            raise UserFacingException(MSG_NEEDS_FW_UPDATE_GENERIC)
        try:
            self.dongleObject.getOperationMode()
        except BTChipException as e:
            if e.sw == 0x6985:
                self.dongleObject.dongle.close()
                self.handler.get_setup()
                # Acquire the new client on the next run
            else:
                raise e
        if self.has_detached_pin_support(
            self.dongleObject
        ) and not self.is_pin_validated(self.dongleObject):
            assert self.handler, "no handler for client"
            remaining_attempts = self.dongleObject.getVerifyPinRemainingAttempts()
            if remaining_attempts != 1:
                msg = "Enter your Ledger PIN - remaining attempts : " + str(
                    remaining_attempts
                )
            else:
                msg = "Enter your Ledger PIN - WARNING : LAST ATTEMPT. If the PIN is not correct, the dongle will be wiped."
            confirmed, p, pin = self.password_dialog(msg)
            if not confirmed:
                raise UserFacingException(
                    "Aborted by user - please unplug the dongle and plug it again before retrying"
                )
            pin = pin.encode()
            self.dongleObject.verifyPin(pin)
    except BTChipException as e:
        if e.sw == 0x6FAA:
            raise UserFacingException(
                "Dongle is temporarily locked - please unplug it and replug it again"
            )
        if (e.sw & 0xFFF0) == 0x63C0:
            raise UserFacingException(
                "Invalid PIN - please unplug the dongle and plug it again before retrying"
            )
        if e.sw == 0x6F00 and e.message == "Invalid channel":
            # based on docs 0x6f00 might be a more general error, hence we also compare message to be sure
            raise UserFacingException(
                "Invalid channel.\n"
                "Please make sure that 'Browser support' is disabled on your device."
            )
        raise e